cal.pub0.org/pages/api/availability/_get.ts

51 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-07-05 18:12:14 +00:00
import { HttpError } from "@/../../packages/lib/http-error";
2022-06-14 21:17:09 +00:00
import type { NextApiRequest } from "next";
import { z } from "zod";
import { getUserAvailability } from "@calcom/core/getUserAvailability";
import { defaultResponder } from "@calcom/lib/server";
import { stringOrNumber } from "@calcom/prisma/zod-utils";
const availabilitySchema = z
.object({
2022-06-16 19:50:41 +00:00
userId: stringOrNumber.optional(),
2022-07-05 18:12:14 +00:00
teamId: stringOrNumber.optional(),
2022-06-14 21:17:09 +00:00
username: z.string().optional(),
dateFrom: z.string(),
dateTo: z.string(),
eventTypeId: stringOrNumber.optional(),
})
2022-07-05 18:12:14 +00:00
.refine(
(data) => !!data.username || !!data.userId || !!data.teamId,
"Either username or userId or teamId should be filled in."
);
2022-06-14 21:17:09 +00:00
async function handler(req: NextApiRequest) {
2022-07-05 18:12:14 +00:00
const { prisma, isAdmin } = req;
const { username, userId, eventTypeId, dateTo, dateFrom, teamId } = availabilitySchema.parse(req.query);
if (!teamId)
return getUserAvailability({
username,
dateFrom,
dateTo,
eventTypeId,
userId,
});
const team = await prisma.team.findUnique({ where: { id: teamId }, select: { members: { select: availabilityUserSelect } } });
2022-07-05 18:12:14 +00:00
if (!team) throw new HttpError({ statusCode: 404, message: "teamId not found" });
if (!team.members) throw new HttpError({ statusCode: 404, message: "teamId not found" });
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "Unauthorized" });
return team.members.map(
async (user) =>
await getUserAvailability({
username,
dateFrom,
dateTo,
eventTypeId,
userId: user.userId,
}, {initialData: { user }})
2022-07-05 18:12:14 +00:00
);
2022-06-14 21:17:09 +00:00
}
export default defaultResponder(handler);