model = new OrderModel(); } public function __invoke(ServerRequestInterface $request) { $body = json_decode((string)$request->getBody(), true) ?? []; try { // Validação dos campos essenciais, esperando 'status_status' v::key('table_id', v::intType()->positive()) ->key('user_id', v::intType()->positive()) ->key('company_id', v::intType()->positive()) ->key('order_name', v::stringType()->notEmpty()->alnum(' ')) ->key('order_phone', v::stringType()->notEmpty()->length(8, 20)) ->key('status_status', v::stringType()->notEmpty()->in(['Aberta', 'Finalizada', 'Cancelada'])) ->assert($body); } catch (ValidationException $e) { return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400); } $tableId = (int) $body['table_id']; $userId = (int) $body['user_id']; $companyId = (int) $body['company_id']; $orderName = $body['order_name']; $orderPhone = $body['order_phone']; $statusStatus = $body['status_status']; // Converte o status_status para status_id usando o Model $statusId = $this->model->getStatusIdByName($statusStatus); if ($statusId === null) { // Isso pode acontecer se o status_status enviado não for mapeado no banco return ResponseLib::sendFail("Invalid status_status provided: '{$statusStatus}'", [], "E_VALIDATE")->withStatus(400); } // AQUI ESTAVA O ERRO: Passando $statusStatus (string) em vez de $statusId (int) $created = $this->model->createOrder( $tableId, $userId, $companyId, $orderName, $orderPhone, $statusId ); return $created ? ResponseLib::sendOk(['created' => true, 'order_id' => $created]) : ResponseLib::sendFail("Failed to create order", [], "E_DATABASE")->withStatus(500); } }