-
Notifications
You must be signed in to change notification settings - Fork 1
[API-69] Implement managed users endpoint #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ee4133
add endpoint to get managed users
schottra c68fb7a
add tests and fix bad queries
schottra 7a4a4d7
200 test for managed_users endpoint
schottra d242a54
PR feedback
schottra e3ea040
optional bool helper
schottra 1577c72
Merge remote-tracking branch 'origin/main' into grants-endpoints-2
schottra 12dd70a
fix tests with new jsonAssert
schottra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package dbv1 | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| type FullManagerGrant struct { | ||
| GetGrantsForUserIdRow | ||
| GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` | ||
| } | ||
|
|
||
| type FullManagedUserGrant struct { | ||
| GetGrantsForGranteeUserIdRow | ||
| GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` | ||
| } | ||
|
|
||
| type FullManager struct { | ||
| Manager FullUser `json:"manager"` | ||
| Grant FullManagerGrant `json:"grant"` | ||
| } | ||
|
|
||
| type FullManagedUser struct { | ||
| User FullUser `json:"user"` | ||
| Grant FullManagedUserGrant `json:"grant"` | ||
| } | ||
|
|
||
| func (q *Queries) FullManagers(ctx context.Context, params GetGrantsForUserIdParams) ([]FullManager, error) { | ||
|
|
||
| grants, err := q.GetGrantsForUserId(ctx, params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| user_ids := make([]int32, len(grants)) | ||
| for i, grant := range grants { | ||
| user_ids[i] = int32(grant.GranteeUserID) | ||
| } | ||
|
|
||
| users, err := q.FullUsersKeyed(ctx, GetUsersParams{ | ||
| Ids: user_ids, | ||
| MyID: params.UserID, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| managers := make([]FullManager, len(grants)) | ||
| for i, grant := range grants { | ||
| managers[i] = FullManager{ | ||
| Manager: users[int32(grant.GranteeUserID)], | ||
| Grant: FullManagerGrant{GetGrantsForUserIdRow: grant}, | ||
| } | ||
| } | ||
|
|
||
| return managers, nil | ||
| } | ||
|
|
||
| func (q *Queries) FullManagedUsers(ctx context.Context, params GetGrantsForGranteeUserIdParams) ([]FullManagedUser, error) { | ||
| grants, err := q.GetGrantsForGranteeUserId(ctx, GetGrantsForGranteeUserIdParams{ | ||
| IsRevoked: params.IsRevoked, | ||
| IsApproved: params.IsApproved, | ||
| UserID: params.UserID, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| user_ids := make([]int32, len(grants)) | ||
| for i, grant := range grants { | ||
| user_ids[i] = int32(grant.UserID) | ||
| } | ||
|
|
||
| users, err := q.FullUsersKeyed(ctx, GetUsersParams{ | ||
| Ids: user_ids, | ||
| MyID: params.UserID, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| managedUsers := make([]FullManagedUser, len(grants)) | ||
| for i, grant := range grants { | ||
| managedUsers[i] = FullManagedUser{ | ||
| User: users[int32(grant.UserID)], | ||
| Grant: FullManagedUserGrant{GetGrantsForGranteeUserIdRow: grant}, | ||
| } | ||
| } | ||
|
|
||
| return managedUsers, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/jackc/pgx/v5/pgtype" | ||
| ) | ||
|
|
||
| func getOptionalBool(c *fiber.Ctx, key string) (pgtype.Bool, error) { | ||
| var value *bool | ||
| if valueStr := c.Query(key); valueStr != "" { | ||
| parsed, err := strconv.ParseBool(valueStr) | ||
| if err != nil { | ||
| return pgtype.Bool{}, err | ||
| } | ||
| value = &parsed | ||
| } | ||
| return pgtype.Bool{Bool: value != nil && *value, Valid: value != nil}, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "bridgerton.audius.co/api/dbv1" | ||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| func (app *ApiServer) v1UsersManagedUsers(c *fiber.Ctx) error { | ||
| // Behavior of this field is a little odd. We only want to filter by it | ||
| // if it is passed, but otherwise not use a default value for either. | ||
| isApproved, err := getOptionalBool(c, "is_approved") | ||
| if err != nil { | ||
| return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_approved") | ||
| } | ||
|
|
||
| isRevoked, err := strconv.ParseBool(c.Query("is_revoked", "false")) | ||
| if err != nil { | ||
| return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_revoked") | ||
| } | ||
| params := dbv1.GetGrantsForGranteeUserIdParams{ | ||
| UserID: app.getUserId(c), | ||
| IsApproved: isApproved, | ||
| IsRevoked: isRevoked, | ||
| } | ||
|
|
||
| managedUsers, err := app.queries.FullManagedUsers(c.Context(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.JSON(fiber.Map{ | ||
| "data": managedUsers, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be written more simple like: