diff --git a/packages/features/auth/signup/handlers/calcomHandler.ts b/packages/features/auth/signup/handlers/calcomHandler.ts index 4d6b0101c3..ac06cebd2e 100644 --- a/packages/features/auth/signup/handlers/calcomHandler.ts +++ b/packages/features/auth/signup/handlers/calcomHandler.ts @@ -124,6 +124,7 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) { }, }); + // Wrapping in a transaction as if one fails we want to rollback the whole thing to preventa any data inconsistencies const membership = await prisma.$transaction(async (tx) => { if (teamMetadata?.isOrganization) { await tx.user.update({ diff --git a/packages/features/auth/signup/utils/organization.ts b/packages/features/auth/signup/utils/organization.ts index 3b95e4f1ec..c34474e324 100644 --- a/packages/features/auth/signup/utils/organization.ts +++ b/packages/features/auth/signup/utils/organization.ts @@ -1,3 +1,5 @@ +import prisma from "@calcom/prisma"; + export async function joinOrganization({ organizationId, userId, @@ -16,41 +18,38 @@ export async function joinOrganization({ } export async function joinAnyChildTeamOnOrgInvite({ userId, orgId }: { userId: number; orgId: number }) { - // Join ORG - await prisma.user.update({ - where: { - id: userId, - }, - data: { - organizationId: orgId, - }, - }); - - /** We do a membership update twice so we can join the ORG invite if the user is invited to a team witin a ORG. */ - await prisma.membership.updateMany({ - where: { - userId, - team: { - id: orgId, + await prisma.$transaction([ + prisma.user.update({ + where: { + id: userId, }, - accepted: false, - }, - data: { - accepted: true, - }, - }); - - // Join any other invites - await prisma.membership.updateMany({ - where: { - userId, - team: { - parentId: orgId, + data: { + organizationId: orgId, }, - accepted: false, - }, - data: { - accepted: true, - }, - }); + }), + prisma.membership.updateMany({ + where: { + userId, + team: { + id: orgId, + }, + accepted: false, + }, + data: { + accepted: true, + }, + }), + prisma.membership.updateMany({ + where: { + userId, + team: { + parentId: orgId, + }, + accepted: false, + }, + data: { + accepted: true, + }, + }), + ]); }