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-04-01 23:55:41 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { DestinationCalendarResponse } from "@lib/types";
|
2022-03-30 12:17:55 +00:00
|
|
|
import {
|
2022-04-01 23:55:41 +00:00
|
|
|
schemaDestinationCalendarBodyParams,
|
|
|
|
schemaDestinationCalendarPublic,
|
2022-03-30 12:17:55 +00:00
|
|
|
withValidDestinationCalendar,
|
|
|
|
} from "@lib/validations/destination-calendar";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /api/destination-calendars/new:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new destinationCalendar
|
2022-04-03 15:47:18 +00:00
|
|
|
* requestBody:
|
|
|
|
* description: Optional description in *Markdown*
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
2022-04-01 23:55:41 +00:00
|
|
|
* schema:
|
|
|
|
* $ref: '#/components/schemas/DestinationCalendar'
|
|
|
|
* tags:
|
2022-04-03 15:47:18 +00:00
|
|
|
* - destination-calendars
|
2022-04-01 23:55:41 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destinationCalendar created
|
|
|
|
* model: DestinationCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
async function createDestinationCalendar(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<DestinationCalendarResponse>
|
|
|
|
) {
|
|
|
|
const safe = schemaDestinationCalendarBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
const destinationCalendar = await prisma.destinationCalendar.create({ data: safe.data });
|
|
|
|
const data = schemaDestinationCalendarPublic.parse(destinationCalendar);
|
|
|
|
|
|
|
|
if (data) res.status(201).json({ data, message: "DestinationCalendar created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new destinationCalendar",
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
export default withMiddleware("HTTP_POST")(withValidDestinationCalendar(createDestinationCalendar));
|