Repository navigation
fix(billing): remediate loyalty re-award and zero-balance idempotency - #1249
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 |
|
@greptile review |
[Medium impact] The changes affect bill settlement and loyalty within the payment workflow. The PR appears safe to merge, with two non-blocking improvements needed in the regression tests. Findings
|
Summary
Fixes financial settlement defects F1 and F2 in
main/routes/bills.tsand adds deterministic regression coverage including owner-reject/waiter-payment concurrency checks.Closes #1241
Closes #1205
What Failed and Why
Defect F1 (#1241): Loyalty Cashback Re-awarded & Kitchen Delivery Check Repeated on Already-Paid Bills
In
applyPaymentBatch(main/routes/bills.ts), when a payment request was submitted with no positive payment lines (prepared.length === 0), the code branched into the zero-amount settlement path without verifying whether the bill was already paid (bill.payment_status === 'paid'). If loyalty cashback was enabled subsequent to the bill's initial settlement (or if the bill lacked an initialloyalty_ledgerentry), the zero-amount call on an already-paid bill recomputed cashback against the bill's historical total, inserted duplicateloyalty_ledgerrows, and creditedcustomers.loyalty_points. Furthermore, it invokedcheckKitchenDeliveryStatusand recorded duplicate kitchen delivery override audit rows, potentially failing with 409 if kitchen items were undelivered even though the bill had already been settled.Defect F2 (#1205): Idempotency Record Not Persisted for Zero-Balance Settlements & Missing Kitchen Override Flag
When settling an unpaid or partially paid bill whose remaining balance was zero (e.g., fully discounted),
applyPaymentBatchreturned immediately at line 3425. It bypassed line 3565 wherepayment_idempotencyrecords are inserted, leavingpayment_idempotencyempty for legitimate zero-balance settlements. Consequently, any network retry or replay with the sameIdempotency-Keywas treated as a fresh payment request; if an override PIN was rotated or a manager deactivated, subsequent replays failed with 403 instead of returning the cached idempotent response (200withidempotent_replay: true). In addition, the initial zero-balance settlement response failed to propagatekitchenDeliveryOverridden: truewhen a kitchen override had occurred.How It Was Reproduced (Red-to-Green)
A comprehensive integration test suite was created in
tests/bills-settlement-idempotency.test.tscovering:loyalty_ledgerinsertions.bills,orders, andtablesare untouched by no-op calls on paid bills.payment_idempotencyrecord persistence andkitchenDeliveryOverridden: truein response./Users/gurkiratkhaira/tools/firstmate/data/remediate-f1-f2-settlement/repro-red.txt./Users/gurkiratkhaira/tools/firstmate/data/remediate-f1-f2-settlement/repro-green.txt.Minimal Fix Details
main/routes/bills.ts(validatePaymentFields):amount === 0andamount === '0'so zero-amount requests reachpreparePaymentBatch(which validates that positive balances cannot tender zero, while zero-balance/paid bills proceed to batch processing).main/routes/bills.ts(applyPaymentBatch):wasAlreadyPaid = bill.payment_status === 'paid'.wasAlreadyPaid && prepared.length === 0: return cached bill withloyaltyPointsEarned: 0, persist idempotency response if an idempotency key was supplied and not yet recorded, and skip kitchen delivery checks and database mutations.!wasAlreadyPaid && prepared.length === 0: include...(kitchenStatus.overridden ? { kitchenDeliveryOverridden: true } : {})in the result and persist thepayment_idempotencyrow inside the transaction before returning.main/routes/bills.ts(POST /:id/paymentandPOST /:id/payments):isAlreadyPaidNoOpchecks skippingcheckKitchenDeliveryStatusandpreVerifyKitchenDeliveryPinwhenbillRow.payment_status === 'paid'and no positive payment amount is submitted.Invariant and Safety Disclosures
kitchenDeliveryOverridden: truewhen a kitchen delivery override was performed.Verification
npx oxfmt --writeandnpx oxfmt --checkpassed cleanly across modified and new files.npm run lint(0 errors)npm run lint:budget(backend: 998/1000, frontend: 0/5)npm run build(compiled clean)npm run test:script-coverage(245 reachable scripts verified)npm run test:bills-settlement-idempotency(64/64 passed, 0 failed)npm test(full suite executed and passed)