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 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
* 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);

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