152 lines
5.6 KiB
TypeScript
152 lines
5.6 KiB
TypeScript
import {
|
|
entitlementLimitTypes,
|
|
evaluateEntitlement,
|
|
evaluateProductAccess,
|
|
getDefaultProductBlueprint,
|
|
getStandardEntitlementPlan,
|
|
type EntitlementDecision,
|
|
type EntitlementLimitType,
|
|
type EvaluateEntitlementInput,
|
|
type ProductAccessDecision,
|
|
type ProductBlueprintId,
|
|
type TenantFeatureOverride,
|
|
} from "@hivegrid/entitlements";
|
|
import { DefaultFeatures } from "~/application/dtos/shared/DefaultFeatures";
|
|
import type { SubscriptionFeatureDto } from "~/application/dtos/subscriptions/SubscriptionFeatureDto";
|
|
import type { SubscriptionProductDto } from "~/application/dtos/subscriptions/SubscriptionProductDto";
|
|
import { SubscriptionFeatureLimitType } from "~/application/enums/subscriptions/SubscriptionFeatureLimitType";
|
|
import { t as i18nT } from "i18next";
|
|
|
|
export interface SubscriptionEntitlementTrialState {
|
|
readonly active: boolean;
|
|
readonly period?: string;
|
|
readonly message?: string;
|
|
}
|
|
|
|
export interface EvaluateSubscriptionEntitlementInput {
|
|
readonly featureKey: DefaultFeatures | string;
|
|
readonly subscription?: SubscriptionEntitlementSubscription | null;
|
|
readonly trial?: SubscriptionEntitlementTrialState | null;
|
|
readonly used?: number | null;
|
|
readonly period?: string;
|
|
readonly message?: string;
|
|
}
|
|
|
|
export interface EvaluateProductSubscriptionAccessInput extends EvaluateSubscriptionEntitlementInput {
|
|
readonly blueprintId?: ProductBlueprintId | string;
|
|
readonly tenantOverride?: TenantFeatureOverride;
|
|
readonly tenantRolePermissionNames?: readonly string[];
|
|
}
|
|
|
|
export interface SubscriptionEntitlementSubscription {
|
|
readonly products?: readonly SubscriptionEntitlementProduct[] | null;
|
|
}
|
|
|
|
export interface SubscriptionEntitlementProduct {
|
|
readonly features?: readonly (SubscriptionFeatureDto & { code?: string | null })[] | null;
|
|
readonly subscriptionProduct?: Pick<SubscriptionProductDto, "features"> | null;
|
|
readonly customEntitlementPlanId?: string | null;
|
|
}
|
|
|
|
const appLimitTypeToEntitlementLimitType = Object.freeze({
|
|
[SubscriptionFeatureLimitType.NOT_INCLUDED]: entitlementLimitTypes.notIncluded,
|
|
[SubscriptionFeatureLimitType.INCLUDED]: entitlementLimitTypes.included,
|
|
[SubscriptionFeatureLimitType.MONTHLY]: entitlementLimitTypes.monthly,
|
|
[SubscriptionFeatureLimitType.MAX]: entitlementLimitTypes.max,
|
|
[SubscriptionFeatureLimitType.UNLIMITED]: entitlementLimitTypes.unlimited,
|
|
} as const satisfies Record<SubscriptionFeatureLimitType, EntitlementLimitType>);
|
|
|
|
const defaultIncludedTrialFeature: SubscriptionFeatureDto = Object.freeze({
|
|
order: 0,
|
|
title: i18nT("entitlements.trial.access"),
|
|
name: DefaultFeatures.API,
|
|
type: SubscriptionFeatureLimitType.INCLUDED,
|
|
value: 0,
|
|
accumulate: false,
|
|
});
|
|
|
|
function getSubscriptionProductFeatures(product: SubscriptionEntitlementProduct): readonly (SubscriptionFeatureDto & { code?: string | null })[] {
|
|
return product.features ?? product.subscriptionProduct?.features ?? [];
|
|
}
|
|
|
|
function getCustomPlanFeature(product: SubscriptionEntitlementProduct, featureKey: string): SubscriptionFeatureDto | undefined {
|
|
if (!product.customEntitlementPlanId) {
|
|
return undefined;
|
|
}
|
|
const customPlan = getStandardEntitlementPlan(product.customEntitlementPlanId);
|
|
const assignment = customPlan?.features.find((feature) => feature.featureKey === featureKey);
|
|
if (!assignment) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
order: assignment.order,
|
|
title: assignment.label,
|
|
name: assignment.featureKey,
|
|
type: assignment.limitType,
|
|
value: assignment.limit ?? 0,
|
|
accumulate: assignment.limitType === entitlementLimitTypes.monthly,
|
|
};
|
|
}
|
|
|
|
function findSubscriptionFeature(
|
|
products: readonly SubscriptionEntitlementProduct[] | null | undefined,
|
|
featureKey: string
|
|
): SubscriptionFeatureDto | undefined {
|
|
for (const product of products ?? []) {
|
|
const customFeature = getCustomPlanFeature(product, featureKey);
|
|
if (customFeature) {
|
|
return customFeature;
|
|
}
|
|
const feature = getSubscriptionProductFeatures(product).find((item) => (item.code ?? item.name) === featureKey);
|
|
if (feature) {
|
|
return feature;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function toEvaluateEntitlementInput({
|
|
featureKey,
|
|
subscription,
|
|
trial,
|
|
used,
|
|
period,
|
|
message,
|
|
}: EvaluateSubscriptionEntitlementInput): EvaluateEntitlementInput {
|
|
const products = subscription?.products ?? [];
|
|
const hasSubscription = products.length > 0;
|
|
const feature = findSubscriptionFeature(products, featureKey) ?? (trial?.active === true ? defaultIncludedTrialFeature : undefined);
|
|
|
|
return {
|
|
featureKey,
|
|
hasSubscription,
|
|
trialActive: hasSubscription ? undefined : trial?.active,
|
|
trialPeriod: hasSubscription ? undefined : trial?.period,
|
|
trialMessage: hasSubscription ? undefined : trial?.message,
|
|
limitType: feature ? appLimitTypeToEntitlementLimitType[feature.type] : undefined,
|
|
limit: feature?.value,
|
|
used,
|
|
period,
|
|
message,
|
|
};
|
|
}
|
|
|
|
export function evaluateSubscriptionEntitlement(input: EvaluateSubscriptionEntitlementInput): EntitlementDecision {
|
|
return evaluateEntitlement(toEvaluateEntitlementInput(input));
|
|
}
|
|
|
|
export function evaluateProductSubscriptionAccess(input: EvaluateProductSubscriptionAccessInput): ProductAccessDecision {
|
|
const entitlementInput = toEvaluateEntitlementInput(input);
|
|
return evaluateProductAccess({
|
|
blueprintId: input.blueprintId ?? getDefaultProductBlueprint().id,
|
|
featureKey: entitlementInput.featureKey,
|
|
hasSubscription: entitlementInput.hasSubscription ?? false,
|
|
limitType: entitlementInput.limitType,
|
|
limit: entitlementInput.limit,
|
|
used: entitlementInput.used,
|
|
period: entitlementInput.period,
|
|
tenantOverride: input.tenantOverride ?? "inherit",
|
|
tenantRolePermissionNames: input.tenantRolePermissionNames,
|
|
});
|
|
}
|