-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfeature-flags.db.ts
More file actions
51 lines (45 loc) · 2.19 KB
/
Copy pathfeature-flags.db.ts
File metadata and controls
51 lines (45 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { prisma } from '@jetstream/api-config';
import { DEFAULT_FEATURE_FLAGS, FeatureFlagKey, FeatureFlags, isFeatureFlagKey } from '@jetstream/types';
/**
* Resolve the effective feature flags for a user.
*
* Starts from the code-defined defaults, then applies any DB overrides scoped to the user or their
* team. Resolution is "most permissive wins": a flag is enabled if the default OR any matching
* override is enabled. Override rows whose key is no longer defined in code are ignored (drift-safe).
* Always returns the full set of known flags.
*/
export async function resolveFeatureFlagsForUser({ userId, teamId }: { userId: string; teamId?: string | null }): Promise<FeatureFlags> {
const result: FeatureFlags = { ...DEFAULT_FEATURE_FLAGS };
const overrides = await prisma.featureFlagOverride.findMany({
where: { OR: [{ userId }, ...(teamId ? [{ teamId }] : [])] },
select: { key: true, enabled: true },
});
for (const { key, enabled } of overrides) {
if (isFeatureFlagKey(key)) {
result[key] = result[key] || enabled === true;
}
}
return result;
}
/**
* Authoritative server-side check for a single flag. Use this to enforce a gate on a dedicated API
* endpoint (mirrors `checkUserEntitlement`). Most of the UI hits generic endpoints, so this is only
* useful for the rare feature with its own server route.
*/
/**
* Canonical "active team" used for flag scoping: an ACTIVE membership in an ACTIVE team. The /api/me
* profile path (`findIdByUserIdUserFacing`) applies the same rule in-memory off its already-loaded
* membership, so the flags shown in the UI and the server-side gate always agree on the team.
*/
export async function resolveActiveTeamIdForUser(userId: string): Promise<string | null> {
const membership = await prisma.teamMember.findFirst({
select: { teamId: true },
where: { userId, status: 'ACTIVE', team: { status: 'ACTIVE' } },
});
return membership?.teamId ?? null;
}
export async function checkFeatureFlag({ userId, key }: { userId: string; key: FeatureFlagKey }): Promise<boolean> {
const teamId = await resolveActiveTeamIdForUser(userId);
const flags = await resolveFeatureFlagsForUser({ userId, teamId });
return flags[key];
}