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 14:56:24 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-08 23:29:26 +00:00
|
|
|
import { SelectedCalendarResponse, SelectedCalendarsResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaSelectedCalendarBodyParams,
|
|
|
|
schemaSelectedCalendarPublic,
|
|
|
|
} from "@lib/validations/selected-calendar";
|
2022-03-30 14:56:24 +00:00
|
|
|
|
2022-04-08 23:29:26 +00:00
|
|
|
async function createOrlistAllSelectedCalendars(
|
2022-06-06 16:17:10 +00:00
|
|
|
{ method, body, userId, prisma }: NextApiRequest,
|
2022-04-08 23:29:26 +00:00
|
|
|
res: NextApiResponse<SelectedCalendarsResponse | SelectedCalendarResponse>
|
|
|
|
) {
|
|
|
|
if (method === "GET") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /selected-calendars:
|
|
|
|
* get:
|
2022-05-05 16:18:00 +00:00
|
|
|
* operationId: listSelectedCalendars
|
2022-04-29 15:29:57 +00:00
|
|
|
* summary: Find all selected calendars
|
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No selected calendars were found
|
|
|
|
*/
|
2022-04-20 22:31:42 +00:00
|
|
|
const data = await prisma.selectedCalendar.findMany({ where: { userId } });
|
2022-04-11 10:03:15 +00:00
|
|
|
const selected_calendars = data.map((selected_calendar) =>
|
|
|
|
schemaSelectedCalendarPublic.parse(selected_calendar)
|
2022-04-08 23:29:26 +00:00
|
|
|
);
|
2022-04-11 10:03:15 +00:00
|
|
|
if (selected_calendars) res.status(200).json({ selected_calendars });
|
2022-04-08 23:29:26 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No SelectedCalendars were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /selected-calendars:
|
|
|
|
* get:
|
2022-05-05 16:18:00 +00:00
|
|
|
* operationId: addSelectedCalendars
|
2022-04-29 15:29:57 +00:00
|
|
|
* summary: Find all selected calendars
|
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No selected calendars were found
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new selected calendar
|
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selected calendar created
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-30 18:53:19 +00:00
|
|
|
const safe = schemaSelectedCalendarBodyParams.safeParse(body);
|
2022-05-17 17:33:18 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
res.status(400).json({ message: "Invalid request body" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 22:31:42 +00:00
|
|
|
// Create new selectedCalendar connecting it to current userId
|
|
|
|
const data = await prisma.selectedCalendar.create({
|
|
|
|
data: { ...safe.data, user: { connect: { id: userId } } },
|
|
|
|
});
|
2022-04-11 10:03:15 +00:00
|
|
|
const selected_calendar = schemaSelectedCalendarPublic.parse(data);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
if (selected_calendar)
|
|
|
|
res.status(201).json({ selected_calendar, message: "SelectedCalendar created successfully" });
|
2022-04-08 23:29:26 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
2022-04-11 10:03:15 +00:00
|
|
|
message: "Could not create new selected calendar",
|
2022-04-08 23:29:26 +00:00
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
2022-03-30 14:56:24 +00:00
|
|
|
|
2022-04-20 22:31:42 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllSelectedCalendars);
|