Skip to main content

Reward Engine

packages/reward-engine/src/evaluate.ts is the canonical reward logic package.

What It Does​

The package is pure and deterministic:

  • no I/O
  • no database access
  • no side effects
  • the same inputs always produce the same result

Earn Calculation​

evaluateReward(input) performs the current earn logic:

  1. Reject amounts below minSpendMinorUnits.
  2. Convert minor units to dollars.
  3. Multiply by conversionRate.
  4. Apply the campaign multiplier.
  5. Cap at maxPointsPerTransaction.
  6. Apply the configured rounding policy.

Redemption Eligibility​

checkRedemptionEligibility(balance, cartAmountMinorUnits, rule) decides whether a redemption is allowed and how many points can be redeemed.

It enforces:

  • minimum balance to redeem
  • cart-value redemption cap

Stored Rule Shape​

The reward rule config stored in Postgres currently contains:

  • conversionRate
  • minSpendMinorUnits
  • maxPointsPerTransaction
  • minBalanceToRedeem
  • maxRedemptionPercentage
  • roundingPolicy

Operational Notes​

  • reward rules are versioned per tenant
  • only one rule version is active at a time
  • reward events link the transaction to the exact rule version used
  • ingestion passes the active earn-multiplier window, or 1 when no window is active
Written byDhruv Doshi