diff --git a/packages/features/bookings/lib/getBookingToDelete.ts b/packages/features/bookings/lib/getBookingToDelete.ts index fd368e0cf314ab..2c9d743c6be9a9 100644 --- a/packages/features/bookings/lib/getBookingToDelete.ts +++ b/packages/features/bookings/lib/getBookingToDelete.ts @@ -1,8 +1,9 @@ +import { HttpError } from "@calcom/lib/http-error"; import prisma, { bookingMinimalSelect } from "@calcom/prisma"; import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; export async function getBookingToDelete(id: number | undefined, uid: string | undefined) { - return await prisma.booking.findUniqueOrThrow({ + const booking = await prisma.booking.findUnique({ where: { id, uid, @@ -111,6 +112,12 @@ export async function getBookingToDelete(id: number | undefined, uid: string | u status: true, }, }); + + if (!booking) { + throw new HttpError({ statusCode: 404, message: "Booking not found" }); + } + + return booking; } export type BookingToDelete = Awaited>; diff --git a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts index 3cd18c4a7c9dbd..91d4a8eaef173b 100644 --- a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts +++ b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts @@ -344,6 +344,22 @@ describe("Cancel Booking", () => { ).rejects.toThrow("Cannot cancel a booking that has already ended"); }); + test("Should throw HttpError 404 when booking does not exist", async () => { + const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking")).default; + + await expect( + handleCancelBooking({ + bookingData: { + uid: "non-existent-booking-uid", + cancelledBy: "organizer@example.com", + }, + }) + ).rejects.toMatchObject({ + statusCode: 404, + message: "Booking not found", + }); + }); + test("Should block canceling bookings without a cancellation reason when cancelledBy is set to the host", async () => { const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking")).default;