refactor: replace FeaturesRepository with DI-based feature repositories (#27200)
* refactor: replace FeaturesRepository with DI-based feature repositories Migrate from the legacy FeaturesRepository pattern to separate DI-based repositories: - Add checkIfFeatureIsEnabledGlobally to IFeatureRepository - Add getTeamsWithFeatureEnabled to ITeamFeatureRepository - Update CalendarSubscriptionService to use featureRepository, teamFeatureRepository, and userFeatureRepository - Update SelectedCalendarRepository.findNextSubscriptionBatch to filter by teamIds instead of featureIds - Update OnboardingPathService to use DI via getFeatureRepository() - Remove prisma parameter from OnboardingPathService callsites Co-Authored-By: Claude Opus 4.5 <[email protected]> * test: update tests to use new DI-based feature repositories Update CalendarSubscriptionService and SelectedCalendarRepository tests to use the new separate repository interfaces: - featureRepository for global feature checks - teamFeatureRepository for team-level feature checks - userFeatureRepository for user-level feature checks Co-Authored-By: Claude Opus 4.5 <[email protected]> * fix: update API routes to use DI-based feature repositories Update cron and webhook routes to use the new separate repository interfaces instead of the combined FeaturesRepository. Co-Authored-By: Claude Opus 4.5 <[email protected]> * fix: remove prisma arg from getGettingStartedPathWithParams call The OnboardingPathService method no longer requires a prisma argument as it now uses DI containers internally. Co-Authored-By: Claude Opus 4.5 <[email protected]> * test: update route tests to expect new DI-based feature repositories Update service instantiation tests to expect featureRepository, teamFeatureRepository, and userFeatureRepository instead of the old featuresRepository. Co-Authored-By: Claude Opus 4.5 <[email protected]> * fix: optimize global feature check and add guard in checkForNewSubscriptions - Use targeted select for only `enabled` field in checkIfFeatureIsEnabledGlobally - Add global feature flag guard in checkForNewSubscriptions to avoid unnecessary DB queries and API calls when the cache feature is globally disabled Co-Authored-By: Claude Opus 4.5 <[email protected]> * remove redundant comment --------- Co-authored-by: Claude Opus 4.5 <[email protected]>
This commit is contained in:
1 parent
cb36fc201f
commit
512002aaef
21 files changed
+265
-285
No files matched your search
@@ -1,9 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { Memoize, Unmemoize } from "@calcom/features/cache";
|
||||
import type { TeamFeaturesDto } from "@calcom/lib/dto/TeamFeaturesDto";
|
||||
import { TeamFeaturesDtoSchema } from "@calcom/lib/dto/TeamFeaturesDto";
|
||||
|
||||
import { z } from "zod";
|
||||
import type { FeatureId, TeamFeatures } from "../config";
|
||||
import type { ITeamFeatureRepository } from "./PrismaTeamFeatureRepository";
|
||||
import { booleanSchema } from "./schemas";
|
||||
@@ -91,4 +89,8 @@ export class CachedTeamFeatureRepository implements ITeamFeatureRepository {
|
||||
async getEnabledFeatures(teamId: number): Promise<TeamFeatures | null> {
|
||||
return this.prismaTeamFeatureRepository.getEnabledFeatures(teamId);
|
||||
}
|
||||
|
||||
async getTeamsWithFeatureEnabled(featureId: FeatureId): Promise<number[]> {
|
||||
return this.prismaTeamFeatureRepository.getTeamsWithFeatureEnabled(featureId);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ export interface IFeatureRepository {
|
||||
findAll(): Promise<FeatureDto[]>;
|
||||
findBySlug(slug: string): Promise<FeatureDto | null>;
|
||||
update(input: { featureId: FeatureId; enabled: boolean; updatedBy?: number }): Promise<FeatureDto>;
|
||||
checkIfFeatureIsEnabledGlobally(slug: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
export class PrismaFeatureRepository implements IFeatureRepository {
|
||||
@@ -67,4 +68,12 @@ export class PrismaFeatureRepository implements IFeatureRepository {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async checkIfFeatureIsEnabledGlobally(slug: string): Promise<boolean> {
|
||||
const feature = await this.prisma.feature.findUnique({
|
||||
where: { slug },
|
||||
select: { enabled: true },
|
||||
});
|
||||
return Boolean(feature?.enabled);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
import { captureException } from "@sentry/nextjs";
|
||||
|
||||
import type { TeamFeaturesDto } from "@calcom/lib/dto/TeamFeaturesDto";
|
||||
import type { PrismaClient } from "@calcom/prisma/client";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
import { captureException } from "@sentry/nextjs";
|
||||
import type { FeatureId, TeamFeatures } from "../config";
|
||||
|
||||
export interface ITeamFeatureRepository {
|
||||
@@ -24,6 +22,7 @@ export interface ITeamFeatureRepository {
|
||||
setAutoOptIn(teamId: number, enabled: boolean): Promise<void>;
|
||||
checkIfTeamHasFeature(teamId: number, featureId: FeatureId): Promise<boolean>;
|
||||
getEnabledFeatures(teamId: number): Promise<TeamFeatures | null>;
|
||||
getTeamsWithFeatureEnabled(featureId: FeatureId): Promise<number[]>;
|
||||
}
|
||||
|
||||
export class PrismaTeamFeatureRepository implements ITeamFeatureRepository {
|
||||
@@ -250,4 +249,18 @@ export class PrismaTeamFeatureRepository implements ITeamFeatureRepository {
|
||||
|
||||
return features;
|
||||
}
|
||||
|
||||
async getTeamsWithFeatureEnabled(featureId: FeatureId): Promise<number[]> {
|
||||
const result = await this.prisma.teamFeatures.findMany({
|
||||
where: {
|
||||
featureId,
|
||||
enabled: true,
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
|
||||
return result.map((tf) => tf.teamId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user