feat: teamId availability in API
This commit is contained in:
1 parent
be2647790c
commit
a63b623a7d
7 files changed
+63
-12
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { HttpError } from "@/../../packages/lib/http-error";
|
||||
import type { NextApiRequest } from "next";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -8,22 +9,42 @@ import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
||||
const availabilitySchema = z
|
||||
.object({
|
||||
userId: stringOrNumber.optional(),
|
||||
teamId: stringOrNumber.optional(),
|
||||
username: z.string().optional(),
|
||||
dateFrom: z.string(),
|
||||
dateTo: z.string(),
|
||||
eventTypeId: stringOrNumber.optional(),
|
||||
})
|
||||
.refine((data) => !!data.username || !!data.userId, "Either username or userId should be filled in.");
|
||||
.refine(
|
||||
(data) => !!data.username || !!data.userId || !!data.teamId,
|
||||
"Either username or userId or teamId should be filled in."
|
||||
);
|
||||
|
||||
async function handler(req: NextApiRequest) {
|
||||
const { username, userId, eventTypeId, dateTo, dateFrom } = availabilitySchema.parse(req.query);
|
||||
return getUserAvailability({
|
||||
username,
|
||||
dateFrom,
|
||||
dateTo,
|
||||
eventTypeId,
|
||||
userId,
|
||||
});
|
||||
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: true } });
|
||||
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,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export default defaultResponder(handler);
|
||||
Reference in new issue
Block a user