2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-10-05 13:56:27 +00:00
|
|
|
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
|
|
|
|
2022-10-07 07:16:35 +00:00
|
|
|
import safeParseJSON from "@lib/helpers/safeParseJSON";
|
2022-03-31 20:14:37 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-10 00:10:34 +00:00
|
|
|
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
|
2022-10-06 19:06:07 +00:00
|
|
|
import {
|
|
|
|
schemaScheduleBodyParams,
|
|
|
|
schemaSchedulePublic,
|
|
|
|
schemaCreateScheduleBodyParams,
|
|
|
|
} from "@lib/validations/schedule";
|
2022-03-31 20:14:37 +00:00
|
|
|
|
2022-04-10 00:10:34 +00:00
|
|
|
async function createOrlistAllSchedules(
|
2022-10-05 13:56:27 +00:00
|
|
|
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
|
2022-04-10 00:10:34 +00:00
|
|
|
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
|
|
|
) {
|
2022-10-07 07:16:35 +00:00
|
|
|
const safe = schemaScheduleBodyParams.safeParse(safeParseJSON(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({
|
2022-10-06 13:55:34 +00:00
|
|
|
where: {
|
2022-10-06 18:41:50 +00:00
|
|
|
userId: { in: userIds },
|
2022-10-06 13:55:34 +00:00
|
|
|
},
|
2022-10-06 19:06:07 +00:00
|
|
|
include: { availability: true },
|
2022-10-06 13:55:34 +00:00
|
|
|
...(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 13:56:27 +00:00
|
|
|
|
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 13:56:27 +00:00
|
|
|
},
|
|
|
|
},
|
2022-10-05 14:05:04 +00:00
|
|
|
});
|
|
|
|
const schedule = schemaSchedulePublic.parse(data);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
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` });
|
|
|
|
}
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
2022-03-31 20:14:37 +00:00
|
|
|
|
2022-04-24 21:57:17 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllSchedules);
|