fix: use upsert to prevent race condition in addOrganizationMember

Concurrent invites for the same user could both pass the existence check
and then the second create() would throw a unique constraint violation.
This commit is contained in:
Kevin Turcios 2026-04-13 15:17:44 -05:00
parent 8202ea512c
commit 4269ec0275

View file

@ -131,19 +131,16 @@ export async function addOrganizationMember(
return createErrorResponse("User is already a member of this organization")
}
// Check if user exists in our database
let user = await getUserById(invitedUserId)
// If user doesn't exist, create them
if (!user) {
user = await prisma.users.create({
data: {
user_id: invitedUserId,
github_username: invitedUser.username,
onboarding_completed: false,
},
})
}
// Ensure user exists in our database (upsert handles concurrent invites safely)
const user = await prisma.users.upsert({
where: { user_id: invitedUserId },
update: {},
create: {
user_id: invitedUserId,
github_username: invitedUser.username,
onboarding_completed: false,
},
})
// Add user to organization members
const newMember = await prisma.organization_members.create({