PolyWood Price Builder

Pricing and Widgets

Rule Step Shape

src/lib/widgets.ts defines:

  • RuleStep: { id, widgetId, config }
  • WidgetManifest: widget metadata, configurable fields, and engine definition.
  • WidgetField: number or select field metadata used by UI and validation.

Default Widget Catalog

DEFAULT_WIDGETS includes:

  • price.addPercent
  • price.margin
  • price.round9
  • price.round5

Current default engines:

  • price.addPercent -> percentAdjust
  • price.margin -> marginFromCost
  • price.round9 -> roundNearest9
  • price.round5 -> roundNearest5

Engine Execution

src/lib/rules.ts:

  • applyStep(input, step, widgetsById) resolves widget engine and applies one transformation.
  • applyPipeline(start, steps, widgetsById) applies all steps in order and returns intermediate outputs plus final value.
  • money(n) formats USD display strings.

Engine Rules

  • Unknown widget ID: step is ignored (input passes through).
  • Numeric config values are read using the engine's configured key names.
  • marginFromCost computes input / (1 - percent/100) and safely passes through when denominator is <= 0.
  • roundNearest9 rounds to the nearest whole-dollar value ending in 9.
  • roundNearest5 rounds to the nearest multiple of 5.

Widget Helpers

src/lib/widgets.ts also provides:

  • createStepFromWidget(widget, id) for generating step defaults from widget field defaults.
  • mapWidgetsById(widgets) for building a lookup map used by execution and UI.

Tier Pricing Integration

  • Tier policy parsing and calculation live in src/lib/tier-pricing-policy.ts.
  • Products and builder previews combine:
    • pipeline final output from applyPipeline
    • tier derivation from computeTierPrices
  • See _docs/tier-pricing-policy.md for full tier model details.

Key Separation of Concerns

  • Widget schemas/defaults/helpers: src/lib/widgets.ts
  • Runtime step math: src/lib/rules.ts
  • Tier math and policy parsing: src/lib/tier-pricing-policy.ts
  • Step and tier validation when persisting rulesets: src/lib/s3-rulesets.ts