Repository navigation
fix: cap order item payload size and clamp bill flat discounts to subtotal - #813
Conversation
|
Warning Review limit reachedNext included review available in 21 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: Repository: FreeOpenSourcePOS/FloCafe/.coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (3)
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 |
|
SummaryCoverage focused on order creation and appending at size boundaries, safe retry behavior, and discount safeguards such as authorization, bill-state rules, approval requirements, bounded calculations, and consistent totals across records. These represent primarily business-logic edge cases and adversarial inputs, with no application regression identified. Safe to merge — no regressions, new failures, or previously flagged PR-attributable failures were found. Several runtime-dependent behaviors remain unexercised because the local environment could not initialize, but that is an environment caveat rather than a merge blocker. Tests run by Ito
Tip Reply with @itoqa to send us feedback on this test run. |
dfc11b7 to
289d167
Compare
Commit: SummaryCoverage spans core order flows and boundary handling, including adding items, preventing duplicates, enforcing item limits, and safely rejecting malformed or ineligible requests. It also checks billing and discount business rules such as validation, discount capping, lifecycle restrictions, and consistency of saved financial totals. Safe to merge — the run found no PR-attributable regressions or unresolved failures, and the exercised order and billing behaviors are functioning as expected. The environment prevented direct execution of some order scenarios, but those findings are unrelated to this PR and are not merge blockers. Tests run by Ito
Tip Reply with @itoqa to send us feedback on this test run. |

Problem
Two request-bound gaps allow oversized or nonsensical writes on order and bill endpoints:
POST /api/ordersandPOST /api/orders/:id/itemsonly checked thatitemswas non-empty. A single request could send an arbitrarily large array (no cap), unlike held orders which already enforceMAX_HELD_ORDER_ITEMS. This is both a payload-size / DoS concern and a data-integrity concern (one request can create hundreds of lines that the rest of the UI and KDS paths were never sized for).POST /api/bills/:id/applyDiscountaccepted a flatvaluegreater thanbill.subtotal. The storeddiscount_amountthen exceeded subtotal, drivingdiscountedSubtotal/ tax ratio to 0 with no clamp, producing a non-negative-looking total that no longer matched the discount math. Order-level discount already clamps; bill-level did not.Root cause
main/routes/orders.tscreate + appenditems.length === 0was rejected; no upper boundmain/routes/bills.tsapplyDiscountMath.min(..., bill.subtotal)Fix (surgical)
MAX_ORDER_ITEMS = 200and rejectitems.length > 200on bothPOST /api/ordersandPOST /api/orders/:id/itemswith 400 and a message that names the cap. Also tighten both handlers toArray.isArray(items)before reading.length.applyDiscount, after resolving currency decimals, setdiscountAmount = Math.min(discountAmount, bill.subtotal)before rounding — matching the order-discount clamp behavior. Oversized discounts are clamped, not rejected (same UX as the order endpoint).Cap is per-request (200), slightly above held-order cart max (100) so live orders can grow across multiple append calls.
Test evidence (post-fix)
Pre-fix baseline for the same file was 12/18 (6 failing). Scenarios covered:
Focused suites (all green):
npm run test:discount-system37/37,npm run test:integration-discount18/18,npm run test:integration-discount-settings17/17,npm run test:orders-authzpass,npm run test:order-audit-logpass.Quality gates:
npm run lint:backend— 0 errors (1000 warnings, pre-existing budget),npm run build— pass,node scripts/ci/validate-test-script-coverage.cjs+node tests/test-script-coverage.test.cjs— OK (172 reachable, 6 excluded),ocr delegate preview/ocr delegate rule— run.Notes
PATCH /api/orders/:id/discountbehavior; storeddiscount_valuestill reflects the request (same as the order endpoint).test:issue-payload-and-discount-boundsand appended to the roottestchain for script-coverage.