Skip to content

Harden API v1 order assembly atomicity#313

Open
sureshchouksey8 wants to merge 1 commit into
BAWES-Universe:mainfrom
sureshchouksey8:codex/v1-order-atomicity
Open

Harden API v1 order assembly atomicity#313
sureshchouksey8 wants to merge 1 commit into
BAWES-Universe:mainfrom
sureshchouksey8:codex/v1-order-atomicity

Conversation

@sureshchouksey8
Copy link
Copy Markdown

@sureshchouksey8 sureshchouksey8 commented May 17, 2026

/claim #59

This is a narrow, non-overlapping API v1 order-placement atomicity hardening slice.

Problem:

  • api/modules/v1/controllers/OrderController.php::actionPlaceAnOrder() saves the order first, then can fail while saving order items, extra options, recalculating totals, or enforcing delivery minimums.
  • The later cleanup is best-effort and can be bypassed by early returns, which risks partial order state before payment processing.
  • Invalid store IDs also fall through to $restaurant->updateStats(), which can crash after returning the error response.

Changes:

  • Wrap API v1 pre-payment order assembly in a DB transaction.
  • Roll back item/extra-option/total/min-charge failures before external payment work starts.
  • Commit only after the order shell, items, extras, and pre-payment checks are consistent.
  • Preserve the existing post-commit payment failure cleanup behavior.
  • Guard the store stats update so invalid store IDs return the intended error instead of dereferencing null.
  • Add tests/check-api-v1-order-atomicity.sh as a focused static regression guard.

Validation run locally:

sh tests/check-api-v1-order-atomicity.sh
git diff --check

Note: I could not run php -l in this workspace because php is not installed on PATH.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced order placement reliability with improved transaction handling and error recovery
    • Strengthened data consistency safeguards to prevent incomplete or corrupted orders
    • Refined error handling to ensure orders are safely rolled back when validation fails

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e3bc068b-c1b8-4ee3-afc6-7d253ed84385

📥 Commits

Reviewing files that changed from the base of the PR and between bc485b0 and 31477ab.

📒 Files selected for processing (2)
  • api/modules/v1/controllers/OrderController.php
  • tests/check-api-v1-order-atomicity.sh

📝 Walkthrough

Walkthrough

OrderController::actionPlaceAnOrder is refactored to ensure transactional atomicity when placing orders. A database transaction wraps item and order persistence; failures during item-extra-option saving immediately break out of loops. Failures from order total recalculation are deferred instead of returned early. Transaction commit/rollback logic, post-processing safeguards, and narrowed order deletion ensure cleanup occurs correctly. A new shell test validates the transaction lifecycle and atomicity invariants.

Changes

Order placement transactional atomicity

Layer / File(s) Summary
Transaction initialization and item persistence failure handling
api/modules/v1/controllers/OrderController.php
Order placement state initializes with $response = null, $orderAssemblyCommitted flag, and explicit database transaction start. Failures during nested item-extra-option saving now break out of loops immediately instead of continuing iteration.
Order total calculation and error deferral
api/modules/v1/controllers/OrderController.php
updateOrderTotalPrice() failure handling changes to set $response object instead of returning immediately, allowing subsequent validation and transaction logic to execute uniformly.
Transaction commit/rollback and order cleanup
api/modules/v1/controllers/OrderController.php
Transaction commits on assembly success or rolls back on failure; post-processing safeguard prevents transaction leak. Order deletion narrows to committed-and-failed cases only, and updateStats() call is guarded behind $restaurant null check.
Atomicity validation guard test
tests/check-api-v1-order-atomicity.sh
New shell script validates transaction lifecycle (beginTransaction, commit, rollBack), orderAssemblyCommitted flag behavior, restaurant call guard, and absence of early returns before transaction cleanup in updateOrderTotalPrice failure path.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 A rabbit once placed an order bright,
But things went wrong mid-way through night,
Now transactions roll back with atomic care,
No half-built orders left in the air! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Harden API v1 order assembly atomicity' accurately and concisely describes the main change: wrapping order assembly in a database transaction to ensure atomicity and prevent partial order state persistence on failure.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant