cal.pub0.org/packages/trpc/server/routers/viewer/teams/create.handler.ts

93 lines
2.4 KiB
TypeScript

import { closeComUpsertTeamUser } from "@calcom/lib/sync/SyncServiceManager";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import type { TCreateInputSchema } from "./create.schema";
type CreateOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TCreateInputSchema;
};
export const createHandler = async ({ ctx, input }: CreateOptions) => {
const { user } = ctx;
const { slug, name, logo } = input;
const isOrgChildTeam = !!user.organizationId;
// For orgs we want to create teams under the org
if (user.organizationId && !user.organization.isOrgAdmin) {
throw new TRPCError({ code: "FORBIDDEN", message: "org_admins_can_create_new_teams" });
}
const slugCollisions = await prisma.team.findFirst({
where: {
slug: slug,
// If this is under an org, check that the team doesn't already exist
parentId: isOrgChildTeam ? user.organizationId : null,
},
});
if (slugCollisions) throw new TRPCError({ code: "BAD_REQUEST", message: "team_url_taken" });
if (user.organizationId) {
const nameCollisions = await prisma.user.findFirst({
where: {
organizationId: user.organization.id,
username: slug,
},
});
if (nameCollisions) throw new TRPCError({ code: "BAD_REQUEST", message: "team_slug_exists_as_user" });
}
// Ensure that the user is not duplicating a requested team
const duplicatedRequest = await prisma.team.findFirst({
where: {
members: {
some: {
userId: ctx.user.id,
},
},
metadata: {
path: ["requestedSlug"],
equals: slug,
},
},
});
if (duplicatedRequest) {
return duplicatedRequest;
}
const createTeam = await prisma.team.create({
data: {
...(isOrgChildTeam ? { slug } : {}),
name,
logo,
members: {
create: {
userId: ctx.user.id,
role: MembershipRole.OWNER,
accepted: true,
},
},
metadata: !isOrgChildTeam
? {
requestedSlug: slug,
}
: undefined,
...(isOrgChildTeam && { parentId: user.organizationId }),
},
});
// Sync Services: Close.com
closeComUpsertTeamUser(createTeam, ctx.user, MembershipRole.OWNER);
return createTeam;
};