fix: entity on description / additional overrides on update subscription

This commit is contained in:
John Yeo
2026-04-28 16:56:18 +01:00
parent 19d68d38df
commit 0fc3f01ae1
12 changed files with 124 additions and 9 deletions

View File

@@ -0,0 +1,36 @@
import type { FeatureOptions } from "@models/cusProductModels/cusProductModels";
import type {
UsagePriceConfig,
UsageTier,
} from "@models/productModels/priceModels/priceConfig/usagePriceConfig";
import type { Price } from "@models/productModels/priceModels/priceModels";
import { findTierByQuantity } from "./findTierByQuantity";
/**
* Find the volume-tier the customer is currently on, given a prepaid `Price`
* and the matching `FeatureOptions` entry from `cusProduct.options`.
*
* Convention (Autumn prepaid):
* - `options.quantity` is in PACKS and EXCLUDES the entitlement allowance.
* - Tier `to` boundaries on the price are paid-only — they also EXCLUDE the
* allowance.
* - Lookup quantity = `options.quantity * billing_units` (paid credits).
*
* Returns the matching tier (or undefined when no tier covers the quantity —
* shouldn't happen if the tier list ends with `Infinite`).
*/
export const findTierByOptions = ({
price,
options,
}: {
price: Price;
options: FeatureOptions | undefined;
}): UsageTier | undefined => {
const config = price.config as UsagePriceConfig;
const billingUnits = config.billing_units ?? 1;
const paidQuantity = (options?.quantity ?? 0) * billingUnits;
return findTierByQuantity({
tiers: config.usage_tiers ?? [],
quantity: paidQuantity,
});
};

View File

@@ -0,0 +1,23 @@
import type { UsageTier } from "@models/productModels/priceModels/priceConfig/usagePriceConfig";
import { Infinite } from "@models/productModels/productEnums";
/**
* Find the volume-tier that covers the given quantity. Walks tiers in order
* and returns the first one whose `to` is ≥ quantity (or the final Infinite
* tier). Returns undefined only if no Infinite tier exists and quantity
* exceeds every bound — tier shapes are expected to end with `to: Infinite`,
* so this is rare.
*/
export const findTierByQuantity = ({
tiers,
quantity,
}: {
tiers: UsageTier[];
quantity: number;
}): UsageTier | undefined => {
for (const tier of tiers) {
if (tier.to === Infinite || tier.to === -1) return tier;
if (typeof tier.to === "number" && quantity <= tier.to) return tier;
}
return undefined;
};