chore: DI repositories in AvailableSlotsService class (#22356)

* chore: DI available slots service repositories

* remove configService from worker module

* register scheduleRepo in availableSlotsModule

* chore: bump platform libs

* remove useless comment from prisma module

* fix: availableSlotsModule to class deps

* fix: repositoriesModule deps

* chore: container pattern

* refactor: move modules in DI folder

* fixup! refactor: move modules in DI folder
This commit is contained in:
Morgan authored and GitHub committed 2025-07-09 19:55:55 +00:00
1 parent db84343581
commit 5c508bce6d
32 files changed
+266 -59

No files matched your search

@@ -0,0 +1,19 @@
import { createContainer } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { prismaModule } from "@calcom/prisma/prisma.module";
import type { AvailableSlotsService } from "@calcom/trpc/server/routers/viewer/slots/util";
import { availableSlotsModule } from "../modules/available-slots";
import { oooRepositoryModule } from "../modules/ooo";
import { scheduleRepositoryModule } from "../modules/schedule";
const container = createContainer();
container.load(DI_TOKENS.PRISMA_MODULE, prismaModule);
container.load(DI_TOKENS.OOO_REPOSITORY_MODULE, oooRepositoryModule);
container.load(DI_TOKENS.SCHEDULE_REPOSITORY_MODULE, scheduleRepositoryModule);
container.load(DI_TOKENS.AVAILABLE_SLOTS_SERVICE_MODULE, availableSlotsModule);
export function getAvailableSlotsService() {
return container.get<AvailableSlotsService>(DI_TOKENS.AVAILABLE_SLOTS_SERVICE);
}
@@ -0,0 +1,12 @@
import { createModule } from "@evyweb/ioctopus";
import type { IAvailableSlotsService } from "@calcom/trpc/server/routers/viewer/slots/util";
import { AvailableSlotsService } from "@calcom/trpc/server/routers/viewer/slots/util";
import { DI_TOKENS } from "../tokens";
export const availableSlotsModule = createModule();
availableSlotsModule.bind(DI_TOKENS.AVAILABLE_SLOTS_SERVICE).toClass(AvailableSlotsService, {
oooRepo: DI_TOKENS.OOO_REPOSITORY,
scheduleRepo: DI_TOKENS.SCHEDULE_REPOSITORY,
} satisfies Record<keyof IAvailableSlotsService, symbol>);
+7
View File
@@ -0,0 +1,7 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
export const oooRepositoryModule = createModule();
oooRepositoryModule.bind(DI_TOKENS.OOO_REPOSITORY).toClass(PrismaOOORepository, [DI_TOKENS.PRISMA_CLIENT]);
+9
View File
@@ -0,0 +1,9 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
export const scheduleRepositoryModule = createModule();
scheduleRepositoryModule
.bind(DI_TOKENS.SCHEDULE_REPOSITORY)
.toClass(ScheduleRepository, [DI_TOKENS.PRISMA_CLIENT]); // Maps 'prismaClient' param to PRISMA_CLIENT token
+11
View File
@@ -0,0 +1,11 @@
export const DI_TOKENS = {
PRISMA_CLIENT: Symbol("PrismaClient"),
READ_ONLY_PRISMA_CLIENT: Symbol("ReadOnlyPrismaClient"),
PRISMA_MODULE: Symbol("PrismaModule"),
OOO_REPOSITORY: Symbol("OOORepository"),
OOO_REPOSITORY_MODULE: Symbol("OOORepositoryModule"),
SCHEDULE_REPOSITORY: Symbol("ScheduleRepository"),
SCHEDULE_REPOSITORY_MODULE: Symbol("ScheduleRepositoryModule"),
AVAILABLE_SLOTS_SERVICE: Symbol("AvailableSlotsService"),
AVAILABLE_SLOTS_SERVICE_MODULE: Symbol("AvailableSlotsModule"),
};
+8
View File
@@ -68,6 +68,11 @@
"import": "./dist/conferencing.js",
"require": "./dist/conferencing.cjs",
"types": "./dist/conferencing.d.ts"
},
"./repositories": {
"import": "./dist/repositories.js",
"require": "./dist/repositories.cjs",
"types": "./dist/repositories.d.ts"
}
},
"typesVersions": {
@@ -92,6 +97,9 @@
],
"conferencing": [
"dist/conferencing.d.ts"
],
"repositories": [
"dist/repositories.d.ts"
]
}
}
@@ -0,0 +1,2 @@
export { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
export { ScheduleRepository as PrismaScheduleRepository } from "@calcom/lib/server/repository/schedule";
@@ -25,6 +25,7 @@ export default defineConfig({
workflows: resolve(__dirname, "./workflows.ts"),
slots: resolve(__dirname, "./slots.ts"),
conferencing: resolve(__dirname, "./conferencing.ts"),
repositories: resolve(__dirname, "./repositories.ts"),
},
name: "calcom-lib",
fileName: "calcom-lib",
+11
View File
@@ -0,0 +1,11 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { prisma, readonlyPrisma } from "./index";
export const prismaModule = createModule();
prismaModule.bind(DI_TOKENS.PRISMA_CLIENT).toFactory(() => prisma, "singleton");
prismaModule.bind(DI_TOKENS.READ_ONLY_PRISMA_CLIENT).toFactory(() => readonlyPrisma, "singleton");
@@ -1,6 +1,7 @@
import type { IncomingMessage } from "http";
import { AvailableSlotsService } from "../slots/util";
import { getAvailableSlotsService } from "@calcom/lib/di/containers/available-slots";
import type { TGetTeamScheduleInputSchema } from "./getTeamSchedule.schema";
export type GetTeamScheduleOptions = {
@@ -13,6 +14,6 @@ interface ContextForGetSchedule extends Record<string, unknown> {
}
export const getTeamScheduleHandler = async ({ ctx, input }: GetTeamScheduleOptions) => {
const availableSlotsService = new AvailableSlotsService();
const availableSlotsService = getAvailableSlotsService();
return await availableSlotsService.getAvailableSlots({ ctx, input });
};
@@ -1,7 +1,8 @@
import { getAvailableSlotsService } from "@calcom/lib/di/containers/available-slots";
import type { GetScheduleOptions } from "./types";
import { AvailableSlotsService } from "./util";
export const getScheduleHandler = async ({ ctx, input }: GetScheduleOptions) => {
const availableSlotsService = new AvailableSlotsService();
const availableSlotsService = getAvailableSlotsService();
return await availableSlotsService.getAvailableSlots({ ctx, input });
};
@@ -44,13 +44,12 @@ import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
import { BookingRepository as BookingRepo } from "@calcom/lib/server/repository/booking";
import { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
import { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
import { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
import type { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
import type { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
import { SelectedSlotsRepository } from "@calcom/lib/server/repository/selectedSlots";
import { TeamRepository } from "@calcom/lib/server/repository/team";
import { UserRepository, withSelectedCalendars } from "@calcom/lib/server/repository/user";
import getSlots from "@calcom/lib/slots";
import prisma from "@calcom/prisma";
import { PeriodType } from "@calcom/prisma/client";
import { SchedulingType } from "@calcom/prisma/enums";
import type { EventBusyDate, EventBusyDetails } from "@calcom/types/Calendar";
@@ -89,7 +88,14 @@ export type GetAvailableSlotsResponse = Awaited<
ReturnType<(typeof AvailableSlotsService)["prototype"]["_getAvailableSlots"]>
>;
export interface IAvailableSlotsService {
oooRepo: PrismaOOORepository;
scheduleRepo: ScheduleRepository;
}
export class AvailableSlotsService {
constructor(private readonly dependencies: IAvailableSlotsService) {}
private async _getReservedSlotsAndCleanupExpired({
bookerClientUid,
usersWithCredentials,
@@ -626,8 +632,7 @@ export class AvailableSlotsService {
);
private async _getOOODates(startTimeDate: Date, endTimeDate: Date, allUserIds: number[]) {
const oooRepo = new PrismaOOORepository(prisma);
return oooRepo.findManyOOO({ startTimeDate, endTimeDate, allUserIds });
return this.dependencies.oooRepo.findManyOOO({ startTimeDate, endTimeDate, allUserIds });
}
private getOOODates = withReporting(this._getOOODates.bind(this), "getOOODates");
@@ -1054,8 +1059,7 @@ export class AvailableSlotsService {
// TODO: DI isRestrictionScheduleEnabled
const isRestrictionScheduleFeatureEnabled = await isRestrictionScheduleEnabled(eventType.team?.id);
if (eventType.restrictionScheduleId && isRestrictionScheduleFeatureEnabled) {
const scheduleRepo = new ScheduleRepository(prisma);
const restrictionSchedule = await scheduleRepo.findScheduleByIdForBuildDateRanges({
const restrictionSchedule = await this.dependencies.scheduleRepo.findScheduleByIdForBuildDateRanges({
scheduleId: eventType.restrictionScheduleId,
});
if (restrictionSchedule) {