Skip to content

Commit fa66f25

Browse files
authored
fix: 400 is correct error code for computing slot for past booking (calcom#22574)
* fix * add test
1 parent b40d448 commit fa66f25

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import { BookingDateInPastError, isTimeOutOfBounds } from "@calcom/lib/isOutOfBounds";
4+
5+
import { TRPCError } from "@trpc/server";
6+
7+
describe("BookingDateInPastError handling", () => {
8+
it("should convert BookingDateInPastError to TRPCError with BAD_REQUEST code", () => {
9+
const testFilteringLogic = () => {
10+
const mockSlot = {
11+
time: "2024-05-20T12:30:00.000Z", // Past date
12+
attendees: 1,
13+
};
14+
15+
const mockEventType = {
16+
minimumBookingNotice: 0,
17+
};
18+
19+
const isFutureLimitViolationForTheSlot = false; // Mock this to false
20+
21+
let isOutOfBounds = false;
22+
try {
23+
// This will throw BookingDateInPastError for past dates
24+
isOutOfBounds = isTimeOutOfBounds({
25+
time: mockSlot.time,
26+
minimumBookingNotice: mockEventType.minimumBookingNotice,
27+
});
28+
} catch (error) {
29+
if (error instanceof BookingDateInPastError) {
30+
throw new TRPCError({
31+
code: "BAD_REQUEST",
32+
message: error.message,
33+
});
34+
}
35+
throw error;
36+
}
37+
38+
return !isFutureLimitViolationForTheSlot && !isOutOfBounds;
39+
};
40+
41+
// This should throw a TRPCError with BAD_REQUEST code
42+
expect(() => testFilteringLogic()).toThrow(TRPCError);
43+
expect(() => testFilteringLogic()).toThrow("Attempting to book a meeting in the past.");
44+
});
45+
});

packages/trpc/server/routers/viewer/slots/util.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
calculatePeriodLimits,
3636
isTimeOutOfBounds,
3737
isTimeViolatingFutureLimit,
38+
BookingDateInPastError,
3839
} from "@calcom/lib/isOutOfBounds";
3940
import logger from "@calcom/lib/logger";
4041
import { isRestrictionScheduleEnabled } from "@calcom/lib/restrictionSchedule";
@@ -1313,14 +1314,30 @@ export class AvailableSlotsService {
13131314
periodLimits,
13141315
});
13151316

1317+
let isOutOfBounds = false;
1318+
try {
1319+
isOutOfBounds = isTimeOutOfBounds({
1320+
time: slot.time,
1321+
minimumBookingNotice: eventType.minimumBookingNotice,
1322+
});
1323+
} catch (error) {
1324+
if (error instanceof BookingDateInPastError) {
1325+
throw new TRPCError({
1326+
code: "BAD_REQUEST",
1327+
message: error.message,
1328+
});
1329+
}
1330+
throw error;
1331+
}
1332+
13161333
if (isFutureLimitViolationForTheSlot) {
13171334
foundAFutureLimitViolation = true;
13181335
}
13191336

13201337
return (
13211338
!isFutureLimitViolationForTheSlot &&
13221339
// 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.
1323-
!isTimeOutOfBounds({ time: slot.time, minimumBookingNotice: eventType.minimumBookingNotice })
1340+
!isOutOfBounds
13241341
);
13251342
});
13261343

0 commit comments

Comments
 (0)