Repository navigation
fix(routes): harden order cancellation lifecycle - #289
Merged
Merged
Conversation
… and concurrency coverage
khaira777
force-pushed
the
fm/flocafe-252-fix
branch
from
August 13, 2026 17:13
1708274 to
20f3e7a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix issue #252's residual order/item cancellation defects on the current main branch, add regression coverage, validate the behavior, and publish the completed work from feature branch fm/flocafe-252-fix without merging it. Preserve the existing lifecycle transition rules, idempotent repeated-cancellation behavior, one-time inventory restoration, void exclusions, and insufficient-stock checks. Make authorization, PIN policy, order/item status checks, paid-order checks, and monetary recalculation decisions from transaction-local state so stale reads cannot authorize or mutate an order incorrectly. Cover the externally observable behavior through public HTTP and database-state tests, including terminal-order and terminal-item cancellation behavior, boundary-state/PIN changes, stale delivery-charge recalculation, repeated void cancellation, inventory-restoration safety, and valid lifecycle transitions. Keep the scope limited to this issue: do not touch PR #268, the Iran/issue #241 work, or unrelated files. The final publication should retain the proper fm/flocafe-252-fix branch, use a conventional PR title/body that accurately describes the full branch delta, and apply the bug label for issue #252.
What Changed
Risk Assessment
Testing
Installed locked dependencies after the initial missing-Electron setup failure. Ran the public HTTP/SQLite issue #252 regression suite, corrected its terminal-parent fixture, and reran it successfully; related authorization, lifecycle, and cancel-item/checkout suites also passed. Final diff and transient artifacts were checked and cleaned. No UI evidence was applicable because this is a backend/API/database change; CLI/database transcripts are the direct evidence.
Evidence: Issue #252 regression transcript
Evidence: Related authorization/lifecycle tests
/var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/no-mistakes-evidence/01KZWQ44NYGJQMC0HW73MW4MAH/integration-lifecycle.log) - Evidence: Cancel-item checkout test (local file:/var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/no-mistakes-evidence/01KZWQ44NYGJQMC0HW73MW4MAH/issue-24.log)Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed (5) ✅
main/routes/orders.ts:848- The same-state branch returns rawcurrentOrder, while the normal path appliesparseRowJsonat line 956. Repeated status requests therefore return serializedtax_breakdown/tax_snapshotvalues instead of the established API shape. Normalize the order before returning.main/routes/index.ts:299- The terminal-item no-op reportsorder.item_cancelled, and the common tail still callsrecordOrderChanged. With cloud sync enabled, repeated cancellation creates a fresh outbox event despite no mutation; repeated voids are also mislabeled as cancellations. Skip or correctly deduplicate this event for no-op requests.🔧 Fix: Normalize status responses and suppress duplicate no-op events
4 issues (2 errors, 2 warnings) still open:
main/routes/orders.ts:963- The transaction returnschanged, but the outer destructuring omits it.if (changed)therefore throws a ReferenceError after every status mutation, causing committed transitions to return 500 and skipping sync/KDS notifications. Destructurechangedat line 832.main/routes/index.ts:291- The intent requires preserving “idempotent repeated-cancellation behavior.” A cashier/waiter may successfully void an in-progress item with a manager PIN, but a retry seesvoidedand is rejected because the no-op branch only permits owner/manager. Apply the same authorized replay policy to terminal items while keeping it mutation- and event-free.main/routes/index.ts:465- The intent requires transaction-local “PIN policy” decisions. The item path bases PIN enforcement only on item status, so cancelling the sole pending item of apreparingorder requires no PIN and then auto-cancels the parent at lines 465-471, bypassing the parent cancellation policy that requires a manager PIN frompreparingonward. Evaluate the parent cancellation authorization inside this transaction before the item mutation/auto-cancel.main/routes/index.ts:663- The new restore no-op branch returns without mutation, but the common tail still recordsorder.item_restoredand notifies KDS. With cloud sync enabled, repeated restores of an already-active or voided item create outbox events despite no state change. Return a changed/event flag and suppress these side effects for no-op restores.🔧 Fix: Fix status mutation and restore no-op side effects
3 issues (2 errors, 1 warning) still open:
main/routes/index.ts:315- The transaction re-reads order/item state but reuses the pre-transactionreq.user.roleat lines 291 and 315. If an actor is demoted after authentication, a stale owner/manager role can still authorize item cancellation; the restore role gate is also outside the transaction. Re-read the active actor role inside the transaction before authorization.main/routes/index.ts:384- Restoration uses the product's currenttrack_inventoryflag as proof that this item consumed stock. Order creation deducts based on the flag at creation time, while product settings can change later; toggling tracking after an order can therefore skip a required restore or add stock that was never deducted. Persist the deducted quantity/ledger per item and use it across cancellation and restore paths.tests/issue-252-order-lifecycle-inventory.test.ts:639- The addedPromise.allrequests do not create overlapping route executions: the status handler andwithTxncallback are synchronous, so Node completes the first request before dispatching the second. This test verifies sequential idempotency, not the required concurrent read/mutate race; use a real synchronization barrier, separate writer, or worker-based overlap.🔧 Fix: Hardened transaction auth, inventory provenance, and concurrency coverage
3 errors still open:
main/routes/orders.ts:838- Authorization still uses pre-transactionreq.user.role; a demoted owner can bypass waiter ownership checks and mutate another user's order. Reread the actor role inside the transaction.main/db.ts:3574- Migration v67 infers historical inventory deductions from the mutable current product flag, causing missed or fabricated stock restoration after flag changes. Use historical provenance or an explicit safe legacy-data policy.main/db.ts:3918- Fresh schemas place the new column mid-table, while SQLite appends it during migration. Verbatim schema comparison can reject upgraded databases during recovery; make fresh and migrated definitions identical.🔧 Fix: Re-read active actor role inside status transaction
3 issues (2 errors, 1 warning) still open:
main/routes/index.ts:308- The required criterion is: “Make authorization, PIN policy, order/item status checks, paid-order checks, and monetary recalculation decisions from transaction-local state.” The item-cancel transaction never checks for a paid bill before mutating the item/stock; its bill sync only selects unpaid bills. A paid split-order item can therefore be cancelled and restocked while the paid bill/payment history remains unchanged.main/routes/orders.ts:931- Whole-order cancellation restores stock and marks eligible items/order cancelled without a transaction-local paid-bill guard. On an active order with a paid split bill, this leaves the paid bill/payment record untouched while changing order and inventory state.tests/issue-252-order-lifecycle-inventory.test.ts:95- The worker mutation completes before the request enterswithTxn, and synchronous handlers prevent overlap, so this purported concurrency test only exercises sequential requests plus a pre-transaction state change. Add a barrier that creates genuinely overlapping writers.🔧 Fix: Reject item cancellation on paid orders
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
npm cinpm run test:issue-252npm run test:orders-authznpm run test:integration-lifecyclenpm run test:issue-24git diff --check HEAD✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.