import { evaluateEntitlement, type EntitlementDecision, type EntitlementLimitType } from "./evaluate.js"; import { type FeatureKey } from "./features.js"; import { getProductBlueprint, type ProductBlueprintId } from "./blueprints.js"; export type TenantFeatureOverride = "enabled" | "disabled" | "inherit"; export interface ProductAccessInput { readonly blueprintId: ProductBlueprintId | string; readonly featureKey: FeatureKey | string; readonly hasSubscription: boolean; readonly limitType?: EntitlementLimitType; readonly limit?: number | null; readonly used?: number | null; readonly tenantOverride?: TenantFeatureOverride; readonly tenantRolePermissionNames?: readonly string[]; readonly period?: string; } export type ProductAccessDecisionReason = | EntitlementDecision["reason"] | "blueprint-disabled" | "tenant-override-disabled" | "tenant-override-not-toggleable" | "tenant-role-missing"; export interface ProductAccessDecision extends Omit { readonly reason: ProductAccessDecisionReason; readonly blueprintId: string; readonly tenantOverride: TenantFeatureOverride; readonly requiredTenantRolePermission?: string; } export function evaluateProductAccess(input: ProductAccessInput): ProductAccessDecision { const blueprint = getProductBlueprint(input.blueprintId); const blueprintFeature = blueprint?.features.find((feature) => feature.featureKey === input.featureKey); const tenantOverride = input.tenantOverride ?? "inherit"; const base = { blueprintId: input.blueprintId, featureKey: input.featureKey, tenantOverride, }; if (!blueprint || !blueprintFeature?.defaultEnabled) { return { ...base, allowed: false, reason: "blueprint-disabled", period: input.period, }; } if (tenantOverride === "disabled") { return { ...base, allowed: false, reason: "tenant-override-disabled", period: input.period, }; } // A non-toggleable feature cannot be self-enabled by a tenant override — EXCEPT for // core modules. Fundament D guarantees every tenant already receives each core module // (`includedInCore`), so a redundant "enabled" override on a non-toggleable core module // (e.g. dashboard, crm-basic, users) is a harmless no-op, not a denial. Real add-ons // (`includedInCore: false`, e.g. Email-Marketing credits) keep the not-toggleable // rejection: a tenant must not self-enable an add-on it did not purchase. if (tenantOverride === "enabled" && !blueprintFeature.tenantToggleable && blueprintFeature.includedInCore !== true) { return { ...base, allowed: false, reason: "tenant-override-not-toggleable", period: input.period, }; } const entitlement = evaluateEntitlement({ featureKey: input.featureKey, hasSubscription: input.hasSubscription, limitType: input.limitType, limit: input.limit, used: input.used, period: input.period, }); // Fundament D: core modules are always included. A tenant without an active // subscription (or on a plan that lists the feature as not-included/exhausted) still // gets full access to every core module — the plan-gate is removed for core. Only // real add-ons (Email-Marketing) keep the subscription plan-gate and fall through to // the denied passthrough below. The tenant role permission (RBAC) check further down // is NOT a plan-gate: it stays enforced for core and add-on features alike. const includedInCore = blueprintFeature.includedInCore === true; const entitlementAllowed = entitlement.allowed || includedInCore; if (!entitlementAllowed && tenantOverride !== "enabled") { return { ...entitlement, blueprintId: input.blueprintId, tenantOverride }; } const requiredPermission = blueprintFeature.requiresTenantRolePermission; const hasRolePermission = !requiredPermission || input.tenantRolePermissionNames?.includes(requiredPermission); if (!hasRolePermission) { return { ...entitlement, blueprintId: input.blueprintId, tenantOverride, allowed: false, reason: "tenant-role-missing", requiredTenantRolePermission: requiredPermission, }; } return { ...entitlement, blueprintId: input.blueprintId, tenantOverride, allowed: true, // Access rescued by core-inclusion or an explicit enabled override: the underlying // entitlement reason is a denial, so surface a clean "included" instead. reason: entitlement.allowed ? entitlement.reason : "included", requiredTenantRolePermission: requiredPermission, }; }