Transaction + comments

feat/signup-refactor
Sean Brydon 2023-10-26 11:11:28 +01:00
parent ef4e4b2dcc
commit 16d6cdc57e
2 changed files with 35 additions and 35 deletions

View File

@ -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({

View File

@ -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,
},
}),
]);
}