fix: add teams memberships users

pull/9078/head
Agusti Fernandez Pardo 2022-07-05 20:58:26 +02:00
parent 2263e04217
commit f1ee03f297
1 changed files with 22 additions and 10 deletions

View File

@ -1,11 +1,14 @@
import { HttpError } from "@/../../packages/lib/http-error";
import type { NextApiRequest } from "next"; import type { NextApiRequest } from "next";
import { z } from "zod"; import { z } from "zod";
import { getUserAvailability } from "@calcom/core/getUserAvailability"; import { getUserAvailability } from "@calcom/core/getUserAvailability";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server"; import { defaultResponder } from "@calcom/lib/server";
import { availabilityUserSelect } from "@calcom/prisma";
import { stringOrNumber } from "@calcom/prisma/zod-utils"; import { stringOrNumber } from "@calcom/prisma/zod-utils";
import { User } from ".prisma/client";
const availabilitySchema = z const availabilitySchema = z
.object({ .object({
userId: stringOrNumber.optional(), userId: stringOrNumber.optional(),
@ -31,19 +34,28 @@ async function handler(req: NextApiRequest) {
eventTypeId, eventTypeId,
userId, userId,
}); });
const team = await prisma.team.findUnique({ where: { id: teamId }, select: { members: { select: availabilityUserSelect } } }); const team = await prisma.team.findUnique({
where: { id: teamId },
select: { members: true },
});
if (!team) throw new HttpError({ statusCode: 404, message: "teamId not found" }); if (!team) throw new HttpError({ statusCode: 404, message: "teamId not found" });
if (!team.members) throw new HttpError({ statusCode: 404, message: "teamId not found" }); if (!team.members) throw new HttpError({ statusCode: 404, message: "teamId not found" });
const allMemberIds = team.members.map((membership) => membership.userId);
const members = await prisma.user.findMany({
where: { id: { in: allMemberIds } },
select: availabilityUserSelect,
});
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "Unauthorized" }); if (!isAdmin) throw new HttpError({ statusCode: 401, message: "Unauthorized" });
return team.members.map( return members.map(
async (user) => async (user) =>
await getUserAvailability({ await getUserAvailability(
username, {
dateFrom, dateFrom,
dateTo, dateTo,
eventTypeId, eventTypeId,
userId: user.userId, },
}, {initialData: { user }}) { user }
)
); );
} }