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";
|
2022-04-11 13:10:16 +00:00
|
|
|
import { DestinationCalendarResponse, DestinationCalendarsResponse } from "@lib/types";
|
|
|
|
import {
|
2022-04-27 17:25:36 +00:00
|
|
|
schemaDestinationCalendarCreateBodyParams,
|
|
|
|
schemaDestinationCalendarReadPublic,
|
2022-04-11 13:10:16 +00:00
|
|
|
} from "@lib/validations/destination-calendar";
|
2022-04-01 23:55:41 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
async function createOrlistAllDestinationCalendars(
|
2022-04-29 15:29:57 +00:00
|
|
|
{ method, body, userId }: NextApiRequest,
|
2022-04-11 13:10:16 +00:00
|
|
|
res: NextApiResponse<DestinationCalendarsResponse | DestinationCalendarResponse>
|
2022-04-01 23:55:41 +00:00
|
|
|
) {
|
2022-04-11 13:10:16 +00:00
|
|
|
if (method === "GET") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /destination-calendars:
|
|
|
|
* get:
|
|
|
|
* summary: Find all destination calendars
|
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No destination calendars were found
|
|
|
|
*/
|
2022-04-20 23:15:58 +00:00
|
|
|
const data = await prisma.destinationCalendar.findMany({ where: { userId } });
|
2022-04-11 13:10:16 +00:00
|
|
|
const destination_calendars = data.map((destinationCalendar) =>
|
2022-04-27 17:25:36 +00:00
|
|
|
schemaDestinationCalendarReadPublic.parse(destinationCalendar)
|
2022-04-11 13:10:16 +00:00
|
|
|
);
|
|
|
|
if (data) res.status(200).json({ destination_calendars });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No DestinationCalendars were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /destination-calendars:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new destination calendar
|
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destination calendar created
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
const safe = schemaDestinationCalendarCreateBodyParams.safeParse(body);
|
2022-05-17 17:33:18 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
res.status(400).json({ message: "Invalid request body" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 13:10:16 +00:00
|
|
|
|
2022-04-20 23:15:58 +00:00
|
|
|
const data = await prisma.destinationCalendar.create({ data: { ...safe.data, userId } });
|
2022-04-27 17:25:36 +00:00
|
|
|
const destination_calendar = schemaDestinationCalendarReadPublic.parse(data);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
if (destination_calendar)
|
|
|
|
res.status(201).json({ destination_calendar, message: "DestinationCalendar created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new destinationCalendar",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
2022-04-01 23:55:41 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllDestinationCalendars);
|