AgentEscalationController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\AgentsModel;
  5. use Models\UserModel;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. class AgentEscalationController
  8. {
  9. private UserModel $userModel;
  10. private AgentsModel $agentsModel;
  11. public function __construct()
  12. {
  13. $this->userModel = new UserModel();
  14. $this->agentsModel = new AgentsModel();
  15. }
  16. public function __invoke(ServerRequestInterface $request)
  17. {
  18. $userId = (int) ($request->getAttribute('user_id') ?? 0);
  19. $body = json_decode((string) $request->getBody(), true) ?: [];
  20. $agentId = (int) ($body['id'] ?? 0);
  21. if ($userId <= 0) {
  22. return ResponseLib::sendFail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE')->withStatus(401);
  23. }
  24. if ($agentId <= 0) {
  25. return ResponseLib::sendFail('Missing or invalid id', [], 'E_VALIDATE')->withStatus(400);
  26. }
  27. try {
  28. $companyId = $this->userModel->getCompanyIdByUserId($userId);
  29. if ($companyId === null) {
  30. return ResponseLib::sendFail('User not found', [], 'E_NOT_FOUND')->withStatus(404);
  31. }
  32. $agent = $this->agentsModel->toggleAgentEscalation($companyId, $agentId);
  33. if ($agent === null) {
  34. return ResponseLib::sendFail('Agent not found', [], 'E_NOT_FOUND')->withStatus(404);
  35. }
  36. return ResponseLib::sendOk($agent);
  37. } catch (\Throwable $e) {
  38. return ResponseLib::sendFail('Failed to update agent escalation', [], 'E_GENERIC')->withStatus(500);
  39. }
  40. }
  41. }