Transaction + comments
parent
ef4e4b2dcc
commit
16d6cdc57e
|
@ -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({
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import prisma from "@calcom/prisma";
|
||||
|
||||
export async function joinOrganization({
|
||||
organizationId,
|
||||
userId,
|
||||
|
@ -16,18 +18,16 @@ export async function joinOrganization({
|
|||
}
|
||||
|
||||
export async function joinAnyChildTeamOnOrgInvite({ userId, orgId }: { userId: number; orgId: number }) {
|
||||
// Join ORG
|
||||
await prisma.user.update({
|
||||
await prisma.$transaction([
|
||||
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({
|
||||
}),
|
||||
prisma.membership.updateMany({
|
||||
where: {
|
||||
userId,
|
||||
team: {
|
||||
|
@ -38,10 +38,8 @@ export async function joinAnyChildTeamOnOrgInvite({ userId, orgId }: { userId: n
|
|||
data: {
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Join any other invites
|
||||
await prisma.membership.updateMany({
|
||||
}),
|
||||
prisma.membership.updateMany({
|
||||
where: {
|
||||
userId,
|
||||
team: {
|
||||
|
@ -52,5 +50,6 @@ export async function joinAnyChildTeamOnOrgInvite({ userId, orgId }: { userId: n
|
|||
data: {
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue