Skip to content

fix(shop): the cart refuses quantities it can't fulfil (#215) - #224

Merged
thunpisit merged 2 commits into
mainfrom
fix/cart-stock-gate
Oct 25, 2026
Merged

thunpisit merged 2 commits into
mainfrom
fix/cart-stock-gate

Conversation

@thunpisit

Copy link
Copy Markdown
Contributor

Closes #215.

Bug

POST /api/shop/cart never read inventory; PATCH only capped at 999. Out-of-stock variants could be added and lines raised past stock (curl, stale tab, or a line in stock when added). The customer paid first and the merchant discovered the shortfall later.

Fix

  • CartService.addItem / setQuantity check the line's final quantity against new sellableQuantity() (bundles.ts). It uses the same rules as checkout's reservation: on_hand - reserved at default; untracked / continue-selling / row-less are unlimited; a bundle is limited by its scarcest component.
  • Final quantity, not the amount added: the downstream bactrack fix checked only the delta, so five "+1" calls oversold a stock of three. That case is pinned here.
  • Lowering a line is always allowed, even when stock has dropped below it.
  • API: 409 OUT_OF_STOCK with available (+ cartItemId); other cart errors unchanged (400).
  • Product and cart pages show Only N in stock / Sold out in the visitor's language. The cart page used to swallow PATCH failures silently.

Unlike bactrack's version, this respects continue selling when out of stock (pre-orders) and checks bundles.

Tests

6 integration tests through CartService + real migrations: sold out; +1×N bypass; other shoppers' reservations; raise refused / lower allowed; untracked and continue-selling unlimited; bundle by scarcest component.
Gate: lint 0 errors · check 0 · 1157 tests on this branch alone, merged with main's #223 as well · build · contract-guard · css-inventory +0.

🤖 Generated with Claude Code

thunpisit and others added 2 commits October 25, 2026 19:18
POST /api/shop/cart never read inventory, and PATCH only capped at
999, so an out-of-stock variant could be added and any line raised past
stock — via curl, a stale tab, or a line that was in stock when added.
The customer paid first and the merchant found the shortfall after.

CartService.addItem and setQuantity now check the line's FINAL quantity
against sellableQuantity() — the same rules checkout's reservation
uses: on_hand - reserved at the default location; untracked, continue-
selling and row-less variants unlimited; a bundle limited by its
scarcest component. Checking the final quantity (not the amount added)
closes the "+1 five times" bypass in the downstream bactrack fix.
Lowering a line is always allowed.

The API answers 409 OUT_OF_STOCK with `available`; the product and
cart pages show "Only N in stock" / "Sold out" in the visitor's
language instead of the server's English.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thunpisit
thunpisit merged commit 40eb64b into main Oct 25, 2026
2 checks passed
@thunpisit thunpisit mentioned this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cart API oversells: POST and PATCH both accept out-of-stock and above-stock quantities

1 participant