From debc8dbafb3d8dfc02b08fdbc9a6fabfb78be14b Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Thu, 20 Oct 2022 20:49:57 -0300 Subject: [PATCH] Using abstracted booking cancellation (#191) Implemented `DELETE /booking/:uid` as well as `DELETE /booking/:uid/cancel` based on abstracted cancellation logic from webapp. PR dependant on https://github.com/calcom/cal.com/pull/5105 Co-authored-by: Alex van Andel --- pages/api/bookings/[id]/_delete.ts | 53 +++++++++++++++++++++--------- pages/api/bookings/[id]/cancel.ts | 14 ++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 pages/api/bookings/[id]/cancel.ts diff --git a/pages/api/bookings/[id]/_delete.ts b/pages/api/bookings/[id]/_delete.ts index 46014f5785..d03f1c7abd 100644 --- a/pages/api/bookings/[id]/_delete.ts +++ b/pages/api/bookings/[id]/_delete.ts @@ -1,37 +1,58 @@ import type { NextApiRequest } from "next"; +import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking"; import { defaultResponder } from "@calcom/lib/server"; +import { schemaBookingCancelParams } from "@calcom/prisma/zod-utils"; import { schemaQueryIdParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; /** * @swagger - * /bookings/{id}: + * /bookings/{id}/cancel: * delete: - * summary: Remove an existing booking - * operationId: removeBookingById + * summary: Booking cancellation + * operationId: cancelBookingById + * requestBody: + * content: + * application/json: + * schema: + * type: object + * properties: + * allRemainingBookings: + * type: boolean + * reason: + * type: string * parameters: * - in: path * name: id * schema: * type: integer * required: true - * description: ID of the booking to delete + * description: ID of the booking to cancel * tags: - * - bookings + * - bookings * responses: - * 201: - * description: OK, booking removed successfuly + * 200: + * description: OK, booking cancelled successfuly * 400: - * description: Bad request. Booking id is invalid. - * 401: - * description: Authorization information is missing or invalid. + * description: | + * Message | Cause + * :--|:-- + * Booking not found| The provided id didn't correspond to any existing booking. + * Cannot cancel past events| The provided id matched an existing booking with a past startDate. + * User not found| The userId did not matched an existing user. + * 404: + * description: User not found */ -export async function deleteHandler(req: NextApiRequest) { - const { prisma, query } = req; - const { id } = schemaQueryIdParseInt.parse(query); - await prisma.booking.delete({ where: { id } }); - return { message: `Booking with id: ${id} deleted successfully` }; +async function handler(req: NextApiRequest) { + const { id } = schemaQueryIdParseInt.parse(req.query); + const { allRemainingBookings, cancellationReason } = schemaBookingCancelParams.parse({ + ...req.body, + cancellationReason: req.body.reason, + }); + // Normalizing for universal handler + req.body = { id, allRemainingBookings, cancellationReason }; + return await handleCancelBooking(req); } -export default defaultResponder(deleteHandler); +export default defaultResponder(handler); diff --git a/pages/api/bookings/[id]/cancel.ts b/pages/api/bookings/[id]/cancel.ts new file mode 100644 index 0000000000..d450678e85 --- /dev/null +++ b/pages/api/bookings/[id]/cancel.ts @@ -0,0 +1,14 @@ +import { NextApiRequest, NextApiResponse } from "next"; + +import { defaultHandler } from "@calcom/lib/server"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; + +import authMiddleware from "./_auth-middleware"; + +export default withMiddleware()(async (req: NextApiRequest, res: NextApiResponse) => { + await authMiddleware(req); + return defaultHandler({ + DELETE: import("./_delete"), + })(req, res); +});