Repository navigation
fix(main): enforce order/bill settlement guards and flat-discount caps - #1312
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@greptileai review this at apex |
[Medium impact] The changes reach order and billing workflows that affect totals and payment status. Preserve saved item tax rules during customer changes before merging. Findings
|
ab69dcb to
941e730
Compare
|
@greptileai review this at apex |
1 similar comment
|
@greptileai review this at apex |
941e730 to
7a6975d
Compare
|
@greptileai review this at apex |
1 similar comment
|
@greptileai review this at apex |
Fixes #1295 - Restrict syncUnpaidBillsForOrder and POST /api/bills/generate resync to unpaid and partially paid bills so refunded, partially_refunded, and Rs.0 paid bills are not overwritten. - Guard PATCH /api/orders/:id/convert-to-takeaway against settled, refunded, and split bills, enforce below_collected_funds when takeaway total drops below paid_amount, and record converted_to_takeaway audit entries. - Invoke completeExactBalanceSettlement when convert-to-takeaway, item edit, or POST /api/bills/generate brings a partially paid bill to exact zero balance. - Add orderWriteRateLimit and settled/refunded/Rs.0 paid bill guards to PATCH /api/orders/:orderId/items/:itemId/restore. - Preserve flat order discounts (clamped to newSubtotal) instead of scaling proportionally in handleItemDiscount, handleItemPriceOverride, and item cancel. - Guard PATCH /api/orders/:id/customer against settled, partial, refunded, and split bills, recompute customer-dependent taxes, sync unpaid bills, and record customer_updated audit entries.
…ount recalculation
7a6975d to
46df577
Compare
Intent
Enforce backend order/bill settlement invariants and flat-discount caps in main/routes/orders.ts and main/routes/bills.ts (Issue #1295)
What Changed
POST /api/bills/generateandsyncUnpaidBillsForOrderinmain/routes/bills.tsto re-sync onlyunpaid,partial, andpartially_paidbills (preservingpaid,partially_refunded, andrefundedbills except zero-total paid split siblings during split reallocation), and triggercompleteExactBalanceSettlementwhen a re-synced partially paid bill reaches exact zero balance.main/routes/orders.tsonPATCH /api/orders/:id/customer,PATCH /api/orders/:id/convert-to-takeaway, andPATCH /api/orders/:orderId/items/:itemId/restore(includingorderWriteRateLimit), recomputing non-terminal item taxes from savedtax_snapshotcategory and behavior rules on customer updates, re-resolving takeaway charges withbelow_collected_fundsvalidation on dine-in conversion, and recordingcustomer_updatedandconverted_to_takeawayaudit log entries.resolveOrderLevelDiscountAmountinmain/routes/orders.tsso subtotal-changing mutations scale percentage order discounts while preserving flat order discounts fromdiscount_valuecapped at the updated subtotal, and updatetests/issue-terminal-state-guards.test.ts,docs/architecture/order-lifecycle.md, anddocs/reference/api.md.Risk Assessment
✅ Low: The settlement state guards, exact-balance auto-settlement hooks, customer tax recomputation, and flat order discount clamping/restoration are well-bounded, consistent across order and bill mutation routes, and covered by end-to-end integration assertions.
Testing
Ran the focused integration suites (
npm run test:issue-terminal-state-guardsandnpm run test:issue-exact-balance-settlement), compiled the backend (npm run build), and stood up an isolated live FloCafe Express/SQLite server (http://127.0.0.1:4311) to drive all six settlement-guard, exact-balance auto-settlement, flat-discount clamping/restoration, and customer tax-recomputation scenarios end-to-end, capturing API and SQLite state evidence inlive-settlement-safety-verification.json(no visual screenshot artifacts were captured because this change only modifies backend Express routes and docs with no UI or renderer changes).npm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsEvidence: Live HTTP API and SQLite state verification transcript (Issue #1295)
{ "serverBaseUrl": "http://127.0.0.1:4311", "scenario1": { "zeroPaidBill": { "id": 1, "payment_status": "paid", "total": 0, "balance": 0 }, "partiallyRefundedBill": { "id": 2, "payment_status": "partially_refunded", "total": 1000 } }, "scenario2": { "belowCollectedFundsResponse": { "status": 409, "data": { "error": "Cannot convert order below already collected payment amount", "code": "below_collected_funds", "display_code": "below_collected_funds" } }, "refundedBillResponse": { "status": 409, "data": { "error": "Cannot convert an order with a paid or refunded bill" } } }, "scenario3": { "convertToTakeawayExactSettle": { "bill": { "payment_status": "paid", "total": 1000, "paid_amount": 1000, "balance": 0 }, "order": { "status": "completed", "type": "takeaway", "table_id": null, "total": 1000 }, "tableStatus": "available", "audit": { "action": "converted_to_takeaway" } }, "itemEditExactSettle": { "bill": { "payment_status": "paid", "total": 400, "paid_amount": 400, "balance": 0 }, "order": { "status": "completed", "total": 400 } } }, "scenario4": { "restoreOnZeroPaidBill": { "status": 400, "data": { "error": "Cannot restore items on a paid order" } } }, "scenario5": { "afterPriceOverride": { "subtotal": 1200, "discount_amount": 600, "discount_value": 600, "total": 600 }, "afterCancelClamped": { "subtotal": 200, "discount_amount": 200, "discount_value": 600, "total": 0 }, "afterRestoreFromDiscountValue": { "subtotal": 1200, "discount_amount": 600, "discount_value": 600, "total": 600 } }, "scenario6": { "blockedOnPartialBill": { "status": 409, "data": { "error": "Cannot change customer once a bill has payments, a split, or a refund" } }, "recomputedOrder": { "id": 9, "customer_id": "cust-live-2", "tax_amount": 50, "total": 1050 }, "syncedBill": { "customer_id": "cust-live-2", "tax_amount": 50, "total": 1050, "balance": 1050 }, "audit": { "action": "customer_updated" } } }Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ No issues found.
✅ **Test** - passed
✅ No issues found.
npm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-exact-balance-settlementnpm run test:order-item-cancel-restorenpm run test:order-item-editnpm run test:item-price-overridenpm run test:order-totals-recomputenpm run test:orders-authzLive HTTP server verification againstmain/server.ts+ SQLite in an isolated userData directory (~/.no-mistakes/evidence/01M5JTPS1ZVTJTR1KKAGJPC3XD/live-settlement-and-discount-verification.json)✅ No issues found.
npm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-terminal-state-guardsnpm run test:issue-exact-balance-settlementnpm run buildLive Express/SQLite server verification onhttp://127.0.0.1:4311driving/api/orders,/api/bills/generate,/api/bills/:id/payment,/api/refunds,/api/orders/:id/convert-to-takeaway,/api/orders/:id/items/:itemId/edit,/api/orders/:id/items/:itemId/restore,/api/orders/:id/items/:itemId/price, and/api/orders/:id/customer✅ **Document** - passed
✅ No issues found.
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
✅ No issues found.