Skip to content

fix(orders): make lifecycle and inventory transitions idempotent - #268

Closed
khaira777 wants to merge 5 commits into
mainfrom
fix/252-order-lifecycle-inventory
Closed

khaira777 wants to merge 5 commits into
mainfrom
fix/252-order-lifecycle-inventory

Conversation

@khaira777

@khaira777 khaira777 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #252

Exact Before-Fix Reproduction Numbers

  • Repeated Whole-Order Cancellation: Initial stock: 10. Qty ordered: 2 (Stock -> 8). First cancel: Stock -> 10. Second cancel (PATCH /api/orders/:id/status with { status: 'cancelled' }): Stock -> 12 (+2 unwanted inventory inflation).
  • Invalid Backward & Terminal State Transitions: Backward transitions (e.g. ready -> preparing, served -> preparing, served -> ready) and terminal status changes (e.g. cancelled -> preparing) returned HTTP 200 and mutated orders.status backward.
  • Restocking Voided Items / Void Adjustments: An order with 1 item in preparing status was voided (creating a voided item and a void_adjustment line). When the whole order was cancelled, stock increased from 11 -> 12, restocking an item whose ingredients were already cooked and consumed.

Verified Subclaim Matrix

Subclaim Reported Vulnerability Reproduction Verdict Empirical Evidence
Claim A Repeated whole-order cancel restores stock multiple times. CONFIRMED 2nd cancel incremented stock by item quantity again (8 -> 10 -> 12).
Claim B Invalid backward & terminal transitions allowed. CONFIRMED Reopened cancelled order to preparing & moved ready/served backward (HTTP 200).
Claim C & D Cancel restocks void_adjustment and voided items. CONFIRMED Order cancel queried all items, restocking voided items (11 -> 12).
Claim E Item cancel/restore lack idempotency and stock bounds checks. CONFIRMED Pending item cancel did not track state; restore did not verify stock.
Claim F State read outside transaction lock. CONFIRMED (HARDENED) Reading mutable state outside transaction was a structural weakness and was hardened by transaction-local re-reads.

Order Lifecycle Transition Matrix

In main/routes/orders.ts, order status transitions are strictly governed by an explicit state transition matrix:

  • pending: ['preparing', 'ready', 'served', 'completed', 'cancelled']
  • preparing: ['ready', 'served', 'completed', 'cancelled']
  • ready: ['served', 'completed', 'cancelled']
  • served: ['completed', 'cancelled']
  • completed: [] (terminal)
  • cancelled: [] (terminal)

Behavioral Rules:

  1. Same-state requests (status === currentOrder.status) are safe idempotent no-ops (HTTP 200 returning current order without repeating side effects).
  2. Backward transitions (e.g. ready -> preparing, served -> ready) and invalid terminal transitions are rejected with HTTP 400 (Cannot transition order status from '<current>' to '<target>').
  3. Monotonic forward shortcuts (e.g. pending -> ready, pending -> completed) and cancellation from any active state remain fully supported.

Final Inventory / Restock Rules for Item Statuses

On whole-order cancellation:

  • pending, preparing, ready, served, completed: Eligible for one-time inventory restoration if product.track_inventory is enabled and item has not already been cancelled/voided.
  • cancelled: Excluded from restocking (stock was already restored when item was individually cancelled).
  • voided: Excluded from restocking (ingredients were physically cooked/consumed in kitchen).
  • void_adjustment: Excluded from restocking (accounting adjustment line only).

On item cancellation & restoration:

  • PATCH /api/orders/:orderId/items/:itemId/cancel:
    • isInProgressVoid is determined strictly by currentItem.status (preparing / ready), NOT parent order status. An item that is still pending in a preparing parent order follows pending cancellation & restock contract (+1 stock, status = cancelled, no void_adjustment).
    • Pending item cancel restores stock exactly once; repeat calls are safe no-ops.
  • PATCH /api/orders/:orderId/items/:itemId/restore:
    • Only cancelled items can be restored.
    • Verifies stock availability (product.stock_quantity >= item.quantity), re-deducts stock, and marks item pending. If stock is insufficient, returns HTTP 400.

Files Changed

  • main/routes/orders.ts: Enforces VALID_TRANSITIONS matrix, re-reads order inside withTxn(), and filters restock candidates.
  • main/routes/index.ts: Strict currentItem.status void semantics, idempotent item cancel/restore with stock checks, and excluded voided items from auto-cancel restock.
  • package.json: Added test:issue-252 script and registered bash tests/run-test.sh npm run test:issue-252 in the main npm test chain.
  • tests/issue-252-order-lifecycle-inventory.test.ts: Added 50-assertion integration regression test suite (including concurrent cancellation HTTP test case) with real exit code handling.

Tests Run & Exact Results

  • git diff --check: 0 errors.
  • npm run test:issue-252: 50/50 assertion suites PASSED (100%)
  • npm run test:integration-lifecycle: 18/18 PASSED (100%)
  • npm run test:cancel-override: 14/14 PASSED (100%)
  • npm run test:issue-24: PASSED (100%)
  • npm run test:kds-integration: PASSED (100%)
  • npm run test:kds-contract: PASSED (100%)
  • npm run test:integration-reconciliation: PASSED (100%)
  • npm run test:integration-payments: PASSED (100%)
  • npm run build: Compiled cleanly
  • npm run lint:backend: 0 errors

Risk & Testing Scope

  • Regression Risk: Low. Monotonic transitions preserve all documented forward workflows (including direct checkout and KDS item completions).
  • Verification Coverage: Tested with isolated unit and integration suites spanning single-item orders, multi-item orders, order item status API calls (/api/order-items/:itemId/status), concurrent HTTP cancellation requests, and bill reconciliation.

Important

This PR requires maintainer review before merge. Do not auto-merge.

@khaira777 khaira777 added bug area:backend Backend (Express/SQLite) labels Aug 12, 2026
@khaira777
khaira777 requested a review from itsbkm August 12, 2026 22:01
@khaira777
khaira777 marked this pull request as ready for review August 12, 2026 22:06
@khaira777 khaira777 self-assigned this Aug 13, 2026
@khaira777 khaira777 added the area:frontend Frontend (React/Next.js) label Aug 13, 2026
@itsbkm

itsbkm commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Closing this as superseded by merged PR #289, which closed issue #252 with the hardened order lifecycle/inventory implementation and expanded regression coverage.

@itsbkm itsbkm closed this Aug 14, 2026
@itsbkm
itsbkm deleted the fix/252-order-lifecycle-inventory branch August 14, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Backend (Express/SQLite) area:frontend Frontend (React/Next.js)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(orders): enforce lifecycle transitions and one-time inventory restoration

2 participants