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 <me@alexvanandel.com>
pull/9078/head
Leo Giovanetti 2022-10-20 20:49:57 -03:00 committed by GitHub
parent f66ed50ecb
commit debc8dbafb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 16 deletions

View File

@ -1,37 +1,58 @@
import type { NextApiRequest } from "next"; import type { NextApiRequest } from "next";
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
import { defaultResponder } from "@calcom/lib/server"; import { defaultResponder } from "@calcom/lib/server";
import { schemaBookingCancelParams } from "@calcom/prisma/zod-utils";
import { schemaQueryIdParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
/** /**
* @swagger * @swagger
* /bookings/{id}: * /bookings/{id}/cancel:
* delete: * delete:
* summary: Remove an existing booking * summary: Booking cancellation
* operationId: removeBookingById * operationId: cancelBookingById
* requestBody:
* content:
* application/json:
* schema:
* type: object
* properties:
* allRemainingBookings:
* type: boolean
* reason:
* type: string
* parameters: * parameters:
* - in: path * - in: path
* name: id * name: id
* schema: * schema:
* type: integer * type: integer
* required: true * required: true
* description: ID of the booking to delete * description: ID of the booking to cancel
* tags: * tags:
* - bookings * - bookings
* responses: * responses:
* 201: * 200:
* description: OK, booking removed successfuly * description: OK, booking cancelled successfuly
* 400: * 400:
* description: Bad request. Booking id is invalid. * description: |
* 401: * Message | Cause
* description: Authorization information is missing or invalid. * :--|:--
* 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) { async function handler(req: NextApiRequest) {
const { prisma, query } = req; const { id } = schemaQueryIdParseInt.parse(req.query);
const { id } = schemaQueryIdParseInt.parse(query); const { allRemainingBookings, cancellationReason } = schemaBookingCancelParams.parse({
await prisma.booking.delete({ where: { id } }); ...req.body,
return { message: `Booking with id: ${id} deleted successfully` }; 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);

View File

@ -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);
});