|
|
@@ -4,6 +4,7 @@ namespace Controllers;
|
|
|
|
|
|
use Libs\ResponseLib;
|
|
|
use Models\OrderModel;
|
|
|
+use Models\UserModel;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
use Respect\Validation\Validator as v;
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
@@ -11,10 +12,12 @@ use Respect\Validation\Exceptions\ValidationException;
|
|
|
class OrderCreateController
|
|
|
{
|
|
|
private OrderModel $model;
|
|
|
+ private UserModel $userModel;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->model = new OrderModel();
|
|
|
+ $this->userModel = new UserModel();
|
|
|
}
|
|
|
|
|
|
public function __invoke(ServerRequestInterface $request)
|
|
|
@@ -22,34 +25,37 @@ class OrderCreateController
|
|
|
$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);
|
|
|
+ ->key('user_name', v::stringType()->notEmpty()->alnum('_'))
|
|
|
+ ->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'];
|
|
|
+ $userName = $body['user_name'];
|
|
|
+
|
|
|
+ // Buscar user_id correspondente ao user_name
|
|
|
+ $userId = $this->getUserIdByUserName($userName, $companyId);
|
|
|
+
|
|
|
+ if (!$userId) {
|
|
|
+ return ResponseLib::sendFail("Usuário '{$userName}' não encontrado", [], "E_USER_NOT_FOUND")->withStatus(404);
|
|
|
+ }
|
|
|
|
|
|
- // 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,
|
|
|
@@ -63,4 +69,15 @@ class OrderCreateController
|
|
|
? ResponseLib::sendOk(['created' => true, 'order_id' => $created])
|
|
|
: ResponseLib::sendFail("Failed to create order", [], "E_DATABASE")->withStatus(500);
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ private function getUserIdByUserName(string $username, int $companyId): ?int
|
|
|
+ {
|
|
|
+ $users = $this->userModel->getUsersByCompany($companyId);
|
|
|
+ foreach ($users as $user) {
|
|
|
+ if ($user['user_name'] === $username) {
|
|
|
+ return (int) $user['user_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|