fix: 400 is correct error code for computing slot for past booking (#22574)

* fix

* add test
This commit is contained in:
Benny Joo
2025-07-16 22:46:03 +00:00
committed by GitHub
parent b40d448e07
commit fa66f2531f
2 changed files with 63 additions and 1 deletions
@@ -0,0 +1,45 @@
import { describe, it, expect } from "vitest";
import { BookingDateInPastError, isTimeOutOfBounds } from "@calcom/lib/isOutOfBounds";
import { TRPCError } from "@trpc/server";
describe("BookingDateInPastError handling", () => {
it("should convert BookingDateInPastError to TRPCError with BAD_REQUEST code", () => {
const testFilteringLogic = () => {
const mockSlot = {
time: "2024-05-20T12:30:00.000Z", // Past date
attendees: 1,
};
const mockEventType = {
minimumBookingNotice: 0,
};
const isFutureLimitViolationForTheSlot = false; // Mock this to false
let isOutOfBounds = false;
try {
// This will throw BookingDateInPastError for past dates
isOutOfBounds = isTimeOutOfBounds({
time: mockSlot.time,
minimumBookingNotice: mockEventType.minimumBookingNotice,
});
} catch (error) {
if (error instanceof BookingDateInPastError) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
}
throw error;
}
return !isFutureLimitViolationForTheSlot && !isOutOfBounds;
};
// This should throw a TRPCError with BAD_REQUEST code
expect(() => testFilteringLogic()).toThrow(TRPCError);
expect(() => testFilteringLogic()).toThrow("Attempting to book a meeting in the past.");
});
});
@@ -35,6 +35,7 @@ import {
calculatePeriodLimits,
isTimeOutOfBounds,
isTimeViolatingFutureLimit,
BookingDateInPastError,
} from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger";
import { isRestrictionScheduleEnabled } from "@calcom/lib/restrictionSchedule";
@@ -1313,6 +1314,22 @@ export class AvailableSlotsService {
periodLimits,
});
let isOutOfBounds = false;
try {
isOutOfBounds = isTimeOutOfBounds({
time: slot.time,
minimumBookingNotice: eventType.minimumBookingNotice,
});
} catch (error) {
if (error instanceof BookingDateInPastError) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
}
throw error;
}
if (isFutureLimitViolationForTheSlot) {
foundAFutureLimitViolation = true;
}
@@ -1320,7 +1337,7 @@ export class AvailableSlotsService {
return (
!isFutureLimitViolationForTheSlot &&
// TODO: Perf Optimization: Slots calculation logic already seems to consider the minimum booking notice and past booking time and thus there shouldn't be need to filter out slots here.
!isTimeOutOfBounds({ time: slot.time, minimumBookingNotice: eventType.minimumBookingNotice })
!isOutOfBounds
);
});