Skip to content

Commit f6642c1

Browse files
authored
feat: optimize getLuckyUser to skip data fetching for single user (#25337)
- Add early return when only one user is available - Prevents unnecessary database queries, calendar API calls, and OOO checks - Add comprehensive test to verify optimization works correctly Fixes #19503 [CAL-5212]
1 parent 26b4385 commit f6642c1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

packages/features/bookings/lib/getLuckyUser.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,3 +1642,41 @@ describe("get interval times", () => {
16421642
expect(result).toEqual(new Date("2021-06-20T11:59:59Z")); // Based on the mocked system time
16431643
});
16441644
});
1645+
1646+
it("returns the single user correctly without fetching data when only one user available", async () => {
1647+
const singleUser = buildUser({
1648+
id: 42,
1649+
username: "singleuser",
1650+
name: "Single User",
1651+
1652+
bookings: [],
1653+
});
1654+
1655+
// Create spies to verify no data fetching occurs
1656+
const spyCalendar = vi.spyOn(CalendarManagerMock, "getBusyCalendarTimes");
1657+
const spyPrismaUser = vi.spyOn(prismaMock.user, "findMany");
1658+
const spyPrismaHost = vi.spyOn(prismaMock.host, "findMany");
1659+
const spyPrismaBooking = vi.spyOn(prismaMock.booking, "findMany");
1660+
const spyPrismaOOO = vi.spyOn(prismaMock.outOfOfficeEntry, "findMany");
1661+
1662+
await expect(
1663+
luckyUserService.getLuckyUser({
1664+
availableUsers: [singleUser],
1665+
eventType: {
1666+
id: 1,
1667+
isRRWeightsEnabled: false,
1668+
team: { rrResetInterval: RRResetInterval.MONTH, rrTimestampBasis: RRTimestampBasis.CREATED_AT },
1669+
includeNoShowInRRCalculation: false,
1670+
},
1671+
allRRHosts: [],
1672+
routingFormResponse: null,
1673+
})
1674+
).resolves.toStrictEqual(singleUser);
1675+
1676+
// Verify no expensive operations were called
1677+
expect(spyCalendar).not.toHaveBeenCalled();
1678+
expect(spyPrismaUser).not.toHaveBeenCalled();
1679+
expect(spyPrismaHost).not.toHaveBeenCalled();
1680+
expect(spyPrismaBooking).not.toHaveBeenCalled();
1681+
expect(spyPrismaOOO).not.toHaveBeenCalled();
1682+
});

packages/features/bookings/lib/getLuckyUser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,11 @@ export class LuckyUserService implements ILuckyUserService {
916916
weight?: number | null;
917917
}
918918
>(getLuckyUserParams: GetLuckyUserParams<T>) {
919+
// Early return if only one available user to avoid unnecessary data fetching
920+
if (getLuckyUserParams.availableUsers.length === 1) {
921+
return getLuckyUserParams.availableUsers[0];
922+
}
923+
919924
const fetchedData = await this.fetchAllDataNeededForCalculations(getLuckyUserParams);
920925

921926
const { luckyUser } = this.getLuckyUser_requiresDataToBePreFetched({

0 commit comments

Comments
 (0)