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:
1 parent
db84343581
commit
5c508bce6d
32 files changed
+266
-59
No files matched your search
@@ -4,11 +4,11 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { isSupportedTimeZone } from "@calcom/lib/dayjs";
|
||||
import { getAvailableSlotsService } from "@calcom/lib/di/containers/available-slots";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultResponder } from "@calcom/lib/server/defaultResponder";
|
||||
import { createContext } from "@calcom/trpc/server/createContext";
|
||||
import { getScheduleSchema } from "@calcom/trpc/server/routers/viewer/slots/types";
|
||||
import { AvailableSlotsService } from "@calcom/trpc/server/routers/viewer/slots/util";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
|
||||
@@ -27,11 +27,13 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
const input = getScheduleSchema.parse({ usernameList: slugs, isTeamEvent: parsedIsTeamEvent, ...rest });
|
||||
const timeZoneSupported = input.timeZone ? isSupportedTimeZone(input.timeZone) : false;
|
||||
const availableSlotsService = new AvailableSlotsService();
|
||||
|
||||
const availableSlotsService = getAvailableSlotsService();
|
||||
const availableSlots = await availableSlotsService.getAvailableSlots({
|
||||
ctx: await createContext({ req, res }),
|
||||
input,
|
||||
});
|
||||
|
||||
const slotsInProvidedTimeZone = timeZoneSupported
|
||||
? Object.keys(availableSlots.slots).reduce(
|
||||
(acc: Record<string, { time: string; attendees?: number; bookingUid?: string }[]>, date) => {
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
"@axiomhq/winston": "^1.2.0",
|
||||
"@calcom/platform-constants": "*",
|
||||
"@calcom/platform-enums": "*",
|
||||
"@calcom/platform-libraries": "npm:@calcom/[email protected]3",
|
||||
"@calcom/platform-libraries": "npm:@calcom/[email protected]9",
|
||||
"@calcom/platform-types": "*",
|
||||
"@calcom/platform-utils": "*",
|
||||
"@calcom/prisma": "*",
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
import { PrismaOOORepository } from "@calcom/platform-libraries/repositories";
|
||||
import { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
@Injectable()
|
||||
export class OOORepository extends PrismaOOORepository {
|
||||
constructor(private readonly dbWrite: PrismaWriteService) {
|
||||
super(dbWrite.prisma as unknown as PrismaClient);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
import { PrismaScheduleRepository } from "@calcom/platform-libraries/repositories";
|
||||
import { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleRepository extends PrismaScheduleRepository {
|
||||
constructor(private readonly dbWrite: PrismaWriteService) {
|
||||
super(dbWrite.prisma as unknown as PrismaClient);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { OOORepository } from "@/lib/repositories/PrismaOOORepository";
|
||||
import { ScheduleRepository } from "@/lib/repositories/PrismaScheduleRepository";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
import { AvailableSlotsService as BaseAvailableSlotsService } from "@calcom/platform-libraries/slots";
|
||||
|
||||
@Injectable()
|
||||
export class AvailableSlotsService extends BaseAvailableSlotsService {
|
||||
constructor(
|
||||
private readonly oooRepoDependency: OOORepository,
|
||||
private readonly scheduleRepoDependency: ScheduleRepository
|
||||
) {
|
||||
super({ oooRepo: oooRepoDependency, scheduleRepo: scheduleRepoDependency });
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { TRPC_ERROR_CODE, TRPC_ERROR_MAP, TRPCErrorCode } from "@/filters/trpc-exception.filter";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { SlotsOutputService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import type { RangeSlots, TimeSlots } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import { SlotsWorkerService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-worker.service";
|
||||
@@ -17,7 +18,6 @@ import {
|
||||
VERSION_2024_08_13,
|
||||
} from "@calcom/platform-constants";
|
||||
import { TRPCError } from "@calcom/platform-libraries";
|
||||
import { AvailableSlotsService } from "@calcom/platform-libraries/slots";
|
||||
import { RemoveSelectedSlotInput_2024_04_15, ReserveSlotInput_2024_04_15 } from "@calcom/platform-types";
|
||||
import { ApiResponse, GetAvailableSlotsInput_2024_04_15 } from "@calcom/platform-types";
|
||||
|
||||
@@ -31,7 +31,8 @@ export class SlotsController_2024_04_15 {
|
||||
private readonly slotsService: SlotsService_2024_04_15,
|
||||
private readonly config: ConfigService,
|
||||
private readonly slotsOutputService: SlotsOutputService_2024_04_15,
|
||||
private readonly slotsWorkerService: SlotsWorkerService_2024_04_15
|
||||
private readonly slotsWorkerService: SlotsWorkerService_2024_04_15,
|
||||
private readonly availableSlotsService: AvailableSlotsService
|
||||
) {}
|
||||
|
||||
@Post("/reserve")
|
||||
@@ -170,7 +171,7 @@ export class SlotsController_2024_04_15 {
|
||||
: query.isTeamEvent;
|
||||
|
||||
// Do not use workers in E2E, not supported by TS-JEST
|
||||
const availableSlotsService = this.config.get<boolean>("e2e") ? new AvailableSlotsService() : null;
|
||||
const availableSlotsService = this.config.get<boolean>("e2e") ? this.availableSlotsService : null;
|
||||
|
||||
const availableSlots = availableSlotsService
|
||||
? await availableSlotsService.getAvailableSlots({
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { EventTypesModule_2024_04_15 } from "@/ee/event-types/event-types_2024_04_15/event-types.module";
|
||||
import { OOORepository as PrismaOOORepository } from "@/lib/repositories/PrismaOOORepository";
|
||||
import { ScheduleRepository as PrismaScheduleRepository } from "@/lib/repositories/PrismaScheduleRepository";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||
import { SlotsController_2024_04_15 } from "@/modules/slots/slots-2024-04-15/controllers/slots.controller";
|
||||
import { SlotsOutputService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
@@ -14,6 +17,9 @@ import { Module } from "@nestjs/common";
|
||||
SlotsService_2024_04_15,
|
||||
SlotsOutputService_2024_04_15,
|
||||
SlotsWorkerService_2024_04_15,
|
||||
PrismaOOORepository,
|
||||
PrismaScheduleRepository,
|
||||
AvailableSlotsService,
|
||||
],
|
||||
controllers: [SlotsController_2024_04_15],
|
||||
exports: [SlotsService_2024_04_15],
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// apps/api/v2/src/modules/slots/slots-2024-04-15/workers/slots-worker.module.ts
|
||||
// Needed if ConfigService is used anywhere
|
||||
import appConfig from "@/config/app";
|
||||
import { OOORepository as PrismaOOORepository } from "@/lib/repositories/PrismaOOORepository";
|
||||
import { ScheduleRepository as PrismaScheduleRepository } from "@/lib/repositories/PrismaScheduleRepository";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||
import { Module, Logger } from "@nestjs/common";
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
import { SentryModule } from "@sentry/nestjs/setup";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
SentryModule.forRoot(),
|
||||
ConfigModule.forRoot({
|
||||
ignoreEnvFile: true,
|
||||
isGlobal: true,
|
||||
load: [appConfig],
|
||||
}),
|
||||
PrismaModule,
|
||||
],
|
||||
providers: [Logger, PrismaOOORepository, PrismaScheduleRepository, AvailableSlotsService],
|
||||
})
|
||||
export class SlotsWorkerModule {}
|
||||
@@ -1,19 +1,65 @@
|
||||
import { parentPort } from "worker_threads";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { Logger } from "@nestjs/common";
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { IncomingMessage } from "http";
|
||||
import { parentPort, isMainThread } from "worker_threads";
|
||||
|
||||
import { AvailableSlotsService } from "@calcom/platform-libraries/slots";
|
||||
import { SlotsWorkerModule } from "./slots.worker.module";
|
||||
|
||||
const availableSlotsService = new AvailableSlotsService();
|
||||
interface ContextForGetSchedule extends Record<string, unknown> {
|
||||
req?: (IncomingMessage & { cookies: Partial<{ [key: string]: string }> }) | undefined;
|
||||
}
|
||||
|
||||
parentPort?.on("message", async (data) => {
|
||||
try {
|
||||
const { input, ctx } = data;
|
||||
interface WorkerMessage {
|
||||
input: any; // Use a more specific type if available, e.g., TGetScheduleInputSchema
|
||||
ctx?: ContextForGetSchedule;
|
||||
}
|
||||
|
||||
const result = await availableSlotsService.getAvailableSlots({ input, ctx });
|
||||
parentPort?.postMessage({ success: true, data: result });
|
||||
} catch (error) {
|
||||
parentPort?.postMessage({
|
||||
success: false,
|
||||
error: error instanceof Error ? error : new Error("Unknown error"),
|
||||
});
|
||||
interface WorkerResult {
|
||||
success: boolean;
|
||||
data?: any; // Use TimeSlots
|
||||
error?: {
|
||||
message: string;
|
||||
code?: string;
|
||||
stack?: string;
|
||||
};
|
||||
}
|
||||
|
||||
async function bootstrapWorkerApp() {
|
||||
if (isMainThread) {
|
||||
console.error("slots-worker-main.ts should only be run in a worker thread.");
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
const app = await NestFactory.createApplicationContext(SlotsWorkerModule);
|
||||
const logger = app.get(Logger); // Get the NestJS Logger instance
|
||||
logger.log("NestJS worker application bootstrapped successfully.");
|
||||
|
||||
const availableSlotsService = app.get(AvailableSlotsService);
|
||||
|
||||
parentPort?.on("message", async (data: WorkerMessage) => {
|
||||
try {
|
||||
const { input, ctx } = data;
|
||||
|
||||
const result = await availableSlotsService.getAvailableSlots({ input, ctx });
|
||||
parentPort?.postMessage({ success: true, data: result } as WorkerResult);
|
||||
} catch (error: any) {
|
||||
parentPort?.postMessage({
|
||||
success: false,
|
||||
error: error instanceof Error ? error : new Error("Unknown error"),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
parentPort?.on("close", async () => {
|
||||
logger.log("Worker port closed. Shutting down NestJS worker application.");
|
||||
await app.close();
|
||||
});
|
||||
|
||||
parentPort?.on("disconnect", async () => {
|
||||
logger.log("Worker port disconnected. Shutting down NestJS worker application.");
|
||||
await app.close();
|
||||
});
|
||||
}
|
||||
|
||||
bootstrapWorkerApp();
|
||||
@@ -1,4 +1,5 @@
|
||||
import { EventTypesRepository_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/event-types.repository";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { MembershipsRepository } from "@/modules/memberships/memberships.repository";
|
||||
import { MembershipsService } from "@/modules/memberships/services/memberships.service";
|
||||
import { TimeSlots } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
@@ -17,7 +18,6 @@ import {
|
||||
import { DateTime } from "luxon";
|
||||
import { z } from "zod";
|
||||
|
||||
import { AvailableSlotsService } from "@calcom/platform-libraries/slots";
|
||||
import { GetSlotsInput_2024_09_04, ReserveSlotInput_2024_09_04 } from "@calcom/platform-types";
|
||||
import { Booking, EventType } from "@calcom/prisma/client";
|
||||
|
||||
@@ -31,8 +31,6 @@ const DEFAULT_RESERVATION_DURATION = 5;
|
||||
|
||||
@Injectable()
|
||||
export class SlotsService_2024_09_04 {
|
||||
private readonly availableSlotsService: AvailableSlotsService;
|
||||
|
||||
constructor(
|
||||
private readonly eventTypeRepository: EventTypesRepository_2024_06_14,
|
||||
private readonly slotsRepository: SlotsRepository_2024_09_04,
|
||||
@@ -40,10 +38,9 @@ export class SlotsService_2024_09_04 {
|
||||
private readonly slotsInputService: SlotsInputService_2024_09_04,
|
||||
private readonly membershipsService: MembershipsService,
|
||||
private readonly membershipsRepository: MembershipsRepository,
|
||||
private readonly teamsRepository: TeamsRepository
|
||||
) {
|
||||
this.availableSlotsService = new AvailableSlotsService();
|
||||
}
|
||||
private readonly teamsRepository: TeamsRepository,
|
||||
private readonly availableSlotsService: AvailableSlotsService
|
||||
) {}
|
||||
|
||||
async getAvailableSlots(query: GetSlotsInput_2024_09_04) {
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { EventTypesModule_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/event-types.module";
|
||||
import { OOORepository as PrismaOOORepository } from "@/lib/repositories/PrismaOOORepository";
|
||||
import { ScheduleRepository as PrismaScheduleRepository } from "@/lib/repositories/PrismaScheduleRepository";
|
||||
import { AvailableSlotsService } from "@/lib/services/AvailableSlots";
|
||||
import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
||||
import { OrganizationsRepository } from "@/modules/organizations/index/organizations.repository";
|
||||
import { OrganizationsTeamsRepository } from "@/modules/organizations/teams/index/organizations-teams.repository";
|
||||
@@ -33,6 +36,9 @@ import { Module } from "@nestjs/common";
|
||||
OrganizationsUsersRepository,
|
||||
OrganizationsRepository,
|
||||
OrganizationsTeamsRepository,
|
||||
PrismaOOORepository,
|
||||
PrismaScheduleRepository,
|
||||
AvailableSlotsService,
|
||||
],
|
||||
controllers: [SlotsController_2024_09_04],
|
||||
exports: [SlotsService_2024_09_04],
|
||||
|
||||
@@ -25,7 +25,8 @@
|
||||
"@calcom/platform-libraries/schedules": ["../../../packages/platform/libraries/schedules.ts"],
|
||||
"@calcom/platform-libraries/app-store": ["../../../packages/platform/libraries/app-store.ts"],
|
||||
"@calcom/platform-libraries/workflows": ["../../../packages/platform/libraries/workflows.ts"],
|
||||
"@calcom/platform-libraries/conferencing": ["../../../packages/platform/libraries/conferencing.ts"]
|
||||
"@calcom/platform-libraries/conferencing": ["../../../packages/platform/libraries/conferencing.ts"],
|
||||
"@calcom/platform-libraries/repositories": ["../../../packages/platform/libraries/repositories.ts"]
|
||||
},
|
||||
"incremental": true,
|
||||
"skipLibCheck": true,
|
||||
|
||||
Reference in new issue
Block a user