|
| 1 | +package dbv1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | +) |
| 6 | + |
| 7 | +type FullManagerGrant struct { |
| 8 | + GetGrantsForUserIdRow |
| 9 | + GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` |
| 10 | +} |
| 11 | + |
| 12 | +type FullManagedUserGrant struct { |
| 13 | + GetGrantsForGranteeUserIdRow |
| 14 | + GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` |
| 15 | +} |
| 16 | + |
| 17 | +type FullManager struct { |
| 18 | + Manager FullUser `json:"manager"` |
| 19 | + Grant FullManagerGrant `json:"grant"` |
| 20 | +} |
| 21 | + |
| 22 | +type FullManagedUser struct { |
| 23 | + User FullUser `json:"user"` |
| 24 | + Grant FullManagedUserGrant `json:"grant"` |
| 25 | +} |
| 26 | + |
| 27 | +func (q *Queries) FullManagers(ctx context.Context, params GetGrantsForUserIdParams) ([]FullManager, error) { |
| 28 | + |
| 29 | + grants, err := q.GetGrantsForUserId(ctx, params) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + user_ids := make([]int32, len(grants)) |
| 35 | + for i, grant := range grants { |
| 36 | + user_ids[i] = int32(grant.GranteeUserID) |
| 37 | + } |
| 38 | + |
| 39 | + users, err := q.FullUsersKeyed(ctx, GetUsersParams{ |
| 40 | + Ids: user_ids, |
| 41 | + MyID: params.UserID, |
| 42 | + }) |
| 43 | + |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + managers := make([]FullManager, len(grants)) |
| 49 | + for i, grant := range grants { |
| 50 | + managers[i] = FullManager{ |
| 51 | + Manager: users[int32(grant.GranteeUserID)], |
| 52 | + Grant: FullManagerGrant{GetGrantsForUserIdRow: grant}, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return managers, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (q *Queries) FullManagedUsers(ctx context.Context, params GetGrantsForGranteeUserIdParams) ([]FullManagedUser, error) { |
| 60 | + grants, err := q.GetGrantsForGranteeUserId(ctx, GetGrantsForGranteeUserIdParams{ |
| 61 | + IsRevoked: params.IsRevoked, |
| 62 | + IsApproved: params.IsApproved, |
| 63 | + UserID: params.UserID, |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + user_ids := make([]int32, len(grants)) |
| 70 | + for i, grant := range grants { |
| 71 | + user_ids[i] = int32(grant.UserID) |
| 72 | + } |
| 73 | + |
| 74 | + users, err := q.FullUsersKeyed(ctx, GetUsersParams{ |
| 75 | + Ids: user_ids, |
| 76 | + MyID: params.UserID, |
| 77 | + }) |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + managedUsers := make([]FullManagedUser, len(grants)) |
| 84 | + for i, grant := range grants { |
| 85 | + managedUsers[i] = FullManagedUser{ |
| 86 | + User: users[int32(grant.UserID)], |
| 87 | + Grant: FullManagedUserGrant{GetGrantsForGranteeUserIdRow: grant}, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return managedUsers, nil |
| 92 | +} |
0 commit comments