perf: getUserAvailability if dateRange is empty, early return (#26000)

* Early return if date range is empty

* test: add tests for empty workingHours early return optimization

Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Volnei Munhoz <volnei@cal.com>
This commit is contained in:
authored and GitHub committed 2026-01-21 02:51:01 -03:00
1 parent 546a1510b2
commit ec951cacad
2 files changed
+384 -116

No files matched your search

+253 -4
View File
@@ -24,12 +24,14 @@ import { expect, expectedSlotsForSchedule } from "./getSchedule/expects";
import { setupAndTeardown } from "./getSchedule/setupAndTeardown";
import { timeTravelToTheBeginningOfToday } from "./getSchedule/utils";
/* eslint-disable @typescript-eslint/no-explicit-any */
constantsScenarios.set({
IS_PRODUCTION: true as any,
IS_PRODUCTION: true,
WEBAPP_URL: "http://localhost:3000",
RESERVED_SUBDOMAINS: ["auth", "docs"] as any,
SINGLE_ORG_SLUG: "" as any,
});
RESERVED_SUBDOMAINS: ["auth", "docs"],
SINGLE_ORG_SLUG: "",
} as any);
/* eslint-enable @typescript-eslint/no-explicit-any */
describe("getSchedule", () => {
const availableSlotsService = getAvailableSlotsService();
@@ -3630,4 +3632,251 @@ describe("getSchedule", () => {
);
});
});
describe("Empty working hours - early return optimization", () => {
test("returns no slots when workingHours is empty", async () => {
vi.setSystemTime("2024-05-21T00:00:13Z");
const plus1DateString = "2024-05-22";
const plus2DateString = "2024-05-23";
await createBookingScenario({
eventTypes: [
{
id: 1,
slotInterval: 60,
length: 60,
users: [
{
id: 101,
},
],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [TestData.schedules.EmptyAvailability],
},
],
bookings: [],
});
const schedule = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: `${plus1DateString}T18:30:00.000Z`,
endTime: `${plus2DateString}T18:29:59.999Z`,
timeZone: Timezones["+5:30"],
isTeamEvent: false,
orgSlug: null,
},
});
expect(schedule).toHaveDateDisabled({
dateString: plus2DateString,
});
});
test("returns slots only for date override when workingHours is empty but date override exists", async () => {
vi.setSystemTime("2024-05-21T00:00:13Z");
const plus1DateString = "2024-05-22";
const plus2DateString = "2024-05-23";
await createBookingScenario({
eventTypes: [
{
id: 1,
slotInterval: 60,
length: 60,
users: [
{
id: 101,
},
],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
name: "Empty schedule with date override",
availability: [
{
days: [],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T12:00:00.000Z"),
date: plus2DateString,
},
],
timeZone: Timezones["+5:30"],
},
],
},
],
bookings: [],
});
const schedule = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: `${plus1DateString}T18:30:00.000Z`,
endTime: `${plus2DateString}T18:29:59.999Z`,
timeZone: Timezones["+5:30"],
isTeamEvent: false,
orgSlug: null,
},
});
expect(schedule).toHaveTimeSlots(
[`03:30:00.000Z`, `04:30:00.000Z`, `05:30:00.000Z`],
{
dateString: plus2DateString,
}
);
});
test("returns slots only for date overrides when workingHours is empty but multiple date overrides exist", async () => {
vi.setSystemTime("2024-05-21T00:00:13Z");
const plus1DateString = "2024-05-22";
const plus2DateString = "2024-05-23";
const plus3DateString = "2024-05-24";
await createBookingScenario({
eventTypes: [
{
id: 1,
slotInterval: 60,
length: 60,
users: [
{
id: 101,
},
],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
name: "Empty schedule with multiple date overrides",
availability: [
{
days: [],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T11:00:00.000Z"),
date: plus2DateString,
},
{
days: [],
startTime: new Date("1970-01-01T14:00:00.000Z"),
endTime: new Date("1970-01-01T16:00:00.000Z"),
date: plus3DateString,
},
],
timeZone: Timezones["+5:30"],
},
],
},
],
bookings: [],
});
const schedule = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: `${plus1DateString}T18:30:00.000Z`,
endTime: `${plus3DateString}T18:29:59.999Z`,
timeZone: Timezones["+5:30"],
isTeamEvent: false,
orgSlug: null,
},
});
expect(schedule).toHaveTimeSlots([`03:30:00.000Z`, `04:30:00.000Z`], {
dateString: plus2DateString,
});
expect(schedule).toHaveTimeSlots([`08:30:00.000Z`, `09:30:00.000Z`], {
dateString: plus3DateString,
});
});
test("returns no slots for dates without date override when workingHours is empty", async () => {
vi.setSystemTime("2024-05-21T00:00:13Z");
const plus1DateString = "2024-05-22";
const plus2DateString = "2024-05-23";
const plus3DateString = "2024-05-24";
await createBookingScenario({
eventTypes: [
{
id: 1,
slotInterval: 60,
length: 60,
users: [
{
id: 101,
},
],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
name: "Empty schedule with date override only on plus3",
availability: [
{
days: [],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T12:00:00.000Z"),
date: plus3DateString,
},
],
timeZone: Timezones["+5:30"],
},
],
},
],
bookings: [],
});
const schedule = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: `${plus1DateString}T18:30:00.000Z`,
endTime: `${plus3DateString}T18:29:59.999Z`,
timeZone: Timezones["+5:30"],
isTeamEvent: false,
orgSlug: null,
},
});
expect(schedule).toHaveDateDisabled({
dateString: plus2DateString,
});
expect(schedule).toHaveTimeSlots(
[`03:30:00.000Z`, `04:30:00.000Z`, `05:30:00.000Z`],
{
dateString: plus3DateString,
}
);
});
});
});