import { Prisma, UserPlan } from "@prisma/client"; import prisma, { baseEventTypeSelect } from "@calcom/prisma"; import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils"; import { WEBAPP_URL } from "../../../constants"; export type TeamWithMembers = Awaited>; export async function getTeamWithMembers(id?: number, slug?: string, userId?: number) { const userSelect = Prisma.validator()({ username: true, email: true, name: true, id: true, plan: true, bio: true, }); const teamSelect = Prisma.validator()({ id: true, name: true, slug: true, logo: true, bio: true, hideBranding: true, metadata: true, members: { select: { accepted: true, role: true, disableImpersonation: true, user: { select: userSelect, }, }, }, eventTypes: { where: { hidden: false, }, select: { users: { select: userSelect, }, metadata: true, ...baseEventTypeSelect, }, }, }); const where: Prisma.TeamFindFirstArgs["where"] = {}; if (userId) where.members = { some: { userId } }; if (id) where.id = id; if (slug) where.slug = slug; const team = await prisma.team.findFirst({ where, select: teamSelect, }); if (!team) return null; const members = team.members.map((obj) => { return { ...obj.user, isMissingSeat: obj.user.plan === UserPlan.FREE, role: obj.role, accepted: obj.accepted, disableImpersonation: obj.disableImpersonation, avatar: `${WEBAPP_URL}/${obj.user.username}/avatar.png`, }; }); const eventTypes = team.eventTypes.map((eventType) => ({ ...eventType, metadata: EventTypeMetaDataSchema.parse(eventType.metadata), })); return { ...team, eventTypes, 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", }, })); } export async function isTeamMember(userId: number, teamId: number) { return !!(await prisma.membership.findFirst({ where: { userId, teamId, }, })); }