fix: tenant-admin blog create 500 — authorId stamped the Clerk id, violating posts_authorId_fkey - #226
Merged
Merged
Conversation
…eate 500
posts.authorId FKs users.id, but getCurrentUser().id is the Clerk id; they
only coincide for webhook-provisioned rows keyed by the raw Clerk id. Tenant
admins provisioned by tenant-create/team-invite/seeding have UUID PKs, so
every blog create violated posts_authorId_fkey and 500'd (lekkerweed, weeks).
Resolve the tenant-scoped local row (clerkUserId -> id -> email) and fall
back to the Clerk id only when no row is visible (impersonating super-admin,
whose Clerk-keyed row the FK already accepts). Residual P2003 now maps to a
clear 409 instead of an opaque 500.
Also fix include:{author} in both API GETs — the Prisma relation is named
users (introspected), so author threw P2009 and 500'd those routes; alias
the relation back to author in the JSON response.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughTenant-admin post creation now resolves tenant users before author assignment and handles missing author records. Post GET handlers load the ChangesTenant post author handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant TenantAdmin
participant POSTRoute
participant PrismaUsers
participant PrismaPosts
TenantAdmin->>POSTRoute: Submit post
POSTRoute->>PrismaUsers: Resolve tenant user
PrismaUsers-->>POSTRoute: Return local user or no match
POSTRoute->>PrismaPosts: Create post with resolved author ID
PrismaPosts-->>POSTRoute: Return post or foreign-key error
POSTRoute-->>TenantAdmin: Return post or HTTP 409
Possibly related PRs
Suggested reviewers: ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The lekkerweed tenant admin has been unable to create a blog post for weeks — every attempt 500s. Railway logs (2026-08-06 13:26–13:27 WEST, correlationIds
85073817/7396fd0a) show the actual failure:This is not a permissions problem — auth passed (500, not 401/403).
POST /api/tenant-admin/postsstampsauthorId: user.id, andgetCurrentUser()returns the Clerk user id verbatim. That only matchesusers.idfor rows the Clerk webhook itself created (which keys new rows by the raw Clerk id — the webhook even carries a comment about this exact FK trap). Admins provisioned by tenant-create / team-invite / seeding have UUID primary keys, so the FK fails on every create. Super-admin works only because that row happens to be Clerk-keyed.Fix
usersrow (clerkUserId→id→email, all unique) and stamps its id. The lookup runs tenant-scoped, so an impersonating super-admin (row outside the tenant) misses and falls back touser.id— exactly the Clerk-keyed row the FK already accepts today, preserving current super-admin behaviour.P2003now returns a clear 409 ("author account not linked") instead of an opaque 500.include: { author }, but the introspected Prisma relation is namedusers— those routes threw P2009 and 500'd whenever called. Now includeusersand alias it back toauthorin the JSON response (the UI doesn't call these — it server-renders — so no consumer change).Audit-log
userId: user.idwrites were swept and are safe (audit_logs.userIdis a plain string, no FK).posts.authorIdwas the only FK-constrained stamp of the Clerk id.Test plan
tests/unit/posts-author-fk.test.ts(mirrorsposts-slug-scope.test.tsmocking):authorId= local UUID row idauthorIdand renders on the storefront blog.Follow-ups (not this PR)
security.tenant_context_missingwarnings flood the logs forposts.findMany— server components (the-wire,store/[slug]) query with explicitwhere: { tenantId }but outside the ALS tenant context. Benign but noisy.[id]posts routes still usewithAuth+ manual email/tenant checks rather thanwithTenantAuthParams— works, but inconsistent with the collection route.Summary by CodeRabbit