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
73 changes: 26 additions & 47 deletions agent/modules/v1/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,10 +1168,7 @@ public function actionRefund($order_uuid)

$transaction->rollBack();

return [
"operation" => "error",
"message" => $refund->errors
];
return $this->orderSaveError($refund, __METHOD__, "We've faced a problem creating the refund");
}

foreach ($itemsToRefund as $key => $qty) {
Expand Down Expand Up @@ -1207,10 +1204,7 @@ public function actionRefund($order_uuid)
if (!$refundItem->save()) {
$transaction->rollBack();

return [
"operation" => "error",
"message" => $refundItem->errors
];
return $this->orderSaveError($refundItem, __METHOD__, "We've faced a problem creating the refunded item");
}
}

Expand Down Expand Up @@ -1244,10 +1238,7 @@ public function actionRefund($order_uuid)

$transaction->rollBack();

return [
"operation" => "error",
"message" => $model->errors
];
return $this->orderSaveError($model, __METHOD__, "We've faced a problem updating the order status");
}

$transaction->commit();
Expand Down Expand Up @@ -1276,17 +1267,7 @@ public function actionUpdateOrderStatus($order_uuid, $store_uuid = null)
$model->order_status = Yii::$app->request->getBodyParam("order_status");

if (!$model->save()) {
if (isset($model->errors)) {
return [
"operation" => "error",
"message" => $model->errors
];
} else {
return [
"operation" => "error",
"message" => Yii::t('agent', "We've faced a problem updating the order status")
];
}
return $this->orderSaveError($model, __METHOD__, "We've faced a problem updating the order status");
}

$plugn_fee = 0;
Expand Down Expand Up @@ -1423,18 +1404,9 @@ public function actionRequestDriverFromArmada($order_uuid, $store_uuid = null)
}

if (!$model->save()) {
if (isset($model->errors)) {
return [
"operation" => "error",
"message" => $model->errors
];
} else {
return [
"operation" => "error",
"code" => 2,
"message" => Yii::t('agent', "We've faced a problem requesting driver from Armada")
];
}
return $this->orderSaveError($model, __METHOD__, "We've faced a problem requesting driver from Armada", [
"code" => 2
]);
}

return [
Expand Down Expand Up @@ -1584,18 +1556,9 @@ public function actionRequestDriverFromMashkor($order_uuid, $store_uuid = null)
}

if (!$model->save()) {
if (isset($model->errors)) {
return [
"operation" => "error",
"message" => $model->errors
];
} else {
return [
"operation" => "error",
"code" => 2,
"message" => Yii::t('agent', "We've faced a problem requesting driver from Mashkor")
];
}
return $this->orderSaveError($model, __METHOD__, "We've faced a problem requesting driver from Mashkor", [
"code" => 2
]);
}

return [
Expand Down Expand Up @@ -2408,4 +2371,20 @@ protected function findModel($order_uuid, $store_uuid = null)
throw new NotFoundHttpException('The requested record does not exist.');
}
}

private function orderSaveError($model, $context, $message, $extra = [])
{
Yii::error([
'message' => 'Agent order operation failed',
'model' => get_class($model),
'order_uuid' => $model->hasAttribute('order_uuid') ? $model->order_uuid : null,
'restaurant_uuid' => $model->hasAttribute('restaurant_uuid') ? $model->restaurant_uuid : null,
'errors' => $model->getErrors()
], $context);

return array_merge([
"operation" => "error",
"message" => Yii::t('agent', $message)
], $extra);
}
}
29 changes: 29 additions & 0 deletions tests/check-agent-order-refund-error-responses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -euo pipefail

target="agent/modules/v1/controllers/OrderController.php"

grep -q "orderSaveError" "$target"
grep -q "Agent order operation failed" "$target"
grep -q "We've faced a problem creating the refund" "$target"
grep -q "We've faced a problem creating the refunded item" "$target"
grep -q "We've faced a problem updating the order status" "$target"
grep -q "We've faced a problem requesting driver from Armada" "$target"
grep -q "We've faced a problem requesting driver from Mashkor" "$target"

if grep -Eq '"message"[[:space:]]*=>[[:space:]]*\$refund->errors' "$target"; then
echo "agent order controller still returns raw refund errors" >&2
exit 1
fi

if grep -Eq '"message"[[:space:]]*=>[[:space:]]*\$refundItem->errors' "$target"; then
echo "agent order controller still returns raw refunded item errors" >&2
exit 1
fi

if [ "$(grep -c "orderSaveError" "$target")" -lt 7 ]; then
echo "agent order controller is missing expected guarded order save failures" >&2
exit 1
fi

echo "Agent order refund error response guard passed."