Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions api/modules/v1/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public function actionPlaceAnOrder($id) {
}
}


$response = [];
$response = null;
$orderAssemblyCommitted = false;
$transaction = Yii::$app->db->beginTransaction();

if ($order->save()) {

Expand Down Expand Up @@ -207,6 +208,7 @@ public function actionPlaceAnOrder($id) {
'operation' => 'error',
'message' => $orderItemExtraOption->errors,
];
break 2;
}
}
}
Expand All @@ -217,6 +219,7 @@ public function actionPlaceAnOrder($id) {
'operation' => 'error',
'message' => $orderItem->getErrors()
];
break;
}
}
} else {
Expand All @@ -243,13 +246,13 @@ public function actionPlaceAnOrder($id) {
if ($response == null) {

if (!$order->updateOrderTotalPrice()) {
return [
$response = [
'operation' => 'error',
'message' => $order->getErrors()
];
}

if ($order->order_mode == Order::ORDER_MODE_DELIVERY && $order->subtotal < $order->restaurantDelivery->min_charge) {
if ($response == null && $order->order_mode == Order::ORDER_MODE_DELIVERY && $order->subtotal < $order->restaurantDelivery->min_charge) {
$response = [
'operation' => 'error',
'message' => 'Minimum order amount ' . Yii::$app->formatter->asCurrency(
Expand All @@ -260,6 +263,12 @@ public function actionPlaceAnOrder($id) {
];
}

if ($response == null) {
$transaction->commit();
$orderAssemblyCommitted = true;
} else {
$transaction->rollBack();
}

//if payment method not cash redirect customer to payment gateway

Expand Down Expand Up @@ -488,8 +497,11 @@ public function actionPlaceAnOrder($id) {
}
}

if (!$orderAssemblyCommitted && $transaction->getIsActive()) {
$transaction->rollBack();
}

if (array_key_exists('operation', $response) && $response['operation'] == 'error') {
if ($orderAssemblyCommitted && is_array($response) && array_key_exists('operation', $response) && $response['operation'] == 'error') {
$order->delete();
}
} else {
Expand All @@ -501,7 +513,9 @@ public function actionPlaceAnOrder($id) {

//for https://pogi.sentry.io/issues/3889482226/?project=5220572&query=is%3Aunresolved&referrer=issue-stream&stream_index=0

$restaurant->updateStats();
if ($restaurant) {
$restaurant->updateStats();
}

return $response;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/check-api-v1-order-atomicity.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env sh
set -eu

controller="api/modules/v1/controllers/OrderController.php"

grep -q 'Yii::\$app->db->beginTransaction()' "$controller"
grep -q '\$transaction->commit();' "$controller"
grep -q '\$transaction->rollBack();' "$controller"
grep -q '\$orderAssemblyCommitted = true;' "$controller"
grep -q '!\$orderAssemblyCommitted && \$transaction->getIsActive()' "$controller"
grep -q '\$orderAssemblyCommitted && is_array(\$response)' "$controller"
grep -q 'if (\$restaurant) {' "$controller"

if grep -n 'if (!\$order->updateOrderTotalPrice())' "$controller" | grep -q .; then
start_line=$(grep -n 'if (!\$order->updateOrderTotalPrice())' "$controller" | head -1 | cut -d: -f1)
end_line=$((start_line + 8))
if sed -n "${start_line},${end_line}p" "$controller" | grep -q 'return \['; then
echo "v1 order total failure still returns before transaction cleanup" >&2
exit 1
fi
fi

echo "api v1 order atomicity guard passed"