Skip to content

Commit 6758ced

Browse files
fix invite redemption when user already a member
User was hitting a unique constraint on UserToOrg(orgId, userId) when redeeming an invite, because onCreateUser auto-joins new signups in self-serve mode and redeemInvite then tried to create the same row. Make the insert idempotent via upsert so the downstream AccountRequest and invite cleanup still runs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 74dd139 commit 6758ced

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

packages/web/src/lib/authUtils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,22 @@ export const addUserToOrganization = async (userId: string, orgId: number): Prom
193193
}
194194

195195
await __unsafePrisma.$transaction(async (tx) => {
196-
await tx.userToOrg.create({
197-
data: {
196+
// Upsert rather than create: the user may already be a member from the
197+
// self-serve auto-join in onCreateUser, in which case this call is
198+
// just here to trigger the AccountRequest / Invite cleanup below.
199+
await tx.userToOrg.upsert({
200+
where: {
201+
orgId_userId: {
202+
orgId: org.id,
203+
userId: user.id,
204+
},
205+
},
206+
create: {
198207
userId: user.id,
199208
orgId: org.id,
200209
role: OrgRole.MEMBER,
201-
}
210+
},
211+
update: {},
202212
});
203213

204214
// Delete the account request if it exists since we've added the user to the org

0 commit comments

Comments
 (0)