refactor: use dependency injection for InsightsRoutingService (#22677)
* refactor: use dependency injection for InsightsRoutingService * remove unused getter * clean up * Revert "clean up" This reverts commit a95b83d5dfd4c3236e44268df40bab935237101e. * Update insightsRoutingDI.ts * Revert "Update insightsRoutingDI.ts" This reverts commit 39ead809fa8dabfc17da043c6416bbc6d1fed58d. --------- Co-authored-by: Alex van Andel <me@alexvanandel.com>
This commit is contained in:
co-authored by
Alex van Andel
parent
f905ae7502
commit
4ff5f5f3fa
@@ -15,7 +15,7 @@ export function useInsightsOrgTeams() {
|
||||
const teamId = orgTeamsType === "org" ? currentOrgId : orgTeamsType === "team" ? selectedTeamId : undefined;
|
||||
const userId = orgTeamsType === "yours" ? session.data?.user.id : undefined;
|
||||
|
||||
// Adding `scope` for InsightsRoutingService
|
||||
// Adding `scope` for InsightsRoutingBaseService
|
||||
// (renaming 'yours' to 'user')
|
||||
const scope: "org" | "team" | "user" = orgTeamsType === "yours" ? "user" : orgTeamsType;
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import {
|
||||
routingRepositoryBaseInputSchema,
|
||||
bookingRepositoryBaseInputSchema,
|
||||
} from "@calcom/features/insights/server/raw-data.schema";
|
||||
import { getInsightsRoutingService } from "@calcom/lib/di/containers/insights-routing";
|
||||
import { InsightsBookingService } from "@calcom/lib/server/service/insightsBooking";
|
||||
import { InsightsRoutingService } from "@calcom/lib/server/service/insightsRouting";
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import authedProcedure from "@calcom/trpc/server/procedures/authedProcedure";
|
||||
@@ -1688,8 +1688,7 @@ export const insightsRouter = router({
|
||||
timeView,
|
||||
weekStart: ctx.user.weekStart,
|
||||
});
|
||||
const insightsRoutingService = new InsightsRoutingService({
|
||||
prisma: ctx.insightsDb,
|
||||
const insightsRoutingService = getInsightsRoutingService({
|
||||
options: {
|
||||
scope: input.scope,
|
||||
teamId: input.selectedTeamId,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { createContainer } from "@evyweb/ioctopus";
|
||||
|
||||
import { DI_TOKENS } from "@calcom/lib/di/tokens";
|
||||
import type {
|
||||
InsightsRoutingServicePublicOptions,
|
||||
InsightsRoutingServiceFilterOptions,
|
||||
InsightsRoutingBaseService,
|
||||
} from "@calcom/lib/server/service/insightsRoutingBase";
|
||||
import type { InsightsRoutingService } from "@calcom/lib/server/service/insightsRoutingDI";
|
||||
import { prismaModule } from "@calcom/prisma/prisma.module";
|
||||
|
||||
import { insightsRoutingModule } from "../modules/insights-routing";
|
||||
|
||||
export function getInsightsRoutingService({
|
||||
options,
|
||||
filters,
|
||||
}: {
|
||||
options: InsightsRoutingServicePublicOptions;
|
||||
filters: InsightsRoutingServiceFilterOptions;
|
||||
}): InsightsRoutingBaseService {
|
||||
const container = createContainer();
|
||||
container.load(DI_TOKENS.READ_ONLY_PRISMA_CLIENT, prismaModule);
|
||||
container.load(DI_TOKENS.INSIGHTS_ROUTING_SERVICE_MODULE, insightsRoutingModule);
|
||||
|
||||
const diService = container.get<InsightsRoutingService>(DI_TOKENS.INSIGHTS_ROUTING_SERVICE);
|
||||
return diService.create({ options, filters });
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { createModule } from "@evyweb/ioctopus";
|
||||
|
||||
import type { IInsightsRoutingService } from "@calcom/lib/server/service/insightsRoutingDI";
|
||||
import { InsightsRoutingService } from "@calcom/lib/server/service/insightsRoutingDI";
|
||||
|
||||
import { DI_TOKENS } from "../tokens";
|
||||
|
||||
export const insightsRoutingModule = createModule();
|
||||
insightsRoutingModule.bind(DI_TOKENS.INSIGHTS_ROUTING_SERVICE).toClass(InsightsRoutingService, {
|
||||
prisma: DI_TOKENS.READ_ONLY_PRISMA_CLIENT,
|
||||
} satisfies Record<keyof IInsightsRoutingService, symbol>);
|
||||
@@ -20,4 +20,6 @@ export const DI_TOKENS = {
|
||||
ROUTING_FORM_RESPONSE_REPOSITORY_MODULE: Symbol("RoutingFormResponseRepositoryModule"),
|
||||
AVAILABLE_SLOTS_SERVICE: Symbol("AvailableSlotsService"),
|
||||
AVAILABLE_SLOTS_SERVICE_MODULE: Symbol("AvailableSlotsModule"),
|
||||
INSIGHTS_ROUTING_SERVICE: Symbol("InsightsRoutingService"),
|
||||
INSIGHTS_ROUTING_SERVICE_MODULE: Symbol("InsightsRoutingServiceModule"),
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ import { ColumnFilterType } from "@calcom/features/data-table/lib/types";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { BookingStatus, MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
import { InsightsRoutingService } from "../../service/insightsRouting";
|
||||
import { InsightsRoutingBaseService as InsightsRoutingService } from "../../service/insightsRoutingBase";
|
||||
|
||||
// SQL condition constants for testing
|
||||
const NOTHING_CONDITION = Prisma.sql`1=0`;
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ export type InsightsRoutingServiceFilterOptions = {
|
||||
|
||||
const NOTHING_CONDITION = Prisma.sql`1=0`;
|
||||
|
||||
export class InsightsRoutingService {
|
||||
export class InsightsRoutingBaseService {
|
||||
private prisma: typeof readonlyPrisma;
|
||||
private options: InsightsRoutingServiceOptions | null;
|
||||
private filters: InsightsRoutingServiceFilterOptions;
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
|
||||
import {
|
||||
InsightsRoutingBaseService,
|
||||
type InsightsRoutingServicePublicOptions,
|
||||
type InsightsRoutingServiceFilterOptions,
|
||||
} from "./insightsRoutingBase";
|
||||
|
||||
export interface IInsightsRoutingService {
|
||||
prisma: typeof readonlyPrisma;
|
||||
}
|
||||
|
||||
export class InsightsRoutingService {
|
||||
constructor(private readonly dependencies: IInsightsRoutingService) {}
|
||||
|
||||
create({
|
||||
options,
|
||||
filters,
|
||||
}: {
|
||||
options: InsightsRoutingServicePublicOptions;
|
||||
filters: InsightsRoutingServiceFilterOptions;
|
||||
}): InsightsRoutingBaseService {
|
||||
return new InsightsRoutingBaseService({
|
||||
prisma: this.dependencies.prisma,
|
||||
options,
|
||||
filters,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user