mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
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:
parent
8202ea512c
commit
4269ec0275
1 changed files with 10 additions and 13 deletions
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in a new issue