Files
cfw-autumn/shared/utils/cusEntUtils/findCustomerEntitlement/findCustomerEntitlementByFeature.ts
John Yeo cb05d169a2 wip
2026-01-23 17:13:19 +00:00

71 lines
1.9 KiB
TypeScript

import { InternalError } from "@autumn/shared";
import type { FullCustomerEntitlement } from "@models/cusProductModels/cusEntModels/cusEntModels";
import type { FullCusEntWithFullCusProduct } from "@models/cusProductModels/cusEntModels/cusEntWithProduct";
import type { Feature } from "@models/featureModels/featureModels";
export function findCustomerEntitlementByFeature<
T extends FullCustomerEntitlement | FullCusEntWithFullCusProduct,
>(params: {
cusEnts: T[];
internalFeatureId?: string;
featureId?: string;
feature?: Feature;
errorOnNotFound: true;
}): T;
export function findCustomerEntitlementByFeature<
T extends FullCustomerEntitlement | FullCusEntWithFullCusProduct,
>(params: {
cusEnts: T[];
internalFeatureId?: string;
featureId?: string;
feature?: Feature;
errorOnNotFound?: false;
}): T | undefined;
export function findCustomerEntitlementByFeature<
T extends FullCustomerEntitlement | FullCusEntWithFullCusProduct,
>({
cusEnts,
internalFeatureId,
featureId,
feature,
errorOnNotFound = false,
}: {
cusEnts: T[];
internalFeatureId?: string;
featureId?: string;
feature?: Feature;
errorOnNotFound?: boolean;
}): T | undefined {
const cusEnt = cusEnts.find((ce) => {
if (internalFeatureId && ce.internal_feature_id === internalFeatureId) {
return true;
}
if (
featureId &&
(ce.entitlement.feature_id === featureId || ce.feature_id === featureId)
) {
return true;
}
if (
feature &&
(ce.entitlement.feature.id === feature.id ||
ce.entitlement.feature.internal_id === feature.internal_id)
) {
return true;
}
return false;
});
if (!cusEnt && errorOnNotFound) {
const identifier =
internalFeatureId || featureId || feature?.id || feature?.internal_id;
throw new InternalError({
message: `[findCustomerEntitlementByFeature] Customer entitlement not found for feature: ${identifier}`,
});
}
return cusEnt;
}