| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace Controllers;
- use Libs\Logger;
- use Libs\Payload;
- use Models\InteractionDetailsModel;
- use Models\UserModel;
- use Psr\Http\Message\ServerRequestInterface;
- class InteractionDetailsController
- {
- private UserModel $userModel;
- private InteractionDetailsModel $interactionDetailsModel;
- public function __construct()
- {
- $this->userModel = new UserModel();
- $this->interactionDetailsModel = new InteractionDetailsModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $userId = (int) ($request->getAttribute('user_id') ?? 0);
- $conversationId = (int) (($request->getQueryParams()['conversation_id'] ?? 0));
- if ($userId <= 0) {
- return Payload::fail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE', 401);
- }
- if ($conversationId <= 0) {
- return Payload::fail('Missing or invalid conversation_id', [], 'E_VALIDATE', 400);
- }
- try {
- $companyId = $this->userModel->getCompanyIdByUserId($userId);
- if ($companyId === null) {
- return Payload::fail('User not found', [], 'E_NOT_FOUND', 404);
- }
- $data = $this->interactionDetailsModel->getInteractionDetails($companyId, $conversationId);
- if ($data === null) {
- return Payload::fail('Conversation not found', [], 'E_NOT_FOUND', 404);
- }
- return Payload::ok($data);
- } catch (\Throwable $e) {
- Logger::error('Failed to load interaction details', ['error' => $e->getMessage()]);
- return Payload::fail('Failed to load interaction details', [], 'E_GENERIC', 500);
- }
- }
- }
|