PolyWood Price Builder

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 (T1 is derived from pipeline output)
  • Rule shape:
    • target: one of T2-T5
    • dependsOn: one of T1-T5
    • operator: addPercent or percentOf
    • value: 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
  • rules must be an array
  • each rule must have valid target, dependsOn, operator, and numeric value
  • duplicate target rules are rejected
  • rules for all derived tiers (T2, T3, T4, T5) are required

parseTierPricingPolicyFromStorage (lenient S3 read):

  • null/undefined policy 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 T1 from pipeline final output
  • resolves other tiers recursively from dependency graph
  • supports chained dependencies (for example, T4 depending on T5)
  • detects and reports:
    • missing tier rules
    • circular dependencies
    • unavailable dependency values
  • returns:
    • values: { T1, T2, T3, T4, T5 } where unresolved tiers are null
    • errors: non-fatal validation/computation messages for UI display

Ruleset API and Storage Integration

  • POST /api/rulesets requires tierPolicy and validates it via parseTierPolicyInput.
  • Every stored ruleset version includes tierPolicy in:
    • <prefix>/<rulesetId>/versions/vNNNN.json
    • <prefix>/<rulesetId>/latest.json
  • Reads normalize policy with parseTierPolicyFromStorage before returning data.

UI Integration

  • Builder page edits formulas for T2-T5 and previews outputs/errors in real time.
  • Products page uses each product's assigned ruleset latest.tierPolicy to compute displayed T1-T5 values.