Skip to content

Commit 211b958

Browse files
refactor: move isRestrictionScheduleEnabled into AvailableSlotsService with DI (#23015)
* refactor: move isRestrictionScheduleEnabled into AvailableSlotsService with DI - Remove external isRestrictionScheduleEnabled function call from AvailableSlotsService - Add IFeaturesRepository to IAvailableSlotsService interface dependencies - Implement checkRestrictionScheduleEnabled private method within AvailableSlotsService - Update DI module to inject featuresRepo dependency - Update API v2 service to pass featuresRepository to base service - Resolves TODO comment about implementing DI for isRestrictionScheduleEnabled Co-Authored-By: [email protected] <[email protected]> * chore: bump platform libs * fix: api v2 di for qualifiedHostsService * chore: bump platform libs * fix unit test --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Morgan <[email protected]>
1 parent 01a0d43 commit 211b958

File tree

13 files changed

+90
-96
lines changed

13 files changed

+90
-96
lines changed

apps/api/v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@axiomhq/winston": "^1.2.0",
3939
"@calcom/platform-constants": "*",
4040
"@calcom/platform-enums": "*",
41-
"@calcom/platform-libraries": "npm:@calcom/[email protected].298",
41+
"@calcom/platform-libraries": "npm:@calcom/[email protected].300",
4242
"@calcom/platform-types": "*",
4343
"@calcom/platform-utils": "*",
4444
"@calcom/prisma": "*",

apps/api/v2/src/lib/modules/available-slots.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { AvailableSlotsService } from "@/lib/services/available-slots.service";
1111
import { BusyTimesService } from "@/lib/services/busy-times.service";
1212
import { CacheService } from "@/lib/services/cache.service";
1313
import { CheckBookingLimitsService } from "@/lib/services/check-booking-limits.service";
14+
import { FilterHostsService } from "@/lib/services/filter-hosts.service";
15+
import { QualifiedHostsService } from "@/lib/services/qualified-hosts.service";
1416
import { UserAvailabilityService } from "@/lib/services/user-availability.service";
1517
import { PrismaModule } from "@/modules/prisma/prisma.module";
1618
import { RedisService } from "@/modules/redis/redis.service";
@@ -34,6 +36,8 @@ import { Module } from "@nestjs/common";
3436
AvailableSlotsService,
3537
UserAvailabilityService,
3638
BusyTimesService,
39+
FilterHostsService,
40+
QualifiedHostsService,
3741
],
3842
exports: [AvailableSlotsService],
3943
})

apps/api/v2/src/lib/services/available-slots.service.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PrismaUserRepository } from "@/lib/repositories/prisma-user.repository"
1010
import { BusyTimesService } from "@/lib/services/busy-times.service";
1111
import { CacheService } from "@/lib/services/cache.service";
1212
import { CheckBookingLimitsService } from "@/lib/services/check-booking-limits.service";
13+
import { QualifiedHostsService } from "@/lib/services/qualified-hosts.service";
1314
import { RedisService } from "@/modules/redis/redis.service";
1415
import { Injectable } from "@nestjs/common";
1516

@@ -29,7 +30,12 @@ export class AvailableSlotsService extends BaseAvailableSlotsService {
2930
eventTypeRepository: PrismaEventTypeRepository,
3031
userRepository: PrismaUserRepository,
3132
redisService: RedisService,
32-
featuresRepository: PrismaFeaturesRepository
33+
featuresRepository: PrismaFeaturesRepository,
34+
qualifiedHostsService: QualifiedHostsService,
35+
checkBookingLimitsService: CheckBookingLimitsService,
36+
cacheService: CacheService,
37+
userAvailabilityService: UserAvailabilityService,
38+
busyTimesService: BusyTimesService
3339
) {
3440
super({
3541
oooRepo: oooRepoDependency,
@@ -41,15 +47,12 @@ export class AvailableSlotsService extends BaseAvailableSlotsService {
4147
eventTypeRepo: eventTypeRepository,
4248
userRepo: userRepository,
4349
redisClient: redisService,
44-
checkBookingLimitsService: new CheckBookingLimitsService(bookingRepository),
45-
cacheService: new CacheService(featuresRepository),
46-
userAvailabilityService: new UserAvailabilityService(
47-
oooRepoDependency,
48-
bookingRepository,
49-
eventTypeRepository,
50-
redisService
51-
),
52-
busyTimesService: new BusyTimesService(bookingRepository),
50+
checkBookingLimitsService,
51+
cacheService,
52+
userAvailabilityService,
53+
busyTimesService,
54+
qualifiedHostsService,
55+
featuresRepo: featuresRepository,
5356
});
5457
}
5558
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { PrismaBookingRepository } from "@/lib/repositories/prisma-booking.repository";
2+
import { Injectable } from "@nestjs/common";
3+
4+
import { FilterHostsService as BaseFilterHostsService } from "@calcom/platform-libraries/slots";
5+
6+
@Injectable()
7+
export class FilterHostsService extends BaseFilterHostsService {
8+
constructor(bookingRepository: PrismaBookingRepository) {
9+
super({
10+
bookingRepo: bookingRepository,
11+
});
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PrismaBookingRepository } from "@/lib/repositories/prisma-booking.repository";
2+
import { FilterHostsService } from "@/lib/services/filter-hosts.service";
3+
import { Injectable } from "@nestjs/common";
4+
5+
import { QualifiedHostsService as BaseQualifiedHostsService } from "@calcom/platform-libraries/slots";
6+
7+
@Injectable()
8+
export class QualifiedHostsService extends BaseQualifiedHostsService {
9+
constructor(bookingRepository: PrismaBookingRepository, filterHostsService: FilterHostsService) {
10+
super({
11+
bookingRepo: bookingRepository,
12+
filterHostsService,
13+
});
14+
}
15+
}

packages/lib/bookings/findQualifiedHostsWithDelegationCredentials.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { Mock } from "vitest";
55

66
import { RRResetInterval, SchedulingType } from "@calcom/prisma/enums";
77

8+
import { getQualifiedHostsService } from "../di/containers/QualifiedHosts";
89
import { filterHostsByLeadThreshold } from "./filterHostsByLeadThreshold";
9-
import { findQualifiedHostsWithDelegationCredentials } from "./findQualifiedHostsWithDelegationCredentials";
1010
import * as getRoutedUsers from "./getRoutedUsers";
1111

1212
// Mock the filterHostsByLeadThreshold function
@@ -21,6 +21,7 @@ afterEach(() => {
2121
(filterHostsByLeadThreshold as Mock).mockClear();
2222
});
2323

24+
const qualifiedHostsService = getQualifiedHostsService();
2425
describe("findQualifiedHostsWithDelegationCredentials", async () => {
2526
it("should return qualified hosts based on mock of filterHostsByLeadThreshold", async () => {
2627
const hosts = [
@@ -92,7 +93,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
9293
};
9394

9495
// Call the function under test
95-
const result = await findQualifiedHostsWithDelegationCredentials({
96+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
9697
eventType,
9798
routedTeamMemberIds: [],
9899
rescheduleUid: null,
@@ -144,7 +145,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
144145
};
145146

146147
// Call the function under test
147-
const result = await findQualifiedHostsWithDelegationCredentials({
148+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
148149
eventType,
149150
routedTeamMemberIds: [],
150151
rescheduleUid: null,
@@ -231,7 +232,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
231232
};
232233

233234
// Call the function under test
234-
const result = await findQualifiedHostsWithDelegationCredentials({
235+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
235236
eventType,
236237
routedTeamMemberIds: [],
237238
rescheduleUid: null,
@@ -320,7 +321,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
320321
};
321322

322323
// Call the function under test
323-
const result = await findQualifiedHostsWithDelegationCredentials({
324+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
324325
eventType,
325326
routedTeamMemberIds: [1],
326327
rescheduleUid: null,
@@ -398,7 +399,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
398399
};
399400
prismaMock.booking.findFirst.mockResolvedValue({ userId: 2 });
400401

401-
const result = await findQualifiedHostsWithDelegationCredentials({
402+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
402403
eventType,
403404
routedTeamMemberIds: [],
404405
rescheduleUid: "recheduleUid",
@@ -478,7 +479,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
478479
.mockImplementation(async () => [hosts[0]]);
479480

480481
// Call the function under test
481-
const result = await findQualifiedHostsWithDelegationCredentials({
482+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
482483
eventType,
483484
routedTeamMemberIds: [0, 1, 2],
484485
rescheduleUid: null,
@@ -572,7 +573,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
572573
]);
573574

574575
// Call the function under test
575-
const result = await findQualifiedHostsWithDelegationCredentials({
576+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
576577
eventType,
577578
routedTeamMemberIds: [2, 3],
578579
rescheduleUid: null,
@@ -672,7 +673,7 @@ describe("findQualifiedHostsWithDelegationCredentials", async () => {
672673
(filterHostsByLeadThreshold as Mock).mockResolvedValue(rrHostsAfterFairness);
673674

674675
// Call the function under test
675-
const result = await findQualifiedHostsWithDelegationCredentials({
676+
const result = await qualifiedHostsService.findQualifiedHostsWithDelegationCredentials({
676677
eventType,
677678
routedTeamMemberIds: [2, 3],
678679
rescheduleUid: null,

packages/lib/bookings/findQualifiedHostsWithDelegationCredentials.ts

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import type { EventType } from "@calcom/lib/bookings/getRoutedUsers";
77
import { withReporting } from "@calcom/lib/sentryWrapper";
88
import type { BookingRepository } from "@calcom/lib/server/repository/booking";
9-
import type { PrismaClient } from "@calcom/prisma";
109
import type { SelectedCalendar } from "@calcom/prisma/client";
1110
import type { SchedulingType } from "@calcom/prisma/enums";
1211
import type { CredentialForCalendarService, CredentialPayload } from "@calcom/types/Credential";
@@ -15,7 +14,6 @@ import type { RoutingFormResponse } from "../server/getLuckyUser";
1514
import { filterHostsByLeadThreshold } from "./filterHostsByLeadThreshold";
1615

1716
export interface IQualifiedHostsService {
18-
prisma: PrismaClient;
1917
bookingRepo: BookingRepository;
2018
filterHostsService: FilterHostsService;
2119
}
@@ -227,65 +225,3 @@ export class QualifiedHostsService {
227225
"findQualifiedHostsWithDelegationCredentials"
228226
);
229227
}
230-
231-
const _findQualifiedHostsWithDelegationCredentials = async <
232-
T extends {
233-
email: string;
234-
id: number;
235-
credentials: CredentialPayload[];
236-
userLevelSelectedCalendars: SelectedCalendar[];
237-
} & Record<string, unknown>
238-
>({
239-
eventType,
240-
rescheduleUid,
241-
routedTeamMemberIds,
242-
contactOwnerEmail,
243-
routingFormResponse,
244-
}: {
245-
eventType: {
246-
id: number;
247-
schedulingType: SchedulingType | null;
248-
hosts?: Host<T>[];
249-
users: T[];
250-
maxLeadThreshold?: number | null;
251-
team?: {
252-
parentId?: number | null;
253-
rrResetInterval: string | null;
254-
rrTimestampBasis: string | null;
255-
} | null;
256-
isRRWeightsEnabled: boolean;
257-
rescheduleWithSameRoundRobinHost: boolean;
258-
teamId?: number;
259-
includeNoShowInRRCalculation: boolean;
260-
} & EventType;
261-
rescheduleUid: string | null;
262-
routedTeamMemberIds: number[];
263-
contactOwnerEmail: string | null;
264-
routingFormResponse: RoutingFormResponse | null;
265-
}) => {
266-
const { prisma } = await import("@calcom/prisma");
267-
const { BookingRepository } = await import("@calcom/lib/server/repository/booking");
268-
const { FilterHostsService } = await import("@calcom/lib/bookings/filterHostsBySameRoundRobinHost");
269-
270-
const bookingRepo = new BookingRepository(prisma);
271-
const filterHostsService = new FilterHostsService({ bookingRepo });
272-
273-
const service = new QualifiedHostsService({
274-
prisma,
275-
bookingRepo,
276-
filterHostsService,
277-
});
278-
279-
return service.findQualifiedHostsWithDelegationCredentials({
280-
eventType,
281-
rescheduleUid,
282-
routedTeamMemberIds,
283-
contactOwnerEmail,
284-
routingFormResponse,
285-
});
286-
};
287-
288-
export const findQualifiedHostsWithDelegationCredentials = withReporting(
289-
_findQualifiedHostsWithDelegationCredentials,
290-
"findQualifiedHostsWithDelegationCredentials"
291-
);

packages/lib/di/containers/AvailableSlots.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import { cacheModule } from "../modules/Cache";
1212
import { checkBookingLimitsModule } from "../modules/CheckBookingLimits";
1313
import { eventTypeRepositoryModule } from "../modules/EventType";
1414
import { featuresRepositoryModule } from "../modules/Features";
15+
import { filterHostsModule } from "../modules/FilterHosts";
1516
import { getUserAvailabilityModule } from "../modules/GetUserAvailability";
1617
import { oooRepositoryModule } from "../modules/Ooo";
18+
import { qualifiedHostsModule } from "../modules/QualifiedHosts";
1719
import { routingFormResponseRepositoryModule } from "../modules/RoutingFormResponse";
1820
import { scheduleRepositoryModule } from "../modules/Schedule";
1921
import { selectedSlotsRepositoryModule } from "../modules/SelectedSlots";
@@ -37,6 +39,9 @@ container.load(DI_TOKENS.CHECK_BOOKING_LIMITS_SERVICE_MODULE, checkBookingLimits
3739
container.load(DI_TOKENS.AVAILABLE_SLOTS_SERVICE_MODULE, availableSlotsModule);
3840
container.load(DI_TOKENS.GET_USER_AVAILABILITY_SERVICE_MODULE, getUserAvailabilityModule);
3941
container.load(DI_TOKENS.BUSY_TIMES_SERVICE_MODULE, busyTimesModule);
42+
container.load(DI_TOKENS.BUSY_TIMES_SERVICE_MODULE, busyTimesModule);
43+
container.load(DI_TOKENS.FILTER_HOSTS_SERVICE_MODULE, filterHostsModule);
44+
container.load(DI_TOKENS.QUALIFIED_HOSTS_SERVICE_MODULE, qualifiedHostsModule);
4045

4146
export function getAvailableSlotsService() {
4247
return container.get<AvailableSlotsService>(DI_TOKENS.AVAILABLE_SLOTS_SERVICE);

packages/lib/di/modules/AvailableSlots.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ availableSlotsModule.bind(DI_TOKENS.AVAILABLE_SLOTS_SERVICE).toClass(AvailableSl
2020
checkBookingLimitsService: DI_TOKENS.CHECK_BOOKING_LIMITS_SERVICE,
2121
userAvailabilityService: DI_TOKENS.GET_USER_AVAILABILITY_SERVICE,
2222
busyTimesService: DI_TOKENS.BUSY_TIMES_SERVICE,
23+
featuresRepo: DI_TOKENS.FEATURES_REPOSITORY,
24+
qualifiedHostsService: DI_TOKENS.QUALIFIED_HOSTS_SERVICE,
2325
} satisfies Record<keyof IAvailableSlotsService, symbol>);

packages/lib/di/modules/QualifiedHosts.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { DI_TOKENS } from "../tokens";
66

77
export const qualifiedHostsModule = createModule();
88
qualifiedHostsModule.bind(DI_TOKENS.QUALIFIED_HOSTS_SERVICE).toClass(QualifiedHostsService, {
9-
prisma: DI_TOKENS.PRISMA_CLIENT,
109
bookingRepo: DI_TOKENS.BOOKING_REPOSITORY,
1110
filterHostsService: DI_TOKENS.FILTER_HOSTS_SERVICE,
1211
} satisfies Record<keyof IQualifiedHostsService, symbol>);

0 commit comments

Comments
 (0)