convertOrganizationMembers batches the role lookup to make only 1 sql call We
(ctx context.Context, db database.Store, mems []database.OrganizationMember)
| 504 | // convertOrganizationMembers batches the role lookup to make only 1 sql call |
| 505 | // We |
| 506 | func convertOrganizationMembers(ctx context.Context, db database.Store, mems []database.OrganizationMember) ([]codersdk.OrganizationMember, error) { |
| 507 | converted := make([]codersdk.OrganizationMember, 0, len(mems)) |
| 508 | roleLookup := make([]database.NameOrganizationPair, 0) |
| 509 | |
| 510 | for _, m := range mems { |
| 511 | converted = append(converted, codersdk.OrganizationMember{ |
| 512 | UserID: m.UserID, |
| 513 | OrganizationID: m.OrganizationID, |
| 514 | CreatedAt: m.CreatedAt, |
| 515 | UpdatedAt: m.UpdatedAt, |
| 516 | Roles: slice.List(m.Roles, func(r string) codersdk.SlimRole { |
| 517 | // If it is a built-in role, no lookups are needed. |
| 518 | rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{Name: r, OrganizationID: m.OrganizationID}) |
| 519 | if err == nil { |
| 520 | return db2sdk.SlimRole(rbacRole) |
| 521 | } |
| 522 | |
| 523 | // We know the role name and the organization ID. We are missing the |
| 524 | // display name. Append the lookup parameter, so we can get the display name |
| 525 | roleLookup = append(roleLookup, database.NameOrganizationPair{ |
| 526 | Name: r, |
| 527 | OrganizationID: m.OrganizationID, |
| 528 | }) |
| 529 | return codersdk.SlimRole{ |
| 530 | Name: r, |
| 531 | DisplayName: "", |
| 532 | OrganizationID: m.OrganizationID.String(), |
| 533 | } |
| 534 | }), |
| 535 | }) |
| 536 | } |
| 537 | |
| 538 | customRoles, err := db.CustomRoles(ctx, database.CustomRolesParams{ |
| 539 | LookupRoles: roleLookup, |
| 540 | ExcludeOrgRoles: false, |
| 541 | OrganizationID: uuid.Nil, |
| 542 | IncludeSystemRoles: false, |
| 543 | }) |
| 544 | if err != nil { |
| 545 | // We are missing the display names, but that is not absolutely required. So just |
| 546 | // return the converted and the names will be used instead of the display names. |
| 547 | return converted, xerrors.Errorf("lookup custom roles: %w", err) |
| 548 | } |
| 549 | |
| 550 | // Now map the customRoles back to the slimRoles for their display name. |
| 551 | customRolesMap := make(map[string]database.CustomRole) |
| 552 | for _, role := range customRoles { |
| 553 | customRolesMap[role.RoleIdentifier().UniqueName()] = role |
| 554 | } |
| 555 | |
| 556 | for i := range converted { |
| 557 | for j, role := range converted[i].Roles { |
| 558 | if cr, ok := customRolesMap[role.UniqueName()]; ok { |
| 559 | converted[i].Roles[j].DisplayName = cr.DisplayName |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 |
no test coverage detected