Commit af4ab2f
authored
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
- .changeset
- packages/core/utils/src/totals/adjustment
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
Lines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| |||
0 commit comments