Files
calendar/packages/features/auth/signup/utils/createOrUpdateMemberships.ts
T
Benny JooandGitHub ff38d6c7db refactor: Remove circular deps between @calcom/lib and @calcom/features [1] (#24399)
* add eslint config

* migrate autoLock to features

* migrate teamService to features

* migrate userCreationService

* migrate insights services to features

* migrate ProfileRepository

* update imports

* migrate filter segmen tests

* migrate filter segment repository

* migrate getBusyTimes

* migrate getLocaleFromRequest

* refactor csvUtils

* make filename clearer

* migrate getLuckyUser integration test

* migrate autoLock test to features

* wip

* refactors

* migrate useBookerUrl

* migrate more

* wip

* Migrate eventTypeRepository

* membership repository

* update imports

* update imports

* migrate

* move organization repository

* update imports

* update imports

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix tests

* fix type checks

* fix

* fix

* migrate

* update imports

* fix tests

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
2025-10-13 12:01:02 -03:00

91 lines
2.6 KiB
TypeScript

import { updateNewTeamMemberEventTypes } from "@calcom/features/ee/teams/lib/queries";
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
import { prisma } from "@calcom/prisma";
import type { Team, User, OrganizationSettings } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import { getOrgUsernameFromEmail } from "./getOrgUsernameFromEmail";
export const createOrUpdateMemberships = async ({
user,
team,
}: {
user: Pick<User, "id">;
team: Pick<Team, "id" | "parentId" | "isOrganization"> & {
organizationSettings?: Pick<OrganizationSettings, "orgAutoAcceptEmail"> | null;
};
}) => {
return await prisma.$transaction(async (tx) => {
if (team.isOrganization) {
const dbUser = await tx.user.update({
where: {
id: user.id,
},
data: {
organizationId: team.id,
},
select: {
username: true,
email: true,
},
});
// Ideally dbUser.username should never be null, but just in case.
// This method being called only during signup means that dbUser.username should be the correct org username
const orgUsername =
dbUser.username ||
getOrgUsernameFromEmail(dbUser.email, team.organizationSettings?.orgAutoAcceptEmail ?? null);
await tx.profile.upsert({
create: {
uid: ProfileRepository.generateProfileUid(),
userId: user.id,
organizationId: team.id,
username: orgUsername,
},
update: {
username: orgUsername,
},
where: {
userId_organizationId: {
userId: user.id,
organizationId: team.id,
},
},
});
}
const membership = await tx.membership.upsert({
where: {
userId_teamId: { userId: user.id, teamId: team.id },
},
update: {
accepted: true,
},
create: {
userId: user.id,
teamId: team.id,
role: MembershipRole.MEMBER,
accepted: true,
},
});
const orgMembership = null;
if (team.parentId) {
await tx.membership.upsert({
where: {
userId_teamId: { userId: user.id, teamId: team.parentId },
},
update: {
accepted: true,
},
create: {
userId: user.id,
teamId: team.parentId,
role: MembershipRole.MEMBER,
accepted: true,
},
});
}
await updateNewTeamMemberEventTypes(user.id, team.id);
return { membership, orgMembership };
});
};