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-30 14:56:24 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { SelectedCalendarResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaSelectedCalendarBodyParams,
|
|
|
|
schemaSelectedCalendarPublic,
|
|
|
|
withValidSelectedCalendar,
|
|
|
|
} from "@lib/validations/selected-calendar";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-03 15:47:18 +00:00
|
|
|
* /api/selected-calendars/new:
|
2022-03-30 14:56:24 +00:00
|
|
|
* post:
|
2022-03-31 20:14:37 +00:00
|
|
|
* summary: Creates a new selected calendar
|
2022-04-03 15:47:18 +00:00
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selected calendar created
|
|
|
|
* model: SelectedCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
async function createSelectedCalendar(req: NextApiRequest, res: NextApiResponse<SelectedCalendarResponse>) {
|
|
|
|
const safe = schemaSelectedCalendarBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data });
|
|
|
|
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
|
|
|
|
|
|
|
|
if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new selectedCalendar",
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
export default withMiddleware("HTTP_POST")(withValidSelectedCalendar(createSelectedCalendar));
|