Tier Pricing Policy
Scope
Tier pricing logic is implemented in src/lib/tier-pricing-policy.ts and integrated into:
- ruleset persistence (
src/lib/s3-rulesets.ts) - ruleset builder UI (
src/app/ruleset-builder/page.tsx) - product table preview (
src/app/page.tsx)
Core Model
- Tier IDs:
T1,T2,T3,T4,T5 - Configurable targets:
T2-T5(T1is derived from pipeline output) - Rule shape:
target: one ofT2-T5dependsOn: one ofT1-T5operator:addPercentorpercentOfvalue: finite number
- Policy shape:
{ rules: TierFormulaRule[] }
Default Policy
Default rules are:
T2 = T1 + 7.5%T3 = T1 + 15%T5 = T1 + 50%T4 = 93% of T5
Validation and Parsing
parseTierPricingPolicyInput (strict API input):
- policy must be an object
rulesmust be an array- each rule must have valid
target,dependsOn,operator, and numericvalue - duplicate
targetrules are rejected - rules for all derived tiers (
T2,T3,T4,T5) are required
parseTierPricingPolicyFromStorage (lenient S3 read):
null/undefinedpolicy falls back to default policy- partial stored rules are accepted
- missing derived targets are filled with defaults
- duplicate targets still fail parsing
Computation
computeTierPrices(basePipelineOutput, policy):
- sets
T1from pipeline final output - resolves other tiers recursively from dependency graph
- supports chained dependencies (for example,
T4depending onT5) - detects and reports:
- missing tier rules
- circular dependencies
- unavailable dependency values
- returns:
values:{ T1, T2, T3, T4, T5 }where unresolved tiers arenullerrors: non-fatal validation/computation messages for UI display
Ruleset API and Storage Integration
POST /api/rulesetsrequirestierPolicyand validates it viaparseTierPolicyInput.- Every stored ruleset version includes
tierPolicyin:<prefix>/<rulesetId>/versions/vNNNN.json<prefix>/<rulesetId>/latest.json
- Reads normalize policy with
parseTierPolicyFromStoragebefore returning data.
UI Integration
- Builder page edits formulas for
T2-T5and previews outputs/errors in real time. - Products page uses each product's assigned ruleset
latest.tierPolicyto compute displayed T1-T5 values.