Fix transactional usage to use interactive handlers

pull/11421/merge^2
Sean Brydon 2023-09-27 15:14:55 +01:00
parent c30e867147
commit 914161ab08
2 changed files with 44 additions and 28 deletions

View File

@ -15,7 +15,7 @@ import prisma from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums"; import { IdentityProvider } from "@calcom/prisma/enums";
import { signupSchema, teamMetadataSchema } from "@calcom/prisma/zod-utils"; import { signupSchema, teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { joinOrganization, joinAnyChildTeamOnOrgInvite } from "../utils/organization"; import { joinAnyChildTeamOnOrgInvite } from "../utils/organization";
import { findTokenByToken, throwIfTokenExpired, validateUsernameForTeam } from "../utils/token"; import { findTokenByToken, throwIfTokenExpired, validateUsernameForTeam } from "../utils/token";
async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) { async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
@ -124,21 +124,29 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
}, },
}); });
if (teamMetadata?.isOrganization) { const membership = await prisma.$transaction(async (tx) => {
await joinOrganization({ if (teamMetadata?.isOrganization) {
organizationId: team.id, await tx.user.update({
userId: user.id, where: {
}); id: user.id,
} },
data: {
organizationId: team.id,
},
});
}
const membership = await prisma.membership.update({ const membership = await tx.membership.update({
where: { where: {
userId_teamId: { userId: user.id, teamId: team.id }, userId_teamId: { userId: user.id, teamId: team.id },
}, },
data: { data: {
accepted: true, accepted: true,
}, },
});
return membership;
}); });
closeComUpsertTeamUser(team, user, membership.role); closeComUpsertTeamUser(team, user, membership.role);
// Accept any child team invites for orgs. // Accept any child team invites for orgs.

View File

@ -12,7 +12,7 @@ import { IdentityProvider } from "@calcom/prisma/enums";
import { signupSchema } from "@calcom/prisma/zod-utils"; import { signupSchema } from "@calcom/prisma/zod-utils";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils"; import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { joinAnyChildTeamOnOrgInvite, joinOrganization } from "../utils/organization"; import { joinAnyChildTeamOnOrgInvite } from "../utils/organization";
import { findTokenByToken, throwIfTokenExpired, validateUsernameForTeam } from "../utils/token"; import { findTokenByToken, throwIfTokenExpired, validateUsernameForTeam } from "../utils/token";
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@ -66,21 +66,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}, },
}); });
if (teamMetadata?.isOrganization) { const membership = await prisma.$transaction(async (tx) => {
await joinOrganization({ if (teamMetadata?.isOrganization) {
organizationId: team.id, await tx.user.update({
userId: user.id, where: {
}); id: user.id,
} },
data: {
organizationId: team.id,
},
});
}
const membership = await prisma.membership.update({ const membership = await tx.membership.update({
where: { where: {
userId_teamId: { userId: user.id, teamId: team.id }, userId_teamId: { userId: user.id, teamId: team.id },
}, },
data: { data: {
accepted: true, accepted: true,
}, },
});
return membership;
}); });
closeComUpsertTeamUser(team, user, membership.role); closeComUpsertTeamUser(team, user, membership.role);
// Accept any child team invites for orgs. // Accept any child team invites for orgs.