cal.pub0.org/pages/api/schedules/index.ts

129 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-03-30 12:17:55 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
import safeParseJSON from "@lib/helpers/safeParseJSON";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
2022-10-06 19:06:07 +00:00
import {
schemaScheduleBodyParams,
schemaSchedulePublic,
schemaCreateScheduleBodyParams,
} from "@lib/validations/schedule";
async function createOrlistAllSchedules(
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) {
body = safeParseJSON(body);
if (body.success !== undefined && !body.success) {
res.status(400).json({ message: body.message });
2022-10-07 07:54:55 +00:00
return;
}
const safe = schemaScheduleBodyParams.safeParse(body);
2022-10-06 18:41:50 +00:00
2022-10-06 19:06:07 +00:00
if (!safe.success) {
res.status(400).json({ message: "Bad request" });
return;
}
const safeBody = safe.data;
if (safeBody.userId && !isAdmin) {
2022-10-05 14:05:04 +00:00
res.status(401).json({ message: "Unauthorized" });
2022-10-05 20:02:34 +00:00
return;
2022-10-05 14:05:04 +00:00
} else {
if (method === "GET") {
/**
* @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-06 18:41:50 +00:00
2022-10-06 19:06:07 +00:00
const userIds = Array.isArray(safeBody.userId) ? safeBody.userId : [safeBody.userId || userId];
2022-10-06 18:41:50 +00:00
2022-10-05 14:05:04 +00:00
const data = await prisma.schedule.findMany({
where: {
2022-10-06 18:41:50 +00:00
userId: { in: userIds },
},
2022-10-06 19:06:07 +00:00
include: { availability: true },
...(Array.isArray(body.userId) && { orderBy: { userId: "asc" } }),
2022-10-05 14:05:04 +00:00
});
const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule));
if (schedules) res.status(200).json({ schedules });
else
(error: Error) =>
res.status(404).json({
message: "No Schedules were found",
error,
});
} else if (method === "POST") {
/**
* @swagger
* /schedules:
* post:
* operationId: addSchedule
* summary: Creates a new schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule created
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
2022-10-06 19:06:07 +00:00
const safe = schemaCreateScheduleBodyParams.safeParse(body);
2022-10-05 20:02:34 +00:00
if (body.userId && !isAdmin) {
res.status(401).json({ message: "Unauthorized" });
return;
}
2022-10-05 14:05:04 +00:00
if (!safe.success) {
res.status(400).json({ message: "Invalid request body" });
return;
}
2022-10-06 19:06:07 +00:00
2022-10-05 14:05:04 +00:00
const data = await prisma.schedule.create({
data: {
...safe.data,
2022-10-06 19:06:07 +00:00
userId: safe.data.userId || userId,
2022-10-05 14:05:04 +00:00
availability: {
createMany: {
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE).map((schedule) => ({
days: schedule.days,
startTime: schedule.startTime,
endTime: schedule.endTime,
})),
},
},
},
2022-10-05 14:05:04 +00:00
});
const schedule = schemaSchedulePublic.parse(data);
2022-10-05 14:05:04 +00:00
if (schedule) res.status(201).json({ schedule, message: "Schedule created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new schedule",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
}
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllSchedules);