model = new OrderModel(); } public function __invoke(ServerRequestInterface $request) { $body = json_decode((string)$request->getBody(), true) ?? []; try { v::key('company_id', v::intType()->positive()) ->key('order_id', v::intType()->positive()) ->key('status_status', v::stringType()->notEmpty()->in(['Aberta', 'Finalizada', 'Cancelada']), true) ->key('order_flag', v::stringType()->notEmpty()->in(['Dinheiro', 'PIX', 'Cartão de Crédito', 'Cartão de Débito', 'p', 'a']), false) ->assert($body); } catch (ValidationException $e) { return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400); } $companyId = (int) $body['company_id']; $orderId = (int) $body['order_id']; $statusStatus = $body['status_status']; $orderFlag = array_key_exists('order_flag', $body) ? $body['order_flag'] : null; $statusId = $this->model->getStatusIdByName($statusStatus); if ($statusId === '') { return ResponseLib::sendFail("Invalid status_status Provided: '{$statusStatus}'", [], "E_VALIDATE")->withStatus(400); } $updated = $this->model->updateOrderStatus($orderId, $companyId, $statusId, $orderFlag); return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Order Status or Order Not Found", [], "E_DATABASE")->withStatus(204); } }