feat: inject required repositories into AvailableSlotsService using dependency injection (#22413)

* feat: inject all repositories into AvailableSlotsService using dependency injection

- Create DI modules for SelectedSlotsRepository, TeamRepository, UserRepository, BookingRepository, EventTypeRepository, RoutingFormResponseRepository
- Update available-slots DI module to bind all 6 repositories
- Update DI container to load all new repository modules
- Replace all 10 direct repository instantiations with injected dependencies
- Add missing DI tokens for repository modules
- Expand IAvailableSlotsService interface to include all repository dependencies

This follows the same dependency injection pattern established in previous repository refactoring PRs and enables better testability and modularity by making all dependencies explicit and configurable.

Co-Authored-By: [email protected] <[email protected]>

* chore: DI available slots repo on api v2

* chore: bump platform libs

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
This commit is contained in:
devin-ai-integration[bot]and[email protected] authored and GitHub committed 2025-07-11 17:37:24 +03:00
1 parent a1571d01bd
commit 13cb99b7fe
30 files changed
+265 -62

No files matched your search

@@ -5,13 +5,25 @@ 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 { bookingRepositoryModule } from "../modules/booking";
import { eventTypeRepositoryModule } from "../modules/eventType";
import { oooRepositoryModule } from "../modules/ooo";
import { routingFormResponseRepositoryModule } from "../modules/routingFormResponse";
import { scheduleRepositoryModule } from "../modules/schedule";
import { selectedSlotsRepositoryModule } from "../modules/selectedSlots";
import { teamRepositoryModule } from "../modules/team";
import { userRepositoryModule } from "../modules/user";
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.SELECTED_SLOTS_REPOSITORY_MODULE, selectedSlotsRepositoryModule);
container.load(DI_TOKENS.TEAM_REPOSITORY_MODULE, teamRepositoryModule);
container.load(DI_TOKENS.USER_REPOSITORY_MODULE, userRepositoryModule);
container.load(DI_TOKENS.BOOKING_REPOSITORY_MODULE, bookingRepositoryModule);
container.load(DI_TOKENS.EVENT_TYPE_REPOSITORY_MODULE, eventTypeRepositoryModule);
container.load(DI_TOKENS.ROUTING_FORM_RESPONSE_REPOSITORY_MODULE, routingFormResponseRepositoryModule);
container.load(DI_TOKENS.AVAILABLE_SLOTS_SERVICE_MODULE, availableSlotsModule);
export function getAvailableSlotsService() {
@@ -9,4 +9,10 @@ export const availableSlotsModule = createModule();
availableSlotsModule.bind(DI_TOKENS.AVAILABLE_SLOTS_SERVICE).toClass(AvailableSlotsService, {
oooRepo: DI_TOKENS.OOO_REPOSITORY,
scheduleRepo: DI_TOKENS.SCHEDULE_REPOSITORY,
selectedSlotsRepo: DI_TOKENS.SELECTED_SLOTS_REPOSITORY,
teamRepo: DI_TOKENS.TEAM_REPOSITORY,
userRepo: DI_TOKENS.USER_REPOSITORY,
bookingRepo: DI_TOKENS.BOOKING_REPOSITORY,
eventTypeRepo: DI_TOKENS.EVENT_TYPE_REPOSITORY,
routingFormResponseRepo: DI_TOKENS.ROUTING_FORM_RESPONSE_REPOSITORY,
} satisfies Record<keyof IAvailableSlotsService, symbol>);
+9
View File
@@ -0,0 +1,9 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { BookingRepository } from "@calcom/lib/server/repository/booking";
export const bookingRepositoryModule = createModule();
bookingRepositoryModule
.bind(DI_TOKENS.BOOKING_REPOSITORY)
.toClass(BookingRepository, [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 { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
export const eventTypeRepositoryModule = createModule();
eventTypeRepositoryModule
.bind(DI_TOKENS.EVENT_TYPE_REPOSITORY)
.toClass(EventTypeRepository, [DI_TOKENS.PRISMA_CLIENT]);
@@ -0,0 +1,9 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
export const routingFormResponseRepositoryModule = createModule();
routingFormResponseRepositoryModule
.bind(DI_TOKENS.ROUTING_FORM_RESPONSE_REPOSITORY)
.toClass(RoutingFormResponseRepository, [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 { SelectedSlotsRepository } from "@calcom/lib/server/repository/selectedSlots";
export const selectedSlotsRepositoryModule = createModule();
selectedSlotsRepositoryModule
.bind(DI_TOKENS.SELECTED_SLOTS_REPOSITORY)
.toClass(SelectedSlotsRepository, [DI_TOKENS.PRISMA_CLIENT]);
+7
View File
@@ -0,0 +1,7 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { TeamRepository } from "@calcom/lib/server/repository/team";
export const teamRepositoryModule = createModule();
teamRepositoryModule.bind(DI_TOKENS.TEAM_REPOSITORY).toClass(TeamRepository, [DI_TOKENS.PRISMA_CLIENT]);
+7
View File
@@ -0,0 +1,7 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { UserRepository } from "@calcom/lib/server/repository/user";
export const userRepositoryModule = createModule();
userRepositoryModule.bind(DI_TOKENS.USER_REPOSITORY).toClass(UserRepository, [DI_TOKENS.PRISMA_CLIENT]);
+12
View File
@@ -6,6 +6,18 @@ export const DI_TOKENS = {
OOO_REPOSITORY_MODULE: Symbol("OOORepositoryModule"),
SCHEDULE_REPOSITORY: Symbol("ScheduleRepository"),
SCHEDULE_REPOSITORY_MODULE: Symbol("ScheduleRepositoryModule"),
SELECTED_SLOTS_REPOSITORY: Symbol("SelectedSlotsRepository"),
SELECTED_SLOTS_REPOSITORY_MODULE: Symbol("SelectedSlotsRepositoryModule"),
TEAM_REPOSITORY: Symbol("TeamRepository"),
TEAM_REPOSITORY_MODULE: Symbol("TeamRepositoryModule"),
USER_REPOSITORY: Symbol("UserRepository"),
USER_REPOSITORY_MODULE: Symbol("UserRepositoryModule"),
BOOKING_REPOSITORY: Symbol("BookingRepository"),
BOOKING_REPOSITORY_MODULE: Symbol("BookingRepositoryModule"),
EVENT_TYPE_REPOSITORY: Symbol("EventTypeRepository"),
EVENT_TYPE_REPOSITORY_MODULE: Symbol("EventTypeRepositoryModule"),
ROUTING_FORM_RESPONSE_REPOSITORY: Symbol("RoutingFormResponseRepository"),
ROUTING_FORM_RESPONSE_REPOSITORY_MODULE: Symbol("RoutingFormResponseRepositoryModule"),
AVAILABLE_SLOTS_SERVICE: Symbol("AvailableSlotsService"),
AVAILABLE_SLOTS_SERVICE_MODULE: Symbol("AvailableSlotsModule"),
};
@@ -1,2 +1,8 @@
export { PrismaOOORepository } from "@calcom/lib/server/repository/ooo";
export { ScheduleRepository as PrismaScheduleRepository } from "@calcom/lib/server/repository/schedule";
export { BookingRepository as PrismaBookingRepository } from "@calcom/lib/server/repository/booking";
export { EventTypeRepository as PrismaEventTypeRepository } from "@calcom/lib/server/repository/eventType";
export { RoutingFormResponseRepository as PrismaRoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
export { SelectedSlotsRepository as PrismaSelectedSlotsRepository } from "@calcom/lib/server/repository/selectedSlots";
export { TeamRepository as PrismaTeamRepository } from "@calcom/lib/server/repository/team";
export { UserRepository as PrismaUserRepository } from "@calcom/lib/server/repository/user";
@@ -41,16 +41,16 @@ import { isRestrictionScheduleEnabled } from "@calcom/lib/restrictionSchedule";
import { safeStringify } from "@calcom/lib/safeStringify";
import { withReporting } from "@calcom/lib/sentryWrapper";
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
import { BookingRepository } from "@calcom/lib/server/repository/booking";
import { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
import { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
import type { BookingRepository } from "@calcom/lib/server/repository/booking";
import type { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
import type { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
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 type { SelectedSlotsRepository } from "@calcom/lib/server/repository/selectedSlots";
import type { TeamRepository } from "@calcom/lib/server/repository/team";
import type { UserRepository } from "@calcom/lib/server/repository/user";
import { 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";
@@ -92,6 +92,12 @@ export type GetAvailableSlotsResponse = Awaited<
export interface IAvailableSlotsService {
oooRepo: PrismaOOORepository;
scheduleRepo: ScheduleRepository;
selectedSlotsRepo: SelectedSlotsRepository;
teamRepo: TeamRepository;
userRepo: UserRepository;
bookingRepo: BookingRepository;
eventTypeRepo: EventTypeRepository;
routingFormResponseRepo: RoutingFormResponseRepository;
}
export class AvailableSlotsService {
@@ -107,7 +113,7 @@ export class AvailableSlotsService {
eventTypeId: number;
}) {
const currentTimeInUtc = dayjs.utc().format();
const slotsRepo = new SelectedSlotsRepository(prisma);
const slotsRepo = this.dependencies.selectedSlotsRepo;
const unexpiredSelectedSlots =
(await slotsRepo.findManyUnexpiredSlots({
@@ -142,7 +148,7 @@ export class AvailableSlotsService {
}
const dynamicEventType = getDefaultEvent(input.eventTypeSlug);
const userRepo = new UserRepository(prisma);
const userRepo = this.dependencies.userRepo;
const usersForDynamicEventType = await userRepo.findManyUsersForDynamicEventType({
currentOrgDomain: isValidOrgDomain ? currentOrgDomain : null,
usernameList: Array.isArray(input.usernameList)
@@ -212,7 +218,7 @@ export class AvailableSlotsService {
return null;
}
const eventTypeRepo = new EventTypeRepository(prisma);
const eventTypeRepo = this.dependencies.eventTypeRepo;
return await eventTypeRepo.findForSlots({ id: eventTypeId });
}
@@ -250,7 +256,7 @@ export class AvailableSlotsService {
) {
const { currentOrgDomain, isValidOrgDomain } = organizationDetails;
log.info("getUserIdFromUsername", safeStringify({ organizationDetails, username }));
const userRepo = new UserRepository(prisma);
const userRepo = this.dependencies.userRepo;
const [user] = await userRepo.findUsersByUsername({
usernameList: [username],
orgSlug: isValidOrgDomain ? currentOrgDomain : null,
@@ -284,7 +290,7 @@ export class AvailableSlotsService {
organizationDetails ?? { currentOrgDomain: null, isValidOrgDomain: false }
);
}
const eventTypeRepo = new EventTypeRepository(prisma);
const eventTypeRepo = this.dependencies.eventTypeRepo;
const eventType = await eventTypeRepo.findFirstEventTypeId({ slug: eventTypeSlug, teamId, userId });
if (!eventType) {
throw new TRPCError({ code: "NOT_FOUND" });
@@ -297,7 +303,7 @@ export class AvailableSlotsService {
organizationDetails: { currentOrgDomain: string | null; isValidOrgDomain: boolean }
) {
const { currentOrgDomain, isValidOrgDomain } = organizationDetails;
const teamRepo = new TeamRepository(prisma);
const teamRepo = this.dependencies.teamRepo;
const team = await teamRepo.findFirstBySlugAndParentSlug({
slug,
parentSlug: isValidOrgDomain && currentOrgDomain ? currentOrgDomain : null,
@@ -510,7 +516,7 @@ export class AvailableSlotsService {
bookingLimits
);
const bookingRepo = new BookingRepository(prisma);
const bookingRepo = this.dependencies.bookingRepo;
const bookings = await bookingRepo.getAllAcceptedTeamBookingsOfUsers({
users,
teamId,
@@ -713,7 +719,7 @@ export class AvailableSlotsService {
const userIdAndEmailMap = new Map(usersWithCredentials.map((user) => [user.id, user.email]));
const allUserIds = Array.from(userIdAndEmailMap.keys());
const bookingRepo = new BookingRepository(prisma);
const bookingRepo = this.dependencies.bookingRepo;
const [currentBookingsAllUsers, outOfOfficeDaysAllUsers] = await Promise.all([
bookingRepo.findAllExistingBookingsForEventTypeBetween({
startDate: startTimeDate,
@@ -940,12 +946,12 @@ export class AvailableSlotsService {
let routingFormResponse = null;
if (routingFormResponseId) {
const formResponseRepo = new RoutingFormResponseRepository(prisma);
const formResponseRepo = this.dependencies.routingFormResponseRepo;
routingFormResponse = await formResponseRepo.findFormResponseIncludeForm({
routingFormResponseId,
});
} else if (queuedFormResponseId) {
const formResponseRepo = new RoutingFormResponseRepository(prisma);
const formResponseRepo = this.dependencies.routingFormResponseRepo;
routingFormResponse = await formResponseRepo.findQueuedFormResponseIncludeForm({
queuedFormResponseId,
});