Skip to content

Scheduling infrastructure for absolutely everyone.

fix: return 404 when booking does not exist (#29583)

Chayan Das53e32a5bbad4
diff --git a/packages/features/bookings/lib/handleCancelBooking.ts b/packages/features/bookings/lib/handleCancelBooking.ts
index 736ffdf56bc..c8f2df2e86f 100644
--- a/packages/features/bookings/lib/handleCancelBooking.ts
+++ b/packages/features/bookings/lib/handleCancelBooking.ts
@@ -49,6 +49,7 @@ import { getAllCredentialsIncludeServiceAccountKey } from "./getAllCredentialsFo
import { getBookingToDelete } from "./getBookingToDelete";
import cancelAttendeeSeat from "./handleSeats/cancel/cancelAttendeeSeat";
import type { IBookingCancelService } from "./interfaces/IBookingCancelService";
+import { isPrismaError } from "@calcom/lib/server/getServerErrorFromUnknown";
const log = logger.getSubLogger({ prefix: ["handleCancelBooking"] });
@@ -102,7 +103,19 @@ async function handler(input: CancelBookingInput, dependencies?: Dependencies) {
skipCancellationReasonValidation = false,
skipCalendarSyncTaskCancellation = false,
} = bookingCancelInput.parse(body);
- const bookingToDelete = await getBookingToDelete(id, uid);
+ let bookingToDelete: BookingToDelete
+ try {
+ bookingToDelete = await getBookingToDelete(id, uid);
+ } catch (error) {
+ if (isPrismaError(error) && error.code === "P2025") // Record not found
+ {
+ throw new HttpError({
+ statusCode: 404,
+ message: "Booking not found.",
+ });
+ }
+ throw error;
+ }
const {
userId,
platformBookingUrl,
diff --git a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts
index 3cd18c4a7c9..fac503b098e 100644
--- a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts
+++ b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts
@@ -142,7 +142,21 @@ describe("Cancel Booking", () => {
},
},
});
+ });
+
+ test("Should throw when cancelling a non-existing booking", async () => {
+ const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking")).default;
+ await expect(
+ handleCancelBooking({
+ bookingData: {
+ id: 99999,
+ uid: "non-existing-uid",
+ cancelledBy: "[email protected]",
+ cancellationReason: "No reason",
+ },
+ })
+ ).rejects.toThrow("Booking not found.");
});
test("Should call processPaymentRefund", async () => {