Skip to content

Scheduling infrastructure for absolutely everyone.

Using abstracted booking cancellation (#191)

Leo Giovanettidebc8dbafb3d
diff --git a/pages/api/bookings/[id]/_delete.ts b/pages/api/bookings/[id]/_delete.ts
index 46014f5785b..d03f1c7abd4 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 00000000000..d450678e855
--- /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);
+});