2022-02-07 23:35:26 +00:00
|
|
|
import { Prisma, UserPlan } from "@prisma/client";
|
2021-12-09 23:51:30 +00:00
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import prisma, { baseEventTypeSelect } from "@calcom/prisma";
|
2021-12-09 23:51:30 +00:00
|
|
|
|
2022-06-06 18:24:37 +00:00
|
|
|
export type TeamWithMembers = Awaited<ReturnType<typeof getTeamWithMembers>>;
|
2021-12-09 23:51:30 +00:00
|
|
|
|
|
|
|
export async function getTeamWithMembers(id?: number, slug?: string) {
|
|
|
|
const userSelect = Prisma.validator<Prisma.UserSelect>()({
|
|
|
|
username: true,
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
id: true,
|
2022-02-07 23:35:26 +00:00
|
|
|
plan: true,
|
2021-12-09 23:51:30 +00:00
|
|
|
bio: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const teamSelect = Prisma.validator<Prisma.TeamSelect>()({
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
slug: true,
|
|
|
|
logo: true,
|
|
|
|
bio: true,
|
|
|
|
hideBranding: true,
|
|
|
|
members: {
|
|
|
|
select: {
|
|
|
|
user: {
|
|
|
|
select: userSelect,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventTypes: {
|
|
|
|
where: {
|
|
|
|
hidden: false,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
users: {
|
|
|
|
select: userSelect,
|
|
|
|
},
|
2022-06-06 16:54:47 +00:00
|
|
|
...baseEventTypeSelect,
|
2021-12-09 23:51:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const team = await prisma.team.findUnique({
|
|
|
|
where: id ? { id } : { slug },
|
|
|
|
select: teamSelect,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!team) return null;
|
|
|
|
|
|
|
|
const memberships = await prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: team.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const members = team.members.map((obj) => {
|
|
|
|
const membership = memberships.find((membership) => obj.user.id === membership.userId);
|
|
|
|
return {
|
|
|
|
...obj.user,
|
2022-02-07 23:35:26 +00:00
|
|
|
isMissingSeat: obj.user.plan === UserPlan.FREE,
|
2021-12-09 23:51:30 +00:00
|
|
|
role: membership?.role,
|
2022-03-15 18:57:16 +00:00
|
|
|
accepted: membership?.accepted,
|
2022-07-21 17:02:20 +00:00
|
|
|
disableImpersonation: membership?.disableImpersonation,
|
2021-12-09 23:51:30 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return { ...team, members };
|
|
|
|
}
|
|
|
|
// also returns team
|
|
|
|
export async function isTeamAdmin(userId: number, teamId: number) {
|
|
|
|
return (
|
|
|
|
(await prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
|
|
|
|
},
|
|
|
|
})) || false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
export async function isTeamOwner(userId: number, teamId: number) {
|
|
|
|
return !!(await prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
role: "OWNER",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
export async function isTeamMember(userId: number, teamId: number) {
|
|
|
|
return !!(await prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|