Deviopath › TypeScript

Order Pipeline with Result

Hard · Discriminated Unions

Fill in the Result helpers and use them in processOrders(lines, catalog, stock). Prices are cents. A line is "<sku> <qty>" or "<sku> <qty> <coupon>" (extra spaces allowed). Stop at the first failing step:

1. BAD_FORMAT: not 2 or 3 parts, or qty is not a positive integer made of digits.

2. UNKNOWN_SKU: sku is not an own key of catalog.

3. OUT_OF_STOCK: current stock of sku below qty.

4. Coupon on subtotal = price * qty: SAVE10 subtracts Math.floor(subtotal / 10); HALF subtracts Math.floor(subtotal / 2) but fails with COUPON_NOT_APPLICABLE if subtotal < 1000; others fail with BAD_COUPON.

Only a successful order reduces the stock. Return { results, revenue, stock }: "ok <sku> x<qty> = <total>" or "error <CODE>" per line, the sum of the totals, and the final stock as a new object.

Next: Decode Messages from unknown