+1








1b5d50c45c
Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com> Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Kiran K <mailtokirankk@gmail.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com> Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>
73 lines
1.6 KiB
TypeScript
73 lines
1.6 KiB
TypeScript
import userCanCreateTeamGroupMapping from "@calcom/features/ee/dsync/lib/server/userCanCreateTeamGroupMapping";
|
|
import prisma from "@calcom/prisma";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
type Options = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
};
|
|
|
|
export const getHandler = async ({ ctx }: Options) => {
|
|
const { organizationId } = await userCanCreateTeamGroupMapping(ctx.user, ctx.user.organizationId);
|
|
|
|
// Get org teams
|
|
const teamsQuery = await prisma.team.findMany({
|
|
where: {
|
|
parentId: organizationId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
},
|
|
});
|
|
|
|
const directoryId = await prisma.dSyncData.findFirst({
|
|
where: {
|
|
organizationId,
|
|
},
|
|
select: {
|
|
directoryId: true,
|
|
},
|
|
});
|
|
|
|
if (!directoryId) {
|
|
throw new TRPCError({ code: "NOT_FOUND", message: "Could not find directory id" });
|
|
}
|
|
|
|
const teamGroupMappingQuery = await prisma.dSyncTeamGroupMapping.findMany({
|
|
where: {
|
|
directoryId: directoryId.directoryId,
|
|
},
|
|
select: {
|
|
teamId: true,
|
|
groupName: true,
|
|
},
|
|
});
|
|
|
|
const teamGroupMapping = teamsQuery.map((team) => {
|
|
return {
|
|
id: team.id,
|
|
name: team.name,
|
|
slug: team.slug,
|
|
directoryId: directoryId?.directoryId,
|
|
groupNames: teamGroupMappingQuery.reduce((groupNames, mapping) => {
|
|
if (mapping.teamId === team.id) {
|
|
groupNames.push(mapping.groupName);
|
|
}
|
|
|
|
return groupNames;
|
|
}, [] as string[]),
|
|
};
|
|
});
|
|
|
|
return {
|
|
teamGroupMapping,
|
|
};
|
|
};
|
|
|
|
export default getHandler;
|