InteractionDetailsController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Controllers;
  3. use Libs\Logger;
  4. use Libs\Payload;
  5. use Models\InteractionDetailsModel;
  6. use Models\UserModel;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. class InteractionDetailsController
  9. {
  10. private UserModel $userModel;
  11. private InteractionDetailsModel $interactionDetailsModel;
  12. public function __construct()
  13. {
  14. $this->userModel = new UserModel();
  15. $this->interactionDetailsModel = new InteractionDetailsModel();
  16. }
  17. public function __invoke(ServerRequestInterface $request)
  18. {
  19. $userId = (int) ($request->getAttribute('user_id') ?? 0);
  20. $conversationId = (int) (($request->getQueryParams()['conversation_id'] ?? 0));
  21. if ($userId <= 0) {
  22. return Payload::fail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE', 401);
  23. }
  24. if ($conversationId <= 0) {
  25. return Payload::fail('Missing or invalid conversation_id', [], 'E_VALIDATE', 400);
  26. }
  27. try {
  28. $companyId = $this->userModel->getCompanyIdByUserId($userId);
  29. if ($companyId === null) {
  30. return Payload::fail('User not found', [], 'E_NOT_FOUND', 404);
  31. }
  32. $data = $this->interactionDetailsModel->getInteractionDetails($companyId, $conversationId);
  33. if ($data === null) {
  34. return Payload::fail('Conversation not found', [], 'E_NOT_FOUND', 404);
  35. }
  36. return Payload::ok($data);
  37. } catch (\Throwable $e) {
  38. Logger::error('Failed to load interaction details', ['error' => $e->getMessage()]);
  39. return Payload::fail('Failed to load interaction details', [], 'E_GENERIC', 500);
  40. }
  41. }
  42. }