UserInfoController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\UserModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Exceptions\ValidationException;
  7. use Respect\Validation\Validator as val;
  8. class UserInfoController
  9. {
  10. private UserModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new UserModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  18. if ($companyId <= 0) {
  19. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  20. }
  21. $body = json_decode((string)$request->getBody(), true) ?? [];
  22. try {
  23. val::key('user_id', val::intType()->positive())
  24. ->assert($body);
  25. } catch (ValidationException $e) {
  26. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
  27. }
  28. $userId = (int)$body['user_id'];
  29. $row = $this->model->getUserInfoById($userId, $companyId);
  30. if (!$row) {
  31. return ResponseLib::sendFail('User Not Found', [], 'E_DATABASE')->withStatus(204);
  32. }
  33. $data = [
  34. 'id' => (int)($row['user_id'] ?? 0),
  35. 'nome' => $row['user_name'] ?? null,
  36. 'email' => $row['user_email'] ?? null,
  37. 'telefone' => $row['user_phone'] ?? null,
  38. 'cpf' => $row['user_cpf'] ?? null,
  39. 'dataNasc' => isset($row['user_birthdate']) ? (int)$row['user_birthdate'] : null,
  40. 'kyc' => isset($row['user_kyc']) ? (int)$row['user_kyc'] : null,
  41. 'roleId' => isset($row['role_id']) ? (int)$row['role_id'] : null,
  42. 'status' => $row['user_flag'] ?? null,
  43. 'endereco' => $row['user_address'] ?? null,
  44. 'cidade' => $row['user_city'] ?? null,
  45. 'estado' => $row['user_state'] ?? null,
  46. 'cep' => $row['user_zip'] ?? null,
  47. 'pais' => $row['user_country'] ?? null,
  48. ];
  49. return ResponseLib::sendOk($data);
  50. }
  51. }