cal.pub0.org/pages/api/destination-calendars/[id].ts

241 lines
8.6 KiB
TypeScript
Raw Normal View History

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 {
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>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
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.
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
* description: ID of the destination calendar to get
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
2022-04-29 15:29:57 +00:00
* tags:
* - destination-calendars
2022-04-29 15:29:57 +00:00
* 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
* description: ID of the destination calendar to edit
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
* requestBody:
* description: Create a new booking related to one of your event-types
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* integration:
* type: string
* description: 'The integration'
* externalId:
* type: string
* description: 'The external ID of the integration'
* eventTypeId:
* type: integer
* description: 'The ID of the eventType it is associated with'
* bookingId:
* type: integer
* description: 'The booking ID it is associated with'
2022-04-29 15:29:57 +00:00
* tags:
* - destination-calendars
2022-04-29 15:29:57 +00:00
* 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
* description: ID of the destination calendar to delete
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
2022-04-29 15:29:57 +00:00
* tags:
* - destination-calendars
2022-04-29 15:29:57 +00:00
* 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 } })
.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
* description: ID of the destination calendar to edit
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
2022-04-29 15:29:57 +00:00
* tags:
* - destination-calendars
2022-04-29 15:29:57 +00:00
* 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 })
.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
* description: ID of the destination calendar to delete
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
2022-04-29 15:29:57 +00:00
* tags:
* - destination-calendars
2022-04-29 15:29:57 +00:00
* 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-04 00:02:11 +00:00
}
export default withMiddleware("HTTP_GET_DELETE_PATCH")(
withValidQueryIdTransformParseInt(destionationCalendarById)
);