Commit 11d58f7
fix(cart): fetch totals with all fields + fix inconsistent total calculation (#16022)
Note: this should be merged after #16021 to use the shared `normalizeBigNumbers` utility
## Fix 1: Return totals when * fields are selected
Previously, we only returned totals when they're explicitely selected as the computation is expensive. However, this leads to unexpected results when a user passes `*` in the fields. So the following wouldn't work as expected:
```ts
// doesn't return totals
const { data: carts } = await query.graph({
entity: "cart",
fields: ["*"]
})
// also doesn't return total. We strip all fields at the query layer when * is present
const { data: carts } = await query.graph({
entity: "cart",
fields: ["*", "total"]
})
// this works
const { data: carts } = await query.graph({
entity: "cart",
fields: ["total", "id"]
})
```
With the change in this PR, the totals will be returned as expected with `*`. This is fair as `*` by generally expensive and discouraged in the docs, and it matches the expectations of a user using it.
### Caveat
All retrievals of `*` fields or not passing `select` to `listOrders` (and similar methods) will result in totals retrieval, since those will retrieve all order fields. Need to be documented and included as part of the next release notes. To avoid retrieving them, users must pass either an empty array (retrieving no fields), or specify specific fields to retrieve.
## Fix 2: Total miscalculations based on retrieved items
In some use cases when a retrieving a specific field of cart's items without retrieving the quantity, the calculated totals are miscalculated. For example:
```ts
// totals are calculated
const { data: carts } = await query.graph({
entity: "cart",
fields: ["total", "items.quantity"]
})
// totals are miscalculated as there's no quantity on the item
const { data: carts } = await query.graph({
entity: "cart",
fields: ["total", "items.unit_price"]
})
```
This is unexpected behavior, the total shouldn't be impacted by which fields the user selects.
The solution in this PR has a caveat, which is that the quantity of items is now always returned when totals are returned even if not explicitely selected.
Co-authored-by: Nicolas Gorga <62995075+NicolasGorga@users.noreply.github.com>1 parent 4f2b8a2 commit 11d58f7
5 files changed
Lines changed: 201 additions & 33 deletions
File tree
- .changeset
- integration-tests
- http/__tests__/cart
- modules/__tests__/cart/store
- packages/modules/cart
- integration-tests/__tests__/services/cart-module
- src/services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 153 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 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
0 commit comments