|
|
@@ -4,7 +4,7 @@ namespace Controllers;
|
|
|
|
|
|
use Libs\ResponseLib;
|
|
|
use Models\OrderItemModel;
|
|
|
-use Models\ProductModel; // Mantenha se for usar o enriquecimento de produto
|
|
|
+use Models\ProductModel;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
use Respect\Validation\Validator as v;
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
@@ -23,33 +23,29 @@ class OrderItemGetController
|
|
|
$body = json_decode((string)$request->getBody(), true) ?? [];
|
|
|
|
|
|
try {
|
|
|
- // company_id e order_id são obrigatórios para listar os itens de um pedido
|
|
|
- v::key('company_id', v::intType()->positive()) // Validação para company_id
|
|
|
- ->key('order_id', v::intType()->positive())
|
|
|
- ->assert($body);
|
|
|
+ v::key('company_id', v::intType()->positive())->assert($body);
|
|
|
+ if (isset($body['order_id'])) {
|
|
|
+ v::key('order_id', v::intType()->positive())->assert($body);
|
|
|
+ }
|
|
|
} catch (ValidationException $e) {
|
|
|
return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
|
|
|
}
|
|
|
|
|
|
- $companyId = (int) $body['company_id']; // Obtenha o company_id do body
|
|
|
- $orderId = (int) $body['order_id'];
|
|
|
+ $companyId = (int) $body['company_id'];
|
|
|
+ $orderId = isset($body['order_id']) ? (int) $body['order_id'] : null;
|
|
|
|
|
|
- // CORREÇÃO AQUI: Passe o $companyId para o método do Model
|
|
|
$orderItems = $this->model->getOrderItemsByOrderId($orderId, $companyId);
|
|
|
-
|
|
|
- $productModel = new ProductModel(); // Instancie ProductModel aqui
|
|
|
+
|
|
|
+ $productModel = new ProductModel();
|
|
|
foreach ($orderItems as &$item) {
|
|
|
- // Certifique-se que getProductById em ProductModel aceita $companyId
|
|
|
- $product = $productModel->getProductById($item['product_id'], $companyId);
|
|
|
+ $product = $productModel->getProductById($item['product_id'], $companyId);
|
|
|
$item['product_details'] = $product;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if (!empty($orderItems)) {
|
|
|
return ResponseLib::sendOk($orderItems);
|
|
|
}
|
|
|
|
|
|
- // Atualize a mensagem de falha para refletir a nova validação
|
|
|
- return ResponseLib::sendFail("No order items found for the given order ID or company ID", [], "E_DATABASE")->withStatus(404);
|
|
|
+ return ResponseLib::sendFail("No order items found for the given data", [], "E_DATABASE")->withStatus(404);
|
|
|
}
|
|
|
}
|