2022-10-13 20:54:38 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
2022-10-11 14:33:25 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaSchedulePublic } from "~/lib/validations/schedule";
|
|
|
|
import { schemaQuerySingleOrMultipleUserIds } from "~/lib/validations/shared/queryUserId";
|
2022-10-11 14:33:25 +00:00
|
|
|
|
|
|
|
export const schemaUserIds = z
|
|
|
|
.union([z.string(), z.array(z.string())])
|
|
|
|
.transform((val) => (Array.isArray(val) ? val.map((v) => parseInt(v, 10)) : [parseInt(val, 10)]));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /schedules:
|
|
|
|
* get:
|
|
|
|
* operationId: listSchedules
|
|
|
|
* summary: Find all schedules
|
|
|
|
* tags:
|
|
|
|
* - schedules
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No schedules were found
|
|
|
|
*/
|
2022-10-13 20:54:38 +00:00
|
|
|
async function handler(req: NextApiRequest) {
|
|
|
|
const { prisma, userId, isAdmin } = req;
|
|
|
|
const args: Prisma.ScheduleFindManyArgs = isAdmin ? {} : { where: { userId } };
|
|
|
|
args.include = { availability: true };
|
|
|
|
|
|
|
|
if (!isAdmin && req.query.userId)
|
|
|
|
throw new HttpError({
|
|
|
|
statusCode: 401,
|
|
|
|
message: "Unauthorized: Only admins can query other users",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isAdmin && req.query.userId) {
|
|
|
|
const query = schemaQuerySingleOrMultipleUserIds.parse(req.query);
|
|
|
|
const userIds = Array.isArray(query.userId) ? query.userId : [query.userId || userId];
|
|
|
|
args.where = { userId: { in: userIds } };
|
|
|
|
if (Array.isArray(query.userId)) args.orderBy = { userId: "asc" };
|
2022-10-11 14:33:25 +00:00
|
|
|
}
|
2022-10-13 20:54:38 +00:00
|
|
|
const data = await prisma.schedule.findMany(args);
|
|
|
|
return { schedules: data.map((s) => schemaSchedulePublic.parse(s)) };
|
2022-10-11 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(handler);
|