perf: use app dir to fetch team feature flags in settings (#21433)

* use app dir to fetch team feature flags

* guard against type error if teamId does not exist in teamfeatures

* remove redundant router
This commit is contained in:
sean-brydon authored and GitHub committed 2025-05-21 12:41:45 +00:00
1 parent 4ff366cd99
commit 1dca160db0
5 files changed
+79 -19

No files matched your search

@@ -1,6 +1,7 @@
import { Prisma } from "@prisma/client";
import { captureException } from "@sentry/nextjs";
import kysely from "@calcom/kysely";
import db from "@calcom/prisma";
import type { AppFlags } from "./config";
@@ -58,6 +59,27 @@ export class FeaturesRepository implements IFeaturesRepository {
}, {} as AppFlags);
}
/**
* Gets all features enabled for a specific team in a map format.
* @param teamId - The ID of the team to get features for
* @returns Promise<{ [slug: string]: boolean } | null>
*/
public async getTeamFeatures(teamId: number) {
const result = await kysely
.selectFrom("TeamFeatures")
.innerJoin("Feature", "Feature.slug", "TeamFeatures.featureId")
.select(["Feature.slug", "Feature.enabled"])
.where("TeamFeatures.teamId", "=", teamId)
.execute();
if (!result.length) return null;
return result.reduce<Record<keyof AppFlags, boolean>>((acc, feature) => {
acc[feature.slug as keyof AppFlags] = true;
return acc;
}, {} as Record<keyof AppFlags, boolean>);
}
/**
* Checks if a feature is enabled globally in the application.
* @param slug - The feature flag identifier to check
@@ -0,0 +1,16 @@
import type { AppFlags } from "../config";
type TeamFeatures = Record<number, Record<keyof AppFlags, boolean>> | null;
export const useIsFeatureEnabledForTeam = ({
teamFeatures,
teamId,
feature,
}: {
teamFeatures?: TeamFeatures;
teamId?: number;
feature: keyof AppFlags;
}) => {
if (!teamId || !teamFeatures?.[teamId]) return false;
return teamFeatures[teamId][feature];
};
@@ -44,14 +44,6 @@ const checkDelegationCredentialFeature = async ({
};
export const delegationCredentialRouter = router({
check: authedOrgAdminProcedure.query(async (opts) => {
return await checkDelegationCredentialFeature({
ctx: opts.ctx,
next: async () => ({
hasDelegationCredential: true,
}),
});
}),
list: authedOrgAdminProcedure.use(checkDelegationCredentialFeature).query(async (opts) => {
const handler = await import("./list.handler").then((mod) => mod.default);
return handler(opts);