2022-04-11 13:42:50 +00:00
|
|
|
import { BookingModel } from "@/../../packages/prisma/zod";
|
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 { BookingReferenceResponse } from "@lib/types";
|
|
|
|
import {
|
2022-04-26 20:48:15 +00:00
|
|
|
schemaBookingEditBodyParams,
|
|
|
|
schemaBookingReferenceReadPublic,
|
2022-04-04 00:02:11 +00:00
|
|
|
} from "@lib/validations/booking-reference";
|
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-26 19:56:59 +00:00
|
|
|
* /booking-references/{id}:
|
2022-04-04 00:02:11 +00:00
|
|
|
* get:
|
2022-04-27 17:25:36 +00:00
|
|
|
* summary: Find a booking reference by ID
|
2022-04-04 00:02:11 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-26 20:48:15 +00:00
|
|
|
* description: Numeric ID of the booking reference to get
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
|
|
|
* - booking-references
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: BookingReference was not found
|
|
|
|
* patch:
|
2022-04-26 20:48:15 +00:00
|
|
|
* summary: Edit an existing booking reference
|
2022-04-04 00:02:11 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-26 20:48:15 +00:00
|
|
|
* description: Numeric ID of the booking reference to edit
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
|
|
|
* - booking-references
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, bookingReference edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. BookingReference body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* delete:
|
2022-04-26 20:48:15 +00:00
|
|
|
* summary: Remove an existing booking reference
|
2022-04-04 00:02:11 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-26 20:48:15 +00:00
|
|
|
* description: Numeric ID of the booking reference to delete
|
2022-04-28 23:38:40 +00:00
|
|
|
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
|
|
|
* - booking-references
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, bookingReference removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. BookingReference id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function bookingReferenceById(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<BookingReferenceResponse>
|
|
|
|
) {
|
|
|
|
const { method, query, body } = req;
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
2022-04-04 00:02:11 +00:00
|
|
|
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
2022-04-22 01:36:33 +00:00
|
|
|
const userId = req.userId;
|
2022-04-11 13:42:50 +00:00
|
|
|
const userWithBookings = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: { bookings: true },
|
|
|
|
});
|
|
|
|
if (!userWithBookings) throw new Error("User not found");
|
|
|
|
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
|
|
|
const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safeQuery.data.id } });
|
|
|
|
if (!bookingReference) throw new Error("BookingReference not found");
|
|
|
|
if (userBookingIds.includes(bookingReference.bookingId)) {
|
|
|
|
switch (method) {
|
|
|
|
case "GET":
|
|
|
|
await prisma.bookingReference
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
2022-04-26 20:48:15 +00:00
|
|
|
.then((data) => schemaBookingReferenceReadPublic.parse(data))
|
2022-04-11 13:42:50 +00:00
|
|
|
.then((booking_reference) => res.status(200).json({ booking_reference }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-11 13:42:50 +00:00
|
|
|
case "PATCH":
|
2022-04-26 20:48:15 +00:00
|
|
|
const safeBody = schemaBookingEditBodyParams.safeParse(body);
|
|
|
|
|
2022-04-11 13:42:50 +00:00
|
|
|
if (!safeBody.success) {
|
|
|
|
throw new Error("Invalid request body");
|
|
|
|
}
|
|
|
|
await prisma.bookingReference
|
|
|
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
2022-04-26 20:48:15 +00:00
|
|
|
.then((data) => schemaBookingReferenceReadPublic.parse(data))
|
2022-04-11 13:42:50 +00:00
|
|
|
.then((booking_reference) => res.status(200).json({ booking_reference }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-11 13:42:50 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.bookingReference
|
|
|
|
.delete({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
})
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({
|
|
|
|
message: `BookingReference with id: ${safeQuery.data.id} deleted`,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `BookingReference with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-11 13:42:50 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else res.status(401).json({ message: "Unauthorized" });
|
2022-04-04 00:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(
|
|
|
|
withValidQueryIdTransformParseInt(bookingReferenceById)
|
|
|
|
);
|