2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
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";
|
|
|
|
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
|
2022-03-31 20:14:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-11 10:03:15 +00:00
|
|
|
* /v1/schedules:
|
2022-03-31 20:14:37 +00:00
|
|
|
* get:
|
2022-04-10 00:10:34 +00:00
|
|
|
* summary: Get all schedules
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-03-31 20:14:37 +00:00
|
|
|
* tags:
|
|
|
|
* - schedules
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No schedules were found
|
2022-04-10 00:10:34 +00:00
|
|
|
* post:
|
|
|
|
* summary: Creates a new schedule
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-10 00:10:34 +00:00
|
|
|
* tags:
|
|
|
|
* - schedules
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, schedule created
|
|
|
|
* model: Schedule
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Schedule body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
2022-03-31 20:14:37 +00:00
|
|
|
*/
|
2022-04-10 00:10:34 +00:00
|
|
|
async function createOrlistAllSchedules(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
|
|
|
) {
|
|
|
|
const { method } = req;
|
|
|
|
if (method === "GET") {
|
2022-04-11 13:10:16 +00:00
|
|
|
const data = await prisma.schedule.findMany();
|
|
|
|
const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule));
|
|
|
|
if (schedules) res.status(200).json({ schedules });
|
2022-04-10 00:10:34 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No Schedules were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
|
|
|
const safe = schemaScheduleBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body");
|
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
const data = await prisma.schedule.create({ data: safe.data });
|
|
|
|
const schedule = schemaSchedulePublic.parse(data);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
if (schedule) res.status(201).json({ schedule, message: "Schedule created successfully" });
|
2022-04-10 00:10:34 +00:00
|
|
|
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-10 00:10:34 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(withValidSchedule(createOrlistAllSchedules));
|