Skip to content

Commit af4ab2f

Browse files
fix(utils): assign per-adjustment subtotal/total instead of cumulative sum (#16013)
## Summary **What** — Fixes `calculateAdjustmentTotal` in `packages/core/utils/src/totals/adjustment/index.ts` so each adjustment's `subtotal`/`total` is set from its own per-adjustment value instead of the running cumulative sum. **Why** — The loop assigned the plural running accumulators (`adjustmentsSubtotal`/`adjustmentsTotal`) onto each adjustment's own `subtotal`/`total`. Every adjustment after the first was inflated to the cumulative total. The bug is masked when a line has a single adjustment. The sibling `calculateCreditLinesTotal` (credit-lines/index.ts) already does this correctly using the per-item value, confirming intent. Aggregate return values were unaffected — only the per-adjustment object mutation was wrong. **How** — Changed the two assignments to use the singular locals `adjustmentSubtotal` / `adjustmentTotal` computed earlier in the same loop iteration. **Testing** — Added `packages/core/utils/src/totals/adjustment/__tests__/index.spec.ts` (no-tax and with-tax cases, asserting per-adjustment values are not cumulative). `yarn workspace @medusajs/utils test adjustment` passes. --- ## Examples ```ts import { calculateAdjustmentTotal } from "@medusajs/utils" const adjustments = [{ amount: 10 }, { amount: 20 }, { amount: 5 }] calculateAdjustmentTotal({ adjustments }) adjustments.map((a) => Number(a.subtotal)) // before: [10, 30, 35] after: [10, 20, 5]
1 parent ffe9f4c commit af4ab2f

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@medusajs/utils": patch
3+
---
4+
5+
fix(utils): assign per-adjustment subtotal/total instead of the cumulative running sum
6+
7+
`calculateAdjustmentTotal` wrote the plural running accumulators (`adjustmentsSubtotal`/`adjustmentsTotal`) onto each adjustment's own `subtotal`/`total`, instead of the singular per-adjustment values. Every adjustment after the first was inflated to the cumulative total; the bug was masked for lines with a single adjustment. The aggregate return values were unaffected.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { calculateAdjustmentTotal } from "../index"
2+
3+
describe("calculateAdjustmentTotal", function () {
4+
it("assigns each adjustment its own subtotal/total, not the running cumulative sum", function () {
5+
const adjustments: any[] = [{ amount: 10 }, { amount: 20 }, { amount: 5 }]
6+
7+
const result = calculateAdjustmentTotal({ adjustments })
8+
9+
// Per-adjustment fields must reflect each adjustment individually.
10+
expect(Number(adjustments[0].subtotal)).toEqual(10)
11+
expect(Number(adjustments[0].total)).toEqual(10)
12+
// Regression: these were 30 and 35 (cumulative) before the fix.
13+
expect(Number(adjustments[1].subtotal)).toEqual(20)
14+
expect(Number(adjustments[1].total)).toEqual(20)
15+
expect(Number(adjustments[2].subtotal)).toEqual(5)
16+
expect(Number(adjustments[2].total)).toEqual(5)
17+
18+
// Aggregate return values remain the sum across all adjustments.
19+
expect(Number(result.adjustmentsSubtotal)).toEqual(35)
20+
expect(Number(result.adjustmentsTotal)).toEqual(35)
21+
})
22+
23+
it("applies tax per adjustment when a taxRate is provided", function () {
24+
const adjustments: any[] = [
25+
{ amount: 100, is_tax_inclusive: false },
26+
{ amount: 200, is_tax_inclusive: false },
27+
]
28+
29+
calculateAdjustmentTotal({ adjustments, taxRate: 0.1 })
30+
31+
// subtotal is the tax-exclusive amount; total includes 10% tax — per adjustment.
32+
expect(Number(adjustments[0].subtotal)).toEqual(100)
33+
expect(Number(adjustments[0].total)).toEqual(110)
34+
expect(Number(adjustments[1].subtotal)).toEqual(200)
35+
expect(Number(adjustments[1].total)).toEqual(220)
36+
})
37+
})

packages/core/utils/src/totals/adjustment/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ export function calculateAdjustmentTotal({
3838
adjustmentsTaxTotal = MathBN.add(adjustmentsTaxTotal, adjustmentTaxTotal)
3939
adjustmentsTotal = MathBN.add(adjustmentsTotal, adjustmentTotal)
4040

41-
adj["subtotal"] = new BigNumber(adjustmentsSubtotal)
42-
adj["total"] = new BigNumber(adjustmentsTotal)
41+
// Assign this adjustment's OWN subtotal/total — not the running cumulative
42+
// sum across all adjustments. Using the plural accumulators here inflated
43+
// every adjustment after the first to the cumulative total.
44+
adj["subtotal"] = new BigNumber(adjustmentSubtotal)
45+
adj["total"] = new BigNumber(adjustmentTotal)
4346
}
4447

4548
const quantity = item?.quantity || MathBN.convert(1)

0 commit comments

Comments
 (0)