All files / src/pricing discount.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  4x   4x         41x 24x   17x 21x 1x 17x 17x    
import { DiscountGroup } from './types.js';
import { roundHalfEven } from './round.js';
 
export function applyDiscount(
  price: number,
  qty: number,
  group: DiscountGroup | null
): number {
  if (!group || group.discounts.length === 0) {
    return roundHalfEven(price, 3);
  }
  const applicable = group.discounts
    .filter((d) => d.lowerLimit <= qty)
    .sort((a, b) => b.lowerLimit - a.lowerLimit);
  const amount = applicable.length ? applicable[0].amount : 0;
  return roundHalfEven(price * (1 - amount / 100), 3);
}