Skip to content

Fix admin members API N+1 query and extract to dedicated module - #510

Merged
bokelley merged 1 commit into
mainfrom
bokelley/admin-members-fix
Jan 3, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/admin-members-fix

Conversation

@bokelley

@bokelley bokelley commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace N+1 WorkOS API calls with efficient SQL using LEFT JOIN LATERAL to get member emails from local organization_memberships table
  • Move 5 members-related routes from http.ts to new server/src/routes/admin/members.ts module
  • Reduces http.ts by ~456 lines

Problem

The /api/admin/members endpoint was making N+1 WorkOS API calls - one listOrganizationMemberships() and one getUser() call for every organization. With hundreds of organizations, this caused timeouts and "Load failed" errors in production.

Solution

Use efficient SQL with LEFT JOIN LATERAL to get the first member email from the local organization_memberships table (which is synced from WorkOS via webhooks). This replaces potentially hundreds of API calls with a single database query.

Routes Moved

  • GET /api/admin/members - List all members with subscription info
  • POST /api/admin/members/:orgId/sync - Sync from WorkOS/Stripe
  • PATCH /api/admin/members/:orgId/memberships/:membershipId - Update membership role
  • GET /api/admin/members/:orgId/payments - Get payment history
  • DELETE /api/admin/members/:orgId - Delete workspace

Test plan

  • Verified build passes
  • All 144 tests pass
  • Tested admin/members page loads correctly in local dev
  • Tested admin/domain-health page loads correctly (no redirect)

🤖 Generated with Claude Code

- Replace N+1 WorkOS API calls with efficient SQL using LEFT JOIN LATERAL
  to get member emails from local organization_memberships table
- Move 5 members-related routes from http.ts to new members.ts module:
  - GET /api/admin/members (list all members)
  - POST /api/admin/members/:orgId/sync (sync from WorkOS/Stripe)
  - PATCH /api/admin/members/:orgId/memberships/:membershipId (update role)
  - GET /api/admin/members/:orgId/payments (payment history)
  - DELETE /api/admin/members/:orgId (delete workspace)
- Reduces http.ts by ~456 lines

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@bokelley
bokelley merged commit 9caf414 into main Jan 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant