2022-04-04 00:02:11 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { BookingResponse } from "@lib/types";
|
2022-04-27 17:25:36 +00:00
|
|
|
import { schemaBookingEditBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
|
2022-04-04 00:02:11 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
|
2022-04-29 15:29:57 +00:00
|
|
|
export async function bookingById(
|
|
|
|
{ method, query, body, userId }: NextApiRequest,
|
|
|
|
res: NextApiResponse<BookingResponse>
|
|
|
|
) {
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
2022-05-18 12:27:30 +00:00
|
|
|
if (!safeQuery.success) {
|
|
|
|
res.status(400).json({ message: "Your query was invalid" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 14:47:01 +00:00
|
|
|
const userWithBookings = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: { bookings: true },
|
|
|
|
});
|
|
|
|
if (!userWithBookings) throw new Error("User not found");
|
2022-04-29 15:29:57 +00:00
|
|
|
const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat();
|
2022-04-23 00:17:06 +00:00
|
|
|
if (!userBookingIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
|
|
|
|
else {
|
2022-04-11 14:47:01 +00:00
|
|
|
switch (method) {
|
2022-04-29 00:49:38 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings/{id}:
|
|
|
|
* get:
|
2022-04-29 15:29:57 +00:00
|
|
|
* summary: Find a booking
|
2022-05-10 17:52:59 +00:00
|
|
|
* operationId: getBookingById
|
2022-04-29 00:49:38 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the booking to get
|
2022-04-29 00:49:38 +00:00
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: Booking was not found
|
|
|
|
*/
|
2022-04-11 14:47:01 +00:00
|
|
|
case "GET":
|
|
|
|
await prisma.booking
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
2022-04-27 17:25:36 +00:00
|
|
|
.then((data) => schemaBookingReadPublic.parse(data))
|
2022-04-11 14:47:01 +00:00
|
|
|
.then((booking) => res.status(200).json({ booking }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Booking with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 00:49:38 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings/{id}:
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing booking
|
2022-05-10 17:52:59 +00:00
|
|
|
* operationId: editBookingById
|
|
|
|
* requestBody:
|
|
|
|
* description: Edit an existing booking related to one of your event-types
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* title:
|
|
|
|
* type: string
|
|
|
|
* example: 15min
|
|
|
|
* startTime:
|
|
|
|
* type: string
|
|
|
|
* example: 1970-01-01T17:00:00.000Z
|
|
|
|
* endTime:
|
|
|
|
* type: string
|
|
|
|
* example: 1970-01-01T17:00:00.000Z
|
2022-04-29 00:49:38 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the booking to edit
|
2022-04-29 00:49:38 +00:00
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, booking edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Booking body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-11 14:47:01 +00:00
|
|
|
case "PATCH":
|
2022-04-27 17:25:36 +00:00
|
|
|
const safeBody = schemaBookingEditBodyParams.safeParse(body);
|
2022-04-11 14:47:01 +00:00
|
|
|
if (!safeBody.success) {
|
2022-05-10 17:52:59 +00:00
|
|
|
console.log(safeBody.error);
|
|
|
|
res.status(400).json({ message: "Bad request", error: safeBody.error });
|
|
|
|
return;
|
2022-04-11 14:47:01 +00:00
|
|
|
}
|
|
|
|
await prisma.booking
|
|
|
|
.update({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
})
|
2022-05-10 17:52:59 +00:00
|
|
|
// .then((data) => schemaBookingReadPublic.parse(data))
|
2022-04-11 14:47:01 +00:00
|
|
|
.then((booking) => res.status(200).json({ booking }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Booking with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 00:49:38 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings/{id}:
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing booking
|
2022-05-10 17:52:59 +00:00
|
|
|
* operationId: removeBookingById
|
2022-04-29 00:49:38 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the booking to delete
|
2022-04-29 00:49:38 +00:00
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, booking removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Booking id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-11 14:47:01 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.booking
|
|
|
|
.delete({ where: { id: safeQuery.data.id } })
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({
|
|
|
|
message: `Booking with id: ${safeQuery.data.id} deleted successfully`,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Booking with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-11 14:47:01 +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(bookingById));
|