Skip to content

fix(routes): harden order cancellation lifecycle - #289

Merged
khaira777 merged 7 commits into
mainfrom
fm/flocafe-252-fix
Aug 13, 2026
Merged

khaira777 merged 7 commits into
mainfrom
fm/flocafe-252-fix

Conversation

@khaira777

Copy link
Copy Markdown
Contributor

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

  • Hardened order and item cancellation/status flows with transaction-local authorization, PIN, terminal-state, paid-order, and lifecycle checks, including idempotent repeated cancellation.
  • Persisted per-item inventory deductions and made cancellation/restoration update stock exactly once while excluding void/accounting items and preserving current monetary charges during recalculation.
  • Documented the updated order/item APIs and added comprehensive issue fix(orders): enforce lifecycle transitions and one-time inventory restoration #252 HTTP/database regression coverage, wired into the test suite.

Risk Assessment

⚠️ Medium: The stateful transaction, inventory, payment, migration, and event changes are bounded, with no additional actionable defects found in this review pass.

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

> [email protected] test:issue-252
> node tests/run-electron-node-test.cjs tests/issue-252-order-lifecycle-inventory.test.ts

Regression Test: Issue #252 Order Lifecycle & Inventory Safety
=================================================================
[DB] Opening database at: /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-issue-252-test-tdje5w/flo.db
[DB] Schema: v0 → v67
[DB] Triggering auto-backup before migrating v0 → v67...
[DB] Auto-backup before migrating v0 → v67 created at /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-issue-252-test-tdje5w/backups/flo-backup-2026-08-13T17-02-07-853Z-pre-v0-to-v67.db
[DB] Applying migration v1: initial_schema
[DB] Install defaults loaded; first-run setup pending
[DB] Migration v1 complete
[DB] Applying migration v2: hash_plaintext_pins
[DB] Migration v2 complete
[DB] Applying migration v3: cloud_identity_and_outbox
[DB] Migration v3 complete
[DB] Applying migration v4: add_notes_limits_settings
[DB] Migration v4 complete
[DB] Applying migration v5: add_print_logs_table
[DB] Migration v5 complete
[DB] Applying migration v6: add_loyalty_settings
[DB] Migration v6 complete
[DB] Applying migration v7: add_discount_settings
[DB] Migration v7 complete
[DB] Applying migration v8: add_loyalty_index
[DB] Migration v8 complete
[DB] Applying migration v9: add_sequences_table
[DB] Migration v9 complete
[DB] Applying migration v10: fix_sequences_composite_key
[DB] Migration v10 complete
[DB] Applying migration v11: first_run_setup_uses_welcome_form
[DB] Migration v11 complete
[DB] Applying migration v12: fix_table_integer_ids
[DB] Migration v12 complete
[DB] Applying migration v13: fix_null_table_ids
[DB] Migration v13 complete
[DB] Applying migration v14: simplify_loyalty_settings
[DB] Migration v14 complete
[DB] Applying migration v15: add_instagram_handle_setting
[DB] Migration v15 complete
[DB] Applying migration v16: add_terms_accepted_at_to_users
[DB] Migration v16 complete
[DB] Applying migration v17: add_held_orders_table
[DB] Migration v17 complete
[DB] Applying migration v18: fix_null_category_ids
[DB] Migration v18 complete
[DB] Applying migration v19: backfill_product_cb_percent_and_tags
[DB] Migration v19 complete
[DB] Applying migration v20: add_tables_is_active
[DB] Migration v20 complete
[DB] Applying migration v21: clear_legacy_loyalty_expiry
[DB] Migration v21 complete
[DB] Applying migration v22: add_customers_phone_digits
[DB] Migration v22 complete
[DB] Applying migration v23: normalize_customer_phones
[MIGRATION v23] normalized: 0, unparseable: 0
[MIGRATION v23] merged 0 duplicate customer(s)
[MIGRATION v23] verification: 0 customers, 0 still non-E.164
[DB] Migration v23 complete
[DB] Applying migration v24: normalize_customer_phones_retry
[MIGRATION v24] normalized: 0, unparseable: 0
[DB] Migration v24 complete
[DB] Applying migration v25: add_order_item_addons_table
[MIGRATION v25] backfilled addons for 0 order items (0 unparseable, skipped)
[DB] Migration v25 complete
[DB] Applying migration v26: add_kds_default_view
[DB] Migration v26 complete
[DB] Applying migration v27: add_station_printer_link_and_user_stations
[DB] Migration v27 complete
[DB] Applying migration v28: seed_telemetry_settings
[DB] Migration v28 complete
[DB] Applying migration v29: whatsapp_messaging
[DB] Migration v29 complete
[DB] Applying migration v30: drop_order_items_addons_json_column
[MIGRATION v30] backfilled 0 order_item(s) still missing a normalized addons snapshot
[MIGRATION v30] Dropped order_items.addons — order_item_addons is now the only place selected addons live.
[DB] Migration v30 complete
[DB] Applying migration v31: add_customers_tag_counts_column
[DB] Migration v31 complete
[DB] Applying migration v32: add_kds_and_kot_printing_toggles
[DB] Migration v32 complete
[DB] Applying migration v33: add_addon_groups_allow_multiple_quantities
[DB] Migration v33 complete
[DB] Applying migration v34: add_order_items_voided_at
[DB] Migration v34 complete
[DB] Applying migration v35: add_tax_pack_configuration_tables
[DB] Migration v35 complete
[DB] Applying migration v36: add_product_and_addon_tax_categories
[DB] Migration v36 complete
[DB] Applying migration v37: add_transaction_tax_snapshots_and_charge_categories
[DB] Migration v37 complete
[DB] Applying migration v38: register_bundled_tax_pack_versions
[DB] Migration v38 complete
[DB] Applying migration v39: add_users_tokens_valid_after
[DB] Migration v39 complete
[DB] Applying migration v40: v2_cloud_defaults_and_tax_toggle
[DB] Migration v40 complete
[DB] Applying migration v41: support_ticket_outbox
[DB] Migration v41 complete
[DB] Applying migration v42: add_global_cashback_percent
[DB] Migration v42 complete
[DB] Applying migration v43: telemetry_default_on_for_new_installs
[DB] Migration v43 complete
[DB] Applying migration v44: store_diagnostics_outbox
[DB] Migration v44 complete
[DB] Applying migration v45: add_performance_indexes_and_normalize_timestamps
[DB] Migration v45 complete
[DB] Applying migration v46: normalize_cloud_enabled_flags_to_01
[DB] Migration v46 complete
[DB] Applying migration v47: store_diagnostics_enabled_by_default
[DB] Migration v47 complete
[DB] Applying migration v48: deactivate_reusable_demo_credentials
[DB] Migration v48 complete
[DB] Applying migration v49: add_payment_idempotency_records
[DB] Migration v49 complete
[DB] Applying migration v50: add_order_idempotency_records
[DB] Migration v50 complete
[DB] Applying migration v51: enforce_global_payment_transaction_refs
[DB] Migration v51 complete
[DB] Applying migration v52: restore_method_scoped_transaction_refs
[DB] Migration v52 complete
[DB] Applying migration v53: scope_idempotency_records_to_user
[DB] Migration v53 complete
[DB] Applying migration v54: repair_retry_ownership_and_payment_reference_history
[DB] Migration v54 complete
[DB] Applying migration v55: durable_token_revocations
[DB] Migration v55 complete
[DB] Applying migration v56: persist_station_assignment_scope
[DB] Migration v56 complete
[DB] Applying migration v57: rename_gstin_to_generic_tax_registration_number
[DB] Migration v57 complete
[DB] Applying migration v58: configurable_manual_payment_methods
[DB] Migration v58 complete
[DB] Applying migration v59: split_checks_by_item_quantity
[DB] Migration v59 complete
[DB] Applying migration v60: seed_split_checks_disabled_setting
[DB] Migration v60 complete
[DB] Applying migration v61: seed_server_app_enabled_setting
[DB] Migration v61 complete
[DB] Applying migration v62: normalize_cloud_last_error
[DB] Migration v62 complete
[DB] Applying migration v63: seed_printer_trim_decimals_setting
[DB] Migration v63 complete
[DB] Applying migration v64: drop_unused_printer_usb_device_path
[DB] Migration v64 complete
[DB] Applying migration v65: seed_bill_content_visibility_settings
[DB] Migration v65 complete
[DB] Applying migration v66: seed_bill_template_settings
[DB] Migration v66 complete
[DB] Applying migration v67: persist_order_item_inventory_deductions
[DB] Migration v67 complete
[DB] integrity_check: ok
[DB] foreign_key_check: clean
[Auth] Generated new JWT secret for this install

─── 1. Repeated Whole-Order Cancellation ───
  ✓ Stock deducted on order creation (10 -> 8)
  ✓ First cancel HTTP status
  ✓ Stock restored on first cancellation (8 -> 10)
  ✓ Second cancel HTTP status (idempotent no-op)
  ✓ Stock MUST remain 10 after second cancel (no double-restock)

─── 2. Order Lifecycle Transition Matrix Enforcement ───
  ✓ pending -> preparing is valid
  ✓ pending -> preparing persists preparing
  ✓ preparing -> ready is valid
  ✓ preparing -> ready persists ready
[API] Internal error: Error: Cannot transition order status from 'ready' to 'preparing'
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/orders.ts:868:29
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratk

... [20379 bytes truncated] ...

e restored on whole-order cancel
  ✓ Item B (active) stock MUST be restored on whole-order cancel
  ✓ Item A stock unchanged after repeated order cancel
  ✓ Item B stock unchanged after repeated order cancel

─── 5. Pending Item in Preparing Order ───
  ✓ Item status is still pending while order is preparing
  ✓ Cancel pending item in preparing order HTTP status
  ✓ Pending item in preparing order becomes cancelled (not voided)
  ✓ Pending item in preparing order restores stock (+1)
  ✓ No void_adjustment created for pending item

─── 6. Item Cancel & Restore Idempotency ───
  ✓ Pending item cancel HTTP status
  ✓ Pending item cancel restores stock (+1)
  ✓ Repeated pending item cancel HTTP status
  ✓ Repeated pending item cancel MUST NOT restore stock again
[Orders] Restore item error: Error: Only owner or manager can restore items
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:565:31
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:557:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
[API] Internal error: Error: Only owner or manager can restore items
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:565:31
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:557:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
  ✓ demoted actor cannot restore with a stale owner token
  ✓ Item restore HTTP status
  ✓ Item restore re-deducts stock (-1)
  ✓ Repeated item restore HTTP status
  ✓ Repeated item restore MUST NOT deduct stock again
  ✓ item cancellation succeeds after tracking is disabled
  ✓ cancellation restores stock deducted before tracking was disabled
  ✓ item restore succeeds after tracking is disabled
  ✓ restore re-deducts the original inventory quantity
  ✓ insufficient-stock restore fixture cancels the tracked item
[Orders] Restore item error: Error: Insufficient stock to restore item (Available: 0, Required: 1)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:585:33
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:557:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 400
}
[API] Internal error: Error: Insufficient stock to restore item (Available: 0, Required: 1)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:585:33
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:557:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 400
}
  ✓ restore rejects when tracked stock is insufficient
  ✓ insufficient-stock restore leaves the item cancelled
  ✓ insufficient-stock restore leaves stock unchanged
  ✓ insufficient-stock restore leaves the parent order active

─── 7. Final Item Cancellation / Auto-Cancel ───
  ✓ Order auto-cancelled when final item cancelled
  ✓ Stock track-2 restored exactly once on auto-cancel (+1)

─── 8. Concurrent Whole-Order Cancellation ───
  ✓ Stock deducted on creation (10 -> 8)
  ✓ Concurrent cancel request A returned 200
  ✓ Concurrent cancel request B returned 200
  ✓ Final order status is cancelled
  ✓ Inventory restored exactly once (8 -> 10, NOT 12) under concurrent requests
[DB] Database closed

=================================================================
93/93 passed, 0 failed
Evidence: Related authorization/lifecycle tests

> [email protected] test:orders-authz
> node tests/run-electron-node-test.cjs tests/orders-authz.test.ts

[DB] Opening database at: /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-orders-authz-qwA2Wx/flo.db
[DB] Schema: v0 → v67
[DB] Triggering auto-backup before migrating v0 → v67...
[DB] Auto-backup before migrating v0 → v67 created at /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-orders-authz-qwA2Wx/backups/flo-backup-2026-08-13T17-02-31-566Z-pre-v0-to-v67.db
[DB] Applying migration v1: initial_schema
[DB] Install defaults loaded; first-run setup pending
[DB] Migration v1 complete
[DB] Applying migration v2: hash_plaintext_pins
[DB] Migration v2 complete
[DB] Applying migration v3: cloud_identity_and_outbox
[DB] Migration v3 complete
[DB] Applying migration v4: add_notes_limits_settings
[DB] Migration v4 complete
[DB] Applying migration v5: add_print_logs_table
[DB] Migration v5 complete
[DB] Applying migration v6: add_loyalty_settings
[DB] Migration v6 complete
[DB] Applying migration v7: add_discount_settings
[DB] Migration v7 complete
[DB] Applying migration v8: add_loyalty_index
[DB] Migration v8 complete
[DB] Applying migration v9: add_sequences_table
[DB] Migration v9 complete
[DB] Applying migration v10: fix_sequences_composite_key
[DB] Migration v10 complete
[DB] Applying migration v11: first_run_setup_uses_welcome_form
[DB] Migration v11 complete
[DB] Applying migration v12: fix_table_integer_ids
[DB] Migration v12 complete
[DB] Applying migration v13: fix_null_table_ids
[DB] Migration v13 complete
[DB] Applying migration v14: simplify_loyalty_settings
[DB] Migration v14 complete
[DB] Applying migration v15: add_instagram_handle_setting
[DB] Migration v15 complete
[DB] Applying migration v16: add_terms_accepted_at_to_users
[DB] Migration v16 complete
[DB] Applying migration v17: add_held_orders_table
[DB] Migration v17 complete
[DB] Applying migration v18: fix_null_category_ids
[DB] Migration v18 complete
[DB] Applying migration v19: backfill_product_cb_percent_and_tags
[DB] Migration v19 complete
[DB] Applying migration v20: add_tables_is_active
[DB] Migration v20 complete
[DB] Applying migration v21: clear_legacy_loyalty_expiry
[DB] Migration v21 complete
[DB] Applying migration v22: add_customers_phone_digits
[DB] Migration v22 complete
[DB] Applying migration v23: normalize_customer_phones
[MIGRATION v23] normalized: 0, unparseable: 0
[MIGRATION v23] merged 0 duplicate customer(s)
[MIGRATION v23] verification: 0 customers, 0 still non-E.164
[DB] Migration v23 complete
[DB] Applying migration v24: normalize_customer_phones_retry
[MIGRATION v24] normalized: 0, unparseable: 0
[DB] Migration v24 complete
[DB] Applying migration v25: add_order_item_addons_table
[MIGRATION v25] backfilled addons for 0 order items (0 unparseable, skipped)
[DB] Migration v25 complete
[DB] Applying migration v26: add_kds_default_view
[DB] Migration v26 complete
[DB] Applying migration v27: add_station_printer_link_and_user_stations
[DB] Migration v27 complete
[DB] Applying migration v28: seed_telemetry_settings
[DB] Migration v28 complete
[DB] Applying migration v29: whatsapp_messaging
[DB] Migration v29 complete
[DB] Applying migration v30: drop_order_items_addons_json_column
[MIGRATION v30] backfilled 0 order_item(s) still missing a normalized addons snapshot
[MIGRATION v30] Dropped order_items.addons — order_item_addons is now the only place selected addons live.
[DB] Migration v30 complete
[DB] Applying migration v31: add_customers_tag_counts_column
[DB] Migration v31 complete
[DB] Applying migration v32: add_kds_and_kot_printing_toggles
[DB] Migration v32 complete
[DB] Applying migration v33: add_addon_groups_allow_multiple_quantities
[DB] Migration v33 complete
[DB] Applying migration v34: add_order_items_voided_at
[DB] Migration v34 complete
[DB] Applying migration v35: add_tax_pack_configuration_tables
[DB] Migration v35 complete
[DB] Applying migration v36: add_product_and_addon_tax_categories
[DB] Migration v36 complete
[DB] Applying migration v37: add_transaction_tax_snapshots_and_charge_categories
[DB] Migration v37 complete
[DB] Applying migration v38: register_bundled_tax_pack_versions
[DB] Migration v38 complete
[DB] Applying migration v39: add_users_tokens_valid_after
[DB] Migration v39 complete
[DB] Applying migration v40: v2_cloud_defaults_and_tax_toggle
[DB] Migration v40 complete
[DB] Applying migration v41: support_ticket_outbox
[DB] Migration v41 complete
[DB] Applying migration v42: add_global_cashback_percent
[DB] Migration v42 complete
[DB] Applying migration v43: telemetry_default_on_for_new_installs
[DB] Migration v43 complete
[DB] Applying migration v44: store_diagnostics_outbox
[DB] Migration v44 complete
[DB] Applying migration v45: add_performance_indexes_and_normalize_timestamps
[DB] Migration v45 complete
[DB] Applying migration v46: normalize_cloud_enabled_flags_to_01
[DB] Migration v46 complete
[DB] Applying migration v47: store_diagnostics_enabled_by_default
[DB] Migration v47 complete
[DB] Applying migration v48: deactivate_reusable_demo_credentials
[DB] Migration v48 complete
[DB] Applying migration v49: add_payment_idempotency_records
[DB] Migration v49 complete
[DB] Applying migration v50: add_order_idempotency_records
[DB] Migration v50 complete
[DB] Applying migration v51: enforce_global_payment_transaction_refs
[DB] Migration v51 complete
[DB] Applying migration v52: restore_method_scoped_transaction_refs
[DB] Migration v52 complete
[DB] Applying migration v53: scope_idempotency_records_to_user
[DB] Migration v53 complete
[DB] Applying migration v54: repair_retry_ownership_and_payment_reference_history
[DB] Migration v54 complete
[DB] Applying migration v55: durable_token_revocations
[DB] Migration v55 complete
[DB] Applying migration v56: persist_station_assignment_scope
[DB] Migration v56 complete
[DB] Applying migration v57: rename_gstin_to_generic_tax_registration_number
[DB] Migration v57 complete
[DB] Applying migration v58: configurable_manual_payment_methods
[DB] Migration v58 complete
[DB] Applying migration v59: split_checks_by_item_quantity
[DB] Migration v59 complete
[DB] Applying migration v60: seed_split_checks_disabled_setting
[DB] Migration v60 complete
[DB] Applying migration v61: seed_server_app_enabled_setting
[DB] Migration v61 complete
[DB] Applying migration v62: normalize_cloud_last_error
[DB] Migration v62 complete
[DB] Applying migration v63: seed_printer_trim_decimals_setting
[DB] Migration v63 complete
[DB] Applying migration v64: drop_unused_printer_usb_device_path
[DB] Migration v64 complete
[DB] Applying migration v65: seed_bill_content_visibility_settings
[DB] Migration v65 complete
[DB] Applying migration v66: seed_bill_template_settings
[DB] Migration v66 complete
[DB] Applying migration v67: persist_order_item_inventory_deductions
[DB] Migration v67 complete
[DB] integrity_check: ok
[DB] foreign_key_check: clean
  ✓ cashier can advance an order to preparing
  ✓ cashier can void an in-progress item with a valid manager PIN
  ✓ cashier void marks the original item voided
  ✓ waiter can void an in-progress item with a valid manager PIN
  ✓ waiter void marks the original item voided
  ✓ waiter can advance their own order
[API] Internal error: Error: Waiters can only modify their own orders
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/orders.ts:846:29
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/orders.ts:832:65
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/middleware/security.ts:285:5
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3) {
  statusCode: 403
}
  ✓ waiter cannot cancel another user's order
[Orders] Cancel item error: Error: Waiters can only modify their own orders
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:289:31
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:277:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
[API] Internal error: Error: Waiters can only modify their own orders
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:289:31
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:277:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
  ✓ waiter cannot void another user's item
[Orders] Cancel item error: Error: Invalid manager PIN
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:368:33
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:277:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
[API] Internal error: Error: Invalid manager PIN
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:368:33
    at sqliteTransaction (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at withTxn (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/db.ts:559:28)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/main/routes/index.ts:277:29
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at next (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:157:13)
    at Route.dispatch (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/route.js:117:3)
    at handle (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:435:11)
    at Layer.handleRequest (/Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/lib/layer.js:152:17)
    at /Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01KZWQ44NYGJQMC0HW73MW4MAH/node_modules/router/index.js:295:15 {
  statusCode: 403
}
  ✓ cashier with an invalid manager PIN is denied
  ✓ invalid PIN denial identifies the PIN, not the role
[DB] Database closed
- Evidence: Related lifecycle test (local file: /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 raw currentOrder, while the normal path applies parseRowJson at line 956. Repeated status requests therefore return serialized tax_breakdown/tax_snapshot values instead of the established API shape. Normalize the order before returning.
  • ⚠️ main/routes/index.ts:299 - The terminal-item no-op reports order.item_cancelled, and the common tail still calls recordOrderChanged. 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 returns changed, 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. Destructure changed at 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 sees voided and 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 a preparing order requires no PIN and then auto-cancels the parent at lines 465-471, bypassing the parent cancellation policy that requires a manager PIN from preparing onward. 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 records order.item_restored and 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-transaction req.user.role at 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 current track_inventory flag 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 added Promise.all requests do not create overlapping route executions: the status handler and withTxn callback 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-transaction req.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 enters withTxn, 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 ci
  • npm run test:issue-252
  • npm run test:orders-authz
  • npm run test:integration-lifecycle
  • npm run test:issue-24
  • git diff --check HEAD
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@khaira777
khaira777 requested a review from itsbkm as a code owner August 13, 2026 17:10
@khaira777
khaira777 merged commit 98f09dc into main Aug 13, 2026
5 checks passed
@khaira777
khaira777 deleted the fm/flocafe-252-fix branch August 13, 2026 18:08
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.

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

1 participant