Files
cfw-autumn/shared/api/customers/utils/apiCustomerToFeatures.ts
2026-02-18 18:46:19 +00:00

42 lines
1004 B
TypeScript

type ApiCustomerFeatureInput = {
id: string;
name?: string;
type?: string;
credit_schema?: Array<{
metered_feature_id: string;
credit_cost: number;
}>;
creditSchema?: Array<{
meteredFeatureId: string;
creditCost: number;
}>;
};
type ApiCustomerBalanceInput = {
feature?: ApiCustomerFeatureInput;
};
export type ApiCustomerWithBalancesInput = {
balances: Record<string, ApiCustomerBalanceInput>;
};
export const apiCustomerToFeatures = ({
apiCustomer,
}: {
apiCustomer: ApiCustomerWithBalancesInput;
}) => {
const balances = Object.values(apiCustomer.balances);
if (balances.length === 0) return [];
const firstBalance = balances[0];
if (!firstBalance.feature) {
throw new Error(
"[customerToFeatures] please expand `balances.feature` to get features for the customer",
);
}
return Object.values(apiCustomer.balances).map((balance) => {
return balance.feature;
}) as ApiCustomerFeatureInput[]; // safe to cast because we checked for feature in the first balance
};