Skip to content

fix(users): detach portal users instead of deleting against RESTRICT FKs - #368

Merged
mortondev merged 5 commits into
mainfrom
fix/remove-portal-user-chat-fk
Aug 14, 2026
Merged

fix(users): detach portal users instead of deleting against RESTRICT FKs#368
mortondev merged 5 commits into
mainfrom
fix/remove-portal-user-chat-fk

Conversation

@mortondev

@mortondev mortondev commented Aug 14, 2026

Copy link
Copy Markdown
Member

Summary

Admin → Users → Remove from portal deleted the principal. Chat conversations/messages (and posts/comments) are ON DELETE RESTRICT, so Postgres failed, React Query rolled back, and no toast was shown.

Deleting would also drop history. Instead we detach: clear userId, mark the principal anonymous as “Removed user”, keep the Better-Auth user so a later sign-in provisions a fresh principal. The users list inner-joins user, so they disappear immediately. Failures now toast.

Closes #363

Test plan

  • Remove a portal user who only opened chat → they leave the list, conversation remains as “Removed user”
  • Remove a portal user with posts → posts remain, author shows as Removed user
  • Same email can sign in again and get a new principal
  • Include Anonymous filter still does not list them (no user row join)

Verified locally against Docker Postgres (transaction rollback): detach clears visitorEmail/sessions, remints a new principal on identify, authored posts stay as Removed user, inner-join list omits the detached row.

Chat and posts restrict principal deletes, so Remove from portal
failed silently. Anonymize the principal (keep the user row) and
toast mutation failures.

Closes #363
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Detach alone left the Better-Auth cookie live, so getOptionalAuth
provisioned a new principal on the next request. Drop sessions in
the same transaction as the anonymize update.
@CLAassistant

CLAassistant commented Aug 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Bootstrap caches type/role per user for 5 minutes. Invalidate it
after detach so a later sign-in cannot reuse the removed principal.
Spreading unknown[] into a two-arg mock failed TS2556 on the
check job. Accept rest parameters so the factory typechecks.
@mortondev
mortondev force-pushed the fix/remove-portal-user-chat-fk branch from fa884e5 to 7358887 Compare August 14, 2026 15:22
@cursor

cursor Bot commented Aug 14, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

Null conversation visitorEmail in the same transaction, treat cache
invalidation as best-effort, and mint a fresh principal when identify
finds a detached user row.
@mortondev
mortondev merged commit 95a4592 into main Aug 14, 2026
7 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.

Admin Remove from portal fails silently when user has chat RESTRICT FKs

2 participants