2022-04-04 00:02:11 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { withMiddleware } from "~/lib/helpers/withMiddleware";
|
|
|
|
import type { DestinationCalendarResponse } from "~/lib/types";
|
2022-04-04 00:02:11 +00:00
|
|
|
import {
|
2022-04-27 17:25:36 +00:00
|
|
|
schemaDestinationCalendarEditBodyParams,
|
|
|
|
schemaDestinationCalendarReadPublic,
|
2022-11-25 13:56:58 +00:00
|
|
|
} from "~/lib/validations/destination-calendar";
|
2022-04-04 00:02:11 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
2022-11-25 13:56:58 +00:00
|
|
|
} from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-04-04 00:02:11 +00:00
|
|
|
|
|
|
|
export async function destionationCalendarById(
|
2022-06-06 16:17:10 +00:00
|
|
|
{ method, query, body, userId, prisma }: NextApiRequest,
|
2022-04-04 00:02:11 +00:00
|
|
|
res: NextApiResponse<DestinationCalendarResponse>
|
|
|
|
) {
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
2022-04-27 17:25:36 +00:00
|
|
|
const safeBody = schemaDestinationCalendarEditBodyParams.safeParse(body);
|
2022-05-18 12:27:30 +00:00
|
|
|
if (!safeQuery.success) {
|
|
|
|
res.status(400).json({ message: "Your query was invalid" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 23:15:58 +00:00
|
|
|
const data = await prisma.destinationCalendar.findMany({ where: { userId } });
|
|
|
|
const userDestinationCalendars = data.map((destinationCalendar) => destinationCalendar.id);
|
|
|
|
// FIXME: Should we also check ownership of bokingId and eventTypeId to avoid users cross-pollinating other users calendars.
|
|
|
|
// On a related note, moving from sequential integer IDs to UUIDs would be a good idea. and maybe help avoid having this problem.
|
2022-04-23 00:17:06 +00:00
|
|
|
if (userDestinationCalendars.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
|
|
|
|
else {
|
2022-04-20 23:15:58 +00:00
|
|
|
switch (method) {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /destination-calendars/{id}:
|
|
|
|
* get:
|
|
|
|
* summary: Find a destination calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the destination calendar to get
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: DestinationCalendar was not found
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing destination calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the destination calendar to edit
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destinationCalendar edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing destination calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the destination calendar to delete
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destinationCalendar removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-20 23:15:58 +00:00
|
|
|
case "GET":
|
|
|
|
await prisma.destinationCalendar
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
2022-04-27 17:25:36 +00:00
|
|
|
.then((data) => schemaDestinationCalendarReadPublic.parse(data))
|
2022-04-20 23:15:58 +00:00
|
|
|
.then((destination_calendar) => res.status(200).json({ destination_calendar }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `DestinationCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /destination-calendars/{id}:
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing destination calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the destination calendar to edit
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destinationCalendar edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-20 23:15:58 +00:00
|
|
|
case "PATCH":
|
|
|
|
if (!safeBody.success) {
|
2022-05-17 17:33:18 +00:00
|
|
|
{
|
|
|
|
res.status(400).json({ message: "Invalid request body" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 23:15:58 +00:00
|
|
|
}
|
|
|
|
await prisma.destinationCalendar
|
|
|
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
2022-04-27 17:25:36 +00:00
|
|
|
.then((data) => schemaDestinationCalendarReadPublic.parse(data))
|
2022-04-20 23:15:58 +00:00
|
|
|
.then((destination_calendar) => res.status(200).json({ destination_calendar }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `DestinationCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /destination-calendars/{id}:
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing destination calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the destination calendar to delete
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - destination-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, destinationCalendar removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. DestinationCalendar id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-20 23:15:58 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.destinationCalendar
|
|
|
|
.delete({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
})
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({
|
|
|
|
message: `DestinationCalendar with id: ${safeQuery.data.id} deleted`,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `DestinationCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-20 23:15:58 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
2022-04-23 00:17:06 +00:00
|
|
|
}
|
2022-04-04 00:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(
|
|
|
|
withValidQueryIdTransformParseInt(destionationCalendarById)
|
|
|
|
);
|