refactor(organization): batch joinable orgs fetch, extract helper#1652
refactor(organization): batch joinable orgs fetch, extract helper#1652AmanGIT07 wants to merge 1 commit into
Conversation
Replace per-ID orgService.Get loop in ListOrganizationsByUser and ListOrganizationsByCurrentUser with a single orgService.GetByIDs call, and extract the shared joinable-orgs lookup into a joinableOrgsForEmail helper. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR adds a ChangesOrganization bulk-fetch capability
Handler refactoring to use batch fetch
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Suggested reviewers
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 606b54db-edd3-4b12-8fd3-8acffed46da5
📒 Files selected for processing (5)
core/organization/service.gointernal/api/v1beta1connect/interfaces.gointernal/api/v1beta1connect/mocks/organization_service.gointernal/api/v1beta1connect/user.gointernal/api/v1beta1connect/user_test.go
| orgs, err := h.orgService.GetByIDs(ctx, joinableOrgIDs) | ||
| if err != nil { | ||
| args := append([]any{"joinable_org_ids", joinableOrgIDs}, extraCtx...) | ||
| errorLogger.LogUnexpectedError(ctx, request, operation, err, args...) | ||
| return nil, err | ||
| } | ||
|
|
||
| pbOrgs := make([]*frontierv1beta1.Organization, 0, len(orgs)) | ||
| for _, org := range orgs { | ||
| orgPB, err := transformOrgToPB(org) | ||
| if err != nil { | ||
| errorLogger.LogTransformError(ctx, request, operation, org.ID, err) | ||
| return nil, err | ||
| } | ||
| pbOrgs = append(pbOrgs, orgPB) | ||
| } |
There was a problem hiding this comment.
Preserve deterministic joinable org ordering in batch path.
At Line 848, GetByIDs can return organizations in arbitrary DB order, so response order may now drift from joinableOrgIDs order (the previous per-ID loop preserved it).
💡 Proposed fix
orgs, err := h.orgService.GetByIDs(ctx, joinableOrgIDs)
if err != nil {
args := append([]any{"joinable_org_ids", joinableOrgIDs}, extraCtx...)
errorLogger.LogUnexpectedError(ctx, request, operation, err, args...)
return nil, err
}
- pbOrgs := make([]*frontierv1beta1.Organization, 0, len(orgs))
- for _, org := range orgs {
+ orgByID := make(map[string]organization.Organization, len(orgs))
+ for _, org := range orgs {
+ orgByID[org.ID] = org
+ }
+
+ pbOrgs := make([]*frontierv1beta1.Organization, 0, len(joinableOrgIDs))
+ for _, orgID := range joinableOrgIDs {
+ org, ok := orgByID[orgID]
+ if !ok {
+ // disabled/missing orgs are intentionally skipped
+ continue
+ }
orgPB, err := transformOrgToPB(org)
if err != nil {
errorLogger.LogTransformError(ctx, request, operation, org.ID, err)
return nil, err
}
pbOrgs = append(pbOrgs, orgPB)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| orgs, err := h.orgService.GetByIDs(ctx, joinableOrgIDs) | |
| if err != nil { | |
| args := append([]any{"joinable_org_ids", joinableOrgIDs}, extraCtx...) | |
| errorLogger.LogUnexpectedError(ctx, request, operation, err, args...) | |
| return nil, err | |
| } | |
| pbOrgs := make([]*frontierv1beta1.Organization, 0, len(orgs)) | |
| for _, org := range orgs { | |
| orgPB, err := transformOrgToPB(org) | |
| if err != nil { | |
| errorLogger.LogTransformError(ctx, request, operation, org.ID, err) | |
| return nil, err | |
| } | |
| pbOrgs = append(pbOrgs, orgPB) | |
| } | |
| orgs, err := h.orgService.GetByIDs(ctx, joinableOrgIDs) | |
| if err != nil { | |
| args := append([]any{"joinable_org_ids", joinableOrgIDs}, extraCtx...) | |
| errorLogger.LogUnexpectedError(ctx, request, operation, err, args...) | |
| return nil, err | |
| } | |
| orgByID := make(map[string]organization.Organization, len(orgs)) | |
| for _, org := range orgs { | |
| orgByID[org.ID] = org | |
| } | |
| pbOrgs := make([]*frontierv1beta1.Organization, 0, len(joinableOrgIDs)) | |
| for _, orgID := range joinableOrgIDs { | |
| org, ok := orgByID[orgID] | |
| if !ok { | |
| // disabled/missing orgs are intentionally skipped | |
| continue | |
| } | |
| orgPB, err := transformOrgToPB(org) | |
| if err != nil { | |
| errorLogger.LogTransformError(ctx, request, operation, org.ID, err) | |
| return nil, err | |
| } | |
| pbOrgs = append(pbOrgs, orgPB) | |
| } |
Coverage Report for CI Build 26448078626Coverage increased (+0.04%) to 42.847%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Summary
core/organization: addService.GetByIDsthin wrapper over the existing repository method.internal/api/v1beta1connect: exposeGetByIDson theOrganizationServiceinterface; mocks regenerated.ListOrganizationsByUserandListOrganizationsByCurrentUsernow batch-fetch joinable orgs via oneGetByIDscall instead of loopingorgService.Get(id)per ID.joinableOrgsForEmailhelper; service-user skip stays in the current-user caller.CodeInternal.joinable_org_id(string, per failed iteration) tojoinable_org_ids([]string, full attempted set).Addresses items 1 and 2 of #1637.
Test plan
go test ./internal/api/v1beta1connect/... ./core/organization/...make lintTestConnectHandler_ListOrganizationsByUser/TestConnectHandler_ListOrganizationsByCurrentUserupdated to expectGetByIDs.🤖 Generated with Claude Code