InteractionDetailsController.php 1.7 KB

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