diff --git a/agent/modules/v1/controllers/OrderController.php b/agent/modules/v1/controllers/OrderController.php index e4e281d97..1be05f44f 100644 --- a/agent/modules/v1/controllers/OrderController.php +++ b/agent/modules/v1/controllers/OrderController.php @@ -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) { @@ -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"); } } @@ -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(); @@ -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; @@ -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 [ @@ -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 [ @@ -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); + } } diff --git a/tests/check-agent-order-refund-error-responses.sh b/tests/check-agent-order-refund-error-responses.sh new file mode 100644 index 000000000..08e85d289 --- /dev/null +++ b/tests/check-agent-order-refund-error-responses.sh @@ -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."