Skip to content

feat(server): canonical organization_domains writers (#4159 Stage 3a) - #4329

Merged
bokelley merged 1 commit into
mainfrom
bokelley/4159-stage3a-writer-primitives
May 10, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/4159-stage3a-writer-primitives

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Stage 3a of #4159. Stage 2 collapsed four overlapping domain columns onto `organization_domains.is_primary=true`. This PR consolidates the write paths behind two canonical primitives so every caller goes through the same SQL with the same invariants.

New module: `db/organization-domains-db.ts`

  • `linkDomain({orgId, domain, source, verified, isPrimary?})` — INSERT a row. `ON CONFLICT (domain) DO NOTHING`. Logs when the existing row is owned by a different org (organization_domains.is_primary write paths bypass member-self-service WorkOS-source gate #4321 pattern, generalized). When `isPrimary=true` and the row was actually inserted, also denormalizes `organizations.email_domain` — closes a latent drift in the prior member-profiles / prospect call sites that updated only one side.

  • `setPrimaryDomain({orgId, domain, requireSource?})` — atomic clear-existing-primary + set-new-primary + update `organizations.email_domain`. Locks the org row via FOR UPDATE. Optional `requireSource` allowlist (e.g. `['workos']` for member self-service).

Migrated call sites

File Now uses Notes
`services/prospect.ts` `linkDomain` Replaces #4323's hand-rolled INSERT + log
`services/organization-bootstrap.ts` `linkDomain` Drops manual `email_domain` UPDATE — primitive does it
`routes/member-profiles.ts` `linkDomain` Drops `LOWER()` pre-check (upstream domain is already lowercased via `getEmailDomain`)
`routes/me-organization-domains.ts` `setPrimaryDomain` Eliminates ~80 lines of inline transaction code
`addie/bolt-app.ts` `linkDomain` Alias-confirm tool, `isPrimary=false`

Out of scope

Deferred to Stage 3b/3c:

  • `routes/workos-webhooks.ts` — FOR UPDATE locking + idempotency invariants. The webhook composes link + auto-promote in a way the primitives don't yet model; needs careful migration.
  • `routes/admin/domains.ts` — ~10 call sites with diverging flows (set primary, unlink, admin import, recover-from-conflict). Worth a focused PR.
  • `routes/admin/organizations.ts:1001` — admin tool with custom branching.

Test plan

  • New `tests/integration/organization-domains-db.test.ts` — 9 cases covering both primitives + invariants (idempotent re-link, cross-org conflict, isPrimary email_domain sync, source allowlist, atomic flip).
  • Pre-existing call-site tests still pass: `prospect-domain-conflict`, `me-organization-domains`, `member-profile-bootstrap`, `workos-domain-auto-primary`, `find-claimable-prospect-org`, `claim-prospect-org`, `prospect-triage-function` (51/51).
  • Typecheck clean.

🤖 Generated with Claude Code

Introduce db/organization-domains-db.ts with two primitives:

- linkDomain: INSERT a row, ON CONFLICT DO NOTHING, log when the existing
  row is owned by a different org (#4321 pattern, generalized). When
  isPrimary=true and the row was actually inserted, also denormalize
  organizations.email_domain — closes the latent drift in the prior
  member-profiles and prospect call sites that updated only one side.

- setPrimaryDomain: atomic clear-existing-primary + set-new-primary +
  update organizations.email_domain. Locks the org row via FOR UPDATE.
  Optional requireSource allowlist (e.g. ['workos'] for member
  self-service).

Migrate the simpler call sites: prospect.ts, organization-bootstrap.ts,
member-profiles.ts (bootstrap), me-organization-domains.ts, bolt-app.ts.

WorkOS webhook (FOR UPDATE locking + idempotency invariants) and
admin/domains.ts (~10 call sites with diverging flows) are deferred to
Stage 3b/3c.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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