chat select cliente contactos

This commit is contained in:
amazuecos
2025-04-02 20:19:16 +02:00
parent 9d071e462c
commit 69f6d6cbeb
8 changed files with 166 additions and 92 deletions

View File

@ -6,6 +6,7 @@ use App\Controllers\Configuracion\ConfigVariables;
use App\Entities\Chat\ChatEntity;
use App\Entities\Chat\ChatMessageEntity;
use App\Entities\Chat\ChatNotificationEntity;
use App\Entities\Clientes\ClienteContactoEntity;
use App\Entities\Usuarios\UserEntity;
use App\Models\Chat\ChatDeparmentModel;
use App\Models\Chat\ChatDeparmentUserModel;
@ -13,11 +14,13 @@ use App\Models\Chat\ChatMessageModel;
use App\Models\Chat\ChatModel;
use App\Models\ChatNotification;
use App\Models\ChatUser;
use App\Models\Clientes\ClienteContactoModel;
use App\Models\Configuracion\ConfigVariableModel;
use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Config\BaseService;
use App\Services\EmailService;
use Exception;
class ChatService extends BaseService
{
@ -31,6 +34,7 @@ class ChatService extends BaseService
protected PresupuestoModel $presupuestoModel;
protected ChatDeparmentModel $chatDepartmentModel;
protected ChatDeparmentUserModel $chatDepartmentUserModel;
protected ClienteContactoModel $clienteContactoModel;
protected ConfigVariableModel $configVariables;
protected array $modelFkMap = [
"presupuesto" => "presupuesto_id",
@ -43,6 +47,7 @@ class ChatService extends BaseService
$this->emailService = service('emailService'); // Usar el servicio de email de Safekat y no el de CI4
$this->userModel = model(UserModel::class);
$this->chatModel = model(ChatModel::class);
$this->clienteContactoModel = model(ClienteContactoModel::class);
$this->chatMessageModel = model(ChatMessageModel::class);
$this->chatNotificationModel = model(ChatNotification::class);
$this->chatDepartmentUserModel = model(ChatDeparmentUserModel::class);
@ -64,13 +69,17 @@ class ChatService extends BaseService
$this->chatEntity = $this->chatModel->find($chatId);
}
if ($data["client"]) {
$cliente_in_department = $this->chatDepartmentUserModel
->where('chat_department_id', $chatDepartmentId)
->where('user_id', $data['client'])
->where($this->modelFkMap[$model], $modelId)
->first();
if ($cliente_in_department == null) {
$this->chatDepartmentUserModel->insert(['chat_department_id' => $chatDepartmentId, 'user_id' => $data['client'], $this->modelFkMap[$model] => $modelId]);
$cliente_contacto = $this->clienteContactoModel->find($data["client"]);
if ($cliente_contacto) {
$userClienteContacto = $cliente_contacto->cliente()->user();
$cliente_in_department = $this->chatDepartmentUserModel
->where('chat_department_id', $chatDepartmentId)
->where('user_id', $userClienteContacto->id)
->where($this->modelFkMap[$model], $modelId)
->first();
if ($cliente_in_department == null) {
$this->chatDepartmentUserModel->insert(['chat_department_id' => $chatDepartmentId, 'user_id' => $userClienteContacto->id, $this->modelFkMap[$model] => $modelId]);
}
}
}
$userAdminDepartment = $this->chatDepartmentUserModel
@ -91,9 +100,9 @@ class ChatService extends BaseService
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $this->chatEntity->id, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$chatMessageEntity = $this->chatMessageModel->find($chat_message_id);
if ($data["client"]) {
$userClient = $this->userModel->find($data['client']);
if ($userClient) {
$this->sendMessageNotificationEmail($chatMessageEntity, $userClient);
$cliente_contacto = $this->clienteContactoModel->find($data["client"]);
if ($cliente_contacto) {
$this->sendMessageNotificationToClienteContacto($chatMessageEntity, $cliente_contacto);
}
}
return $chatMessageEntity;
@ -156,6 +165,7 @@ class ChatService extends BaseService
);
return true;
}
public function getHebras(string $model, int $modelId): array
{
$chats = $this->chatModel->where('chat_department_id', null)
@ -195,6 +205,23 @@ class ChatService extends BaseService
]);
return $this->emailService->send($subject, $message, $toEmail);
}
public function sendMessageNotificationToClienteContacto(ChatMessageEntity $chatMessageEntity, ClienteContactoEntity $clienteContacto): bool
{
$users = auth()->getProvider();
$toEmail = $clienteContacto->email;
$subject = '[Safekat]' . lang('Chat.mail.mail_subject') . ' - ' . $chatMessageEntity->chat()->title;
$message = view('themes/vuexy/mail/messageNotificationClienteContacto', [
"header" => lang('Chat.mail.mail_subject'),
"contacto" => $clienteContacto,
"data" => $chatMessageEntity,
]);
if ($toEmail) {
return $this->emailService->send($subject, $message, $toEmail);
} else {
throw new Exception('Cliente contacto no tiene email');
}
}
}