Files
calendar/packages/features/auth/signup/utils/organization.ts
T
0970c77009 fix: Revert -> "Revert "chore: Chore team metadata table + isOrganization migration (#13700)
* Revert "Revert "chore: Chore team metadata table + isOrganization migration from metadata (#12828)""

This reverts commit 2408338ed4.

* Remove constraint slug,isOrganization and reset migration

* fix: conflicts

* change schema to bust cache

* Fix issues reported by TS

* change schema to bust cache

* Review fixes

* Colaesce for orgAutoAcceptEmail as well

* Fix missing negation

---------

Co-authored-by: zomars <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
2024-03-01 15:04:28 +00:00

85 lines
1.9 KiB
TypeScript

import { updateNewTeamMemberEventTypes } from "@calcom/lib/server/queries";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { prisma } from "@calcom/prisma";
import type { Team, OrganizationSettings } from "@calcom/prisma/client";
import { getOrgUsernameFromEmail } from "./getOrgUsernameFromEmail";
export async function joinAnyChildTeamOnOrgInvite({
userId,
org,
}: {
userId: number;
org: Pick<Team, "id"> & {
organizationSettings: OrganizationSettings | null;
};
}) {
const user = await prisma.user.findUnique({
where: {
id: userId,
},
});
if (!user) {
throw new Error("User not found");
}
const orgUsername =
user.username ||
getOrgUsernameFromEmail(user.email, org.organizationSettings?.orgAutoAcceptEmail ?? null);
await prisma.$transaction([
// Simply remove this update when we remove the `organizationId` field from the user table
prisma.user.update({
where: {
id: userId,
},
data: {
organizationId: org.id,
},
}),
prisma.profile.upsert({
create: {
uid: ProfileRepository.generateProfileUid(),
userId: userId,
organizationId: org.id,
username: orgUsername,
},
update: {
username: orgUsername,
},
where: {
userId_organizationId: {
userId: user.id,
organizationId: org.id,
},
},
}),
prisma.membership.updateMany({
where: {
userId,
team: {
id: org.id,
},
accepted: false,
},
data: {
accepted: true,
},
}),
prisma.membership.updateMany({
where: {
userId,
team: {
parentId: org.id,
},
accepted: false,
},
data: {
accepted: true,
},
}),
]);
await updateNewTeamMemberEventTypes(userId, org.id);
}