add client to chat

This commit is contained in:
amazuecos
2025-03-17 06:50:35 +01:00
parent 55157d23aa
commit 3662613ba5
7 changed files with 248 additions and 108 deletions

View File

@ -897,7 +897,10 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route
$routes->get('department/presupuesto/(:num)/(:num)', 'ChatController::get_chat_presupuesto/$1/$2', ['as' => 'getChatPresupuesto']); $routes->get('department/presupuesto/(:num)/(:num)', 'ChatController::get_chat_presupuesto/$1/$2', ['as' => 'getChatPresupuesto']);
$routes->get('department/pedido/(:num)/(:num)', 'ChatController::get_chat_pedido/$1/$2', ['as' => 'getChatPedido']); $routes->get('department/pedido/(:num)/(:num)', 'ChatController::get_chat_pedido/$1/$2', ['as' => 'getChatPedido']);
$routes->get('department/factura/(:num)/(:num)', 'ChatController::get_chat_factura/$1/$2', ['as' => 'getChatFactura']); $routes->get('department/factura/(:num)/(:num)', 'ChatController::get_chat_factura/$1/$2', ['as' => 'getChatFactura']);
$routes->get('department/(:num)/users', 'ChatController::get_chat_department_users/$1', ['as' => 'getChatDepartmentUsers']); $routes->get('department/users/presupuesto/(:num)/(:num)', 'ChatController::get_chat_department_presupuesto_users/$1/$2', ['as' => 'getPresupuestoChatDepartmentUsers']);
$routes->get('department/users/pedido/(:num)/(:num)', 'ChatController::get_chat_department_pedido_users/$1/$2', ['as' => 'getPedidoChatDepartmentUsers']);
$routes->get('department/users/factura/(:num)/(:num)', 'ChatController::get_chat_department_factura_users/$1/$2', ['as' => 'getFacturaChatDepartmentUsers']);
$routes->post('department/user', 'ChatController::subscribe_to_chat_deparment/$1', ['as' => 'subscribeToChatDepartment']); $routes->post('department/user', 'ChatController::subscribe_to_chat_deparment/$1', ['as' => 'subscribeToChatDepartment']);
$routes->delete('department/user/(:num)', 'ChatController::delete_user_from_department/$1', ['as' => 'deleteUserFromDepartment']); $routes->delete('department/user/(:num)', 'ChatController::delete_user_from_department/$1', ['as' => 'deleteUserFromDepartment']);

View File

@ -207,15 +207,18 @@ class ChatController extends BaseController
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id); $dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
$chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($data["chat_department_id"],$data["model_id"]);
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]);
$cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->countAllResults(); $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('presupuesto_id',$data['model_id'])->countAllResults();
$auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults();
if($cliente_in_department_count == 0){ if($cliente_in_department_count == 0){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client']]); $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'presupuesto_id' => $data['model_id']]);
} }
if($auth_user_in_department_count){ if($auth_user_in_department_count){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id]); $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers);
if(!in_array(auth()->user()->id,$users_always_in_department)){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'presupuesto_id' => $data['model_id']]);
}
} }
foreach ($chatDepartmentUsers as $user) { foreach ($chatDepartmentUsers as $user) {
@ -223,6 +226,11 @@ class ChatController extends BaseController
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
} }
} }
foreach ($chatExternalUsers as $user) {
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse); return $this->response->setJSON($dataResponse);
} }
public function store_chat_message_pedido() public function store_chat_message_pedido()
@ -239,21 +247,30 @@ class ChatController extends BaseController
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id); $dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
$chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($data["chat_department_id"],$data["model_id"]);
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]);
$cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->countAllResults(); $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('pedido_id',$data['model_id'])->countAllResults();
$auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults();
if($cliente_in_department_count == 0){ if($cliente_in_department_count == 0){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client']]); $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'pedido_id' => $data['model_id']]);
} }
if($auth_user_in_department_count){ if($auth_user_in_department_count){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id]); $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers);
if(!in_array(auth()->user()->id,$users_always_in_department)){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'pedido_id' => $data['model_id']]);
} }
}
foreach ($chatDepartmentUsers as $user) { foreach ($chatDepartmentUsers as $user) {
if ($user->id != auth()->user()->id) { if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
} }
} }
foreach ($chatExternalUsers as $user) {
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse); return $this->response->setJSON($dataResponse);
} }
public function store_chat_message_factura() public function store_chat_message_factura()
@ -270,21 +287,30 @@ class ChatController extends BaseController
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id); $dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
$chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentFacturaUsers($data["chat_department_id"],$data["model_id"]);
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]);
$cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->countAllResults(); $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('factura_id',$data['model_id'])->countAllResults();
$auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults();
if($cliente_in_department_count == 0){ if($cliente_in_department_count == 0){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client']]); $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'factura_id' => $data['model_id']]);
} }
if($auth_user_in_department_count){ if($auth_user_in_department_count){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id]); $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers);
if(!in_array(auth()->user()->id,$users_always_in_department)){
$this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'factura_id' => $data['model_id']]);
} }
}
foreach ($chatDepartmentUsers as $user) { foreach ($chatDepartmentUsers as $user) {
if ($user->id != auth()->user()->id) { if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
} }
} }
foreach ($chatExternalUsers as $user) {
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse); return $this->response->setJSON($dataResponse);
} }
public function store_chat_message_single() public function store_chat_message_single()
@ -392,10 +418,23 @@ class ChatController extends BaseController
return $this->response->setJSON($response); return $this->response->setJSON($response);
} }
public function get_chat_department_users(int $chat_department_id) public function get_chat_department_presupuesto_users(int $chat_department_id,int $presupuesto_id)
{ {
$data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); $adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);
return $this->response->setJSON($data); $externalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($chat_department_id,$presupuesto_id);
return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]);
}
public function get_chat_department_pedido_users(int $chat_department_id,$pedido_id)
{
$adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);
$externalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($chat_department_id,$pedido_id);
return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]);
}
public function get_chat_department_factura_users(int $chat_department_id,$factura_id)
{
$adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);
$externalUsers = $this->chatDeparmentModel->getChatDeparmentFacturaUsers($chat_department_id,$factura_id);
return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]);
} }
public function get_chat_users_internal() public function get_chat_users_internal()
{ {
@ -837,8 +876,18 @@ class ChatController extends BaseController
} }
public function delete_user_from_department($chat_department_id) public function delete_user_from_department($chat_department_id)
{ {
$data = $this->request->getPost();
$user_id = auth()->user()->id; $user_id = auth()->user()->id;
$chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id); $adminExist = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)
->where('user_id',$user_id)
->where('pedido_id',null)
->where('factura_id',null)
->where('presupuesto_id',null)->countAllResults();
if($adminExist)
{
return $this->response->setJSON(["message" => lang('Chat.exit_admin_chat_wrong'),"status" => false]);
}
$chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id)->where($data['model_fk'],$data['model_id_fk']);
if($chatDepartmentUserEntity->countAllResults() > 0) if($chatDepartmentUserEntity->countAllResults() > 0)
{ {
$deleted = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id)->delete(); $deleted = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id)->delete();
@ -851,12 +900,21 @@ class ChatController extends BaseController
{ {
$data = $this->request->getPost(); $data = $this->request->getPost();
$user_id = auth()->user()->id; $user_id = auth()->user()->id;
$chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$data['chat_department_id'])->where('user_id',$user_id); $adminExist = $this->chatDeparmentUserModel->where('chat_department_id',$data['chat_department_id'])
->where('user_id',$user_id)
->where('pedido_id',null)
->where('factura_id',null)
->where('presupuesto_id',null)->countAllResults();
if($adminExist)
{
return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'),"status" => false]);
}
$chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$data['chat_department_id'])->where('user_id',$user_id)->where($data['model_fk'],$data['model_id_fk']);
if($chatDepartmentUserEntity->countAllResults() > 0) if($chatDepartmentUserEntity->countAllResults() > 0)
{ {
return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'),"status" => false]); return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'),"status" => false]);
}else{ }else{
$this->chatDeparmentUserModel->insert(["chat_department_id" => $data["chat_department_id"],"user_id" => $user_id]); $this->chatDeparmentUserModel->insert(["chat_department_id" => $data["chat_department_id"],"user_id" => $user_id,$data['model_fk'] => $data['model_id_fk']]);
return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_ok'), "status" => true]); return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_ok'), "status" => true]);
} }
} }

View File

@ -0,0 +1,45 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use CodeIgniter\Database\RawSql;
class AddForeignChatDepartmentUsers extends Migration
{
protected array $COLUMNS = [
"pedido_id" => [
"type" => "INT",
"constraint" => 16,
"unsigned" => true,
"null" => true
],
"presupuesto_id" => [
"type" => "INT",
"constraint" => 10,
"unsigned" => true,
"null" => true
],
"factura_id" => [
"type" => "INT",
"constraint" => 10,
"unsigned" => true,
"null" => true
],
];
public function up()
{
$this->forge->addColumn("chat_department_users",$this->COLUMNS);
$this->forge->addForeignKey('pedido_id', 'pedidos', 'id');
$this->forge->addForeignKey('factura_id', 'facturas', 'id');
$this->forge->addForeignKey('presupuesto_id', 'presupuestos', 'id');
}
public function down()
{
$this->forge->dropForeignKey("chat_department_users","pedido_id");
$this->forge->dropForeignKey("chat_department_users","factura_id");
$this->forge->dropForeignKey("chat_department_users","presupuesto_id");
$this->forge->dropColumn("chat_department_users",["pedido_id","factura_id","presupuesto_id"]);
}
}

View File

@ -40,9 +40,7 @@ return [
"subscribe_chat" => 'Subscribirse al chat', "subscribe_chat" => 'Subscribirse al chat',
"exit_chat_ok" => "Ha salido de la conversación correctamente. No recibirá más notificaciones a no ser que sea añadido de nuevo.", "exit_chat_ok" => "Ha salido de la conversación correctamente. No recibirá más notificaciones a no ser que sea añadido de nuevo.",
"exit_chat_wrong" => "No puede salir de la conversación porque no pertenece a ella.", "exit_chat_wrong" => "No puede salir de la conversación porque no pertenece a ella.",
"exit_admin_chat_wrong" => "Perteneces como personal a este chat y no puedes salirte. Contacta administrador para ello.",
"subscribe_chat_ok" => "Te has subscrito al chat correctamente. Ahora recibirás notificaciones cuando lleguen mensajes.", "subscribe_chat_ok" => "Te has subscrito al chat correctamente. Ahora recibirás notificaciones cuando lleguen mensajes.",
"subscribe_chat_wrong" => "Ya perteneces a este chat.", "subscribe_chat_wrong" => "Ya perteneces a este chat.",
]; ];

View File

@ -105,9 +105,10 @@ class ChatDeparmentModel extends Model
} }
return $departments; return $departments;
} }
public function getChatDepartmentUsers(int $chat_deparment_id)
public function getChatDeparmentUserQuery(int $chat_deparment_id)
{ {
$result = $this->builder() $query = $this->builder()
->select( ->select(
[ [
"users.*" "users.*"
@ -124,7 +125,37 @@ class ChatDeparmentModel extends Model
'left' 'left'
)->where("chat_departments.id", $chat_deparment_id) )->where("chat_departments.id", $chat_deparment_id)
->where("chat_department_users.deleted_at", null) ->where("chat_department_users.deleted_at", null)
->where("users.deleted_at",null)
->where("users.deleted_at", null);
return $query;
}
public function getChatDepartmentUsers(int $chat_deparment_id)
{
$result = $this->getChatDeparmentUserQuery($chat_deparment_id)
->where('chat_department_users.presupuesto_id', null)
->where('chat_department_users.pedido_id', null)
->where('chat_department_users.factura_id', null)
->get()->getResultObject();
return $result;
}
public function getChatDeparmentPresupuestoUsers(int $chat_deparment_id, int $presupuesto_id)
{
$result = $this->getChatDeparmentUserQuery($chat_deparment_id)
->where('chat_department_users.presupuesto_id', $presupuesto_id)
->get()->getResultObject();
return $result;
}
public function getChatDeparmentPedidoUsers(int $chat_deparment_id, int $pedido_id)
{
$result = $this->getChatDeparmentUserQuery($chat_deparment_id)
->where('chat_department_users.pedido_id', $pedido_id)
->get()->getResultObject();
return $result;
}
public function getChatDeparmentFacturaUsers(int $chat_deparment_id, int $factura_id)
{
$result = $this->getChatDeparmentUserQuery($chat_deparment_id)
->where('chat_department_users.factura_id', $factura_id)
->get()->getResultObject(); ->get()->getResultObject();
return $result; return $result;
} }

View File

@ -15,7 +15,10 @@ class ChatDeparmentUserModel extends Model
protected $protectFields = true; protected $protectFields = true;
protected $allowedFields = [ protected $allowedFields = [
"chat_department_id", "chat_department_id",
"user_id" "user_id",
"pedido_id",
"factura_id",
"presupuesto_id"
]; ];
protected bool $allowEmptyInserts = false; protected bool $allowEmptyInserts = false;
@ -47,6 +50,7 @@ class ChatDeparmentUserModel extends Model
protected $afterFind = []; protected $afterFind = [];
protected $beforeDelete = []; protected $beforeDelete = [];
protected $afterDelete = []; protected $afterDelete = [];
public function getChatDepartmentUser(int $user_id) public function getChatDepartmentUser(int $user_id)
{ {
return $this->db->table($this->table." t1") return $this->db->table($this->table." t1")
@ -55,6 +59,5 @@ class ChatDeparmentUserModel extends Model
->where("t1.user_id",$user_id) ->where("t1.user_id",$user_id)
->where("t1.deleted_at",null) ->where("t1.deleted_at",null)
->get()->getResultObject(); ->get()->getResultObject();
} }
} }

View File

@ -110,8 +110,7 @@ class Chat {
this._handleGetChatDirect() this._handleGetChatDirect()
setInterval(this._handleReloadChatDirectMessages.bind(this), 10000) setInterval(this._handleReloadChatDirectMessages.bind(this), 10000)
} }
initSelectClient() initSelectClient() {
{
this.selectClientUser = new ClassSelect(this.selectItem, `/chat/direct/client/users/select/${this.chatType}/${this.modelId}`, "Seleccione contacto", true) this.selectClientUser = new ClassSelect(this.selectItem, `/chat/direct/client/users/select/${this.chatType}/${this.modelId}`, "Seleccione contacto", true)
this.selectClientUser.init() this.selectClientUser.init()
this.selectItem.on('change', () => { this.selectItem.on('change', () => {
@ -228,7 +227,7 @@ class Chat {
this.domItem.find("#chat-header-users").empty() this.domItem.find("#chat-header-users").empty()
this.domItem.find("#chat-header-dropdown-users").removeClass("d-none") this.domItem.find("#chat-header-dropdown-users").removeClass("d-none")
let ajax = new Ajax( let ajax = new Ajax(
`/chat/department/${this.chatDeparmentId}/users`, `/chat/department/users/${this.chatType}/${this.chatDeparmentId}/${this.modelId}`,
null, null,
null, null,
this._getChatDeparmentUsersSuccess.bind(this), this._getChatDeparmentUsersSuccess.bind(this),
@ -238,17 +237,21 @@ class Chat {
ajax.get() ajax.get()
} }
_getChatDeparmentUsersSuccess(deparmentUsers) { _getChatDeparmentUsersSuccess(deparmentUsers) {
deparmentUsers.map((user) => { deparmentUsers.admin_users.map(user => this.appendChatDepartmentUser(user))
deparmentUsers.external_users.map(user => this.appendChatDepartmentUser(user, 'warning', 'user'))
}
appendChatDepartmentUser(user, color = "primary", icon = "user") {
this.domItem.find("#chat-header-users").append( this.domItem.find("#chat-header-users").append(
` `
<li class="chat-contact-list-item p-1"> <li class="chat-contact-list-item p-1">
<a class="d-flex align-items-center py-1" <div class="d-flex align-items-center py-1"
id="chat-contact-list-item-${user.id}"> id="chat-contact-list-item-${user.id}">
<div class="avatar d-block flex-shrink-0">
<span class="avatar-initial rounded-circle bg-label-primary">
${user?.first_name?.charAt(0) ?? "?"
+ user?.last_name?.charAt(0) ?? "?"}</span>
<div class="avatar-initial avatar-md px-2 py-2">
<span class="badge badge-center rounded-pill text-bg-${color}">
<i class="icon-base ti ti-xs ti-${icon}"></i>
</span>
</div> </div>
<div class="chat-contact-info flex-grow-1 ms-2"> <div class="chat-contact-info flex-grow-1 ms-2">
<h6 class="chat-contact-name text-truncate m-0">${user?.first_name ?? "" + " " + <h6 class="chat-contact-name text-truncate m-0">${user?.first_name ?? "" + " " +
@ -257,12 +260,11 @@ class Chat {
${user.username} ${user.username}
</p> </p>
</div> </div>
</a> </div>
</li> </li>
` `
) )
})
} }
_getChatDeparmentUsersError(err) { } _getChatDeparmentUsersError(err) { }
@ -442,6 +444,7 @@ class Chat {
_sendMessageSuccess(data) { _sendMessageSuccess(data) {
this.messageInput.val("") this.messageInput.val("")
this._getChatMessage(this.chatDeparmentId) this._getChatMessage(this.chatDeparmentId)
this._getChatDeparmentUsers()
} }
_sendMessageError(err) { _sendMessageError(err) {
console.error(err) console.error(err)
@ -739,10 +742,12 @@ class Chat {
this._handleReloadChatDirectMessages(); this._handleReloadChatDirectMessages();
} }
_handleUpdateChatMessagesUnreadError() { } _handleUpdateChatMessagesUnreadError() { }
_handleRequestExitChatDeparment() _handleRequestExitChatDeparment() {
{
const ajax = new Ajax('/chat/department/user/' + this.chatDeparmentId, const ajax = new Ajax('/chat/department/user/' + this.chatDeparmentId,
null, {
model_fk: this.chatType,
model_id_fk: this.modelId
},
null, null,
(response) => { (response) => {
if (response.status) { if (response.status) {
@ -759,11 +764,12 @@ class Chat {
) )
ajax.delete(); ajax.delete();
} }
_handleRequestSubscribeChatDeparment() _handleRequestSubscribeChatDeparment() {
{
const ajax = new Ajax('/chat/department/user', const ajax = new Ajax('/chat/department/user',
{ {
chat_department_id : this.chatDeparmentId chat_department_id: this.chatDeparmentId,
model_fk: this.chatType,
model_id_fk: this.modelId
}, },
null, null,
(response) => { (response) => {
@ -781,10 +787,8 @@ class Chat {
) )
ajax.post(); ajax.post();
} }
_handleExitChatDepartment() _handleExitChatDepartment() {
{ if (this.chatDeparmentId) {
if(this.chatDeparmentId)
{
alertConfirmationDelete('¿Estás seguro de salir de la conversación?').then((result) => { alertConfirmationDelete('¿Estás seguro de salir de la conversación?').then((result) => {
if (result.isConfirmed) { if (result.isConfirmed) {
this._handleRequestExitChatDeparment() this._handleRequestExitChatDeparment()
@ -792,10 +796,8 @@ class Chat {
}) })
} }
} }
_handleSubscribeChatDepartment() _handleSubscribeChatDepartment() {
{ if (this.chatDeparmentId) {
if(this.chatDeparmentId)
{
alertConfirmAction('¿Estás seguro de subscribirte al chat?').then((result) => { alertConfirmAction('¿Estás seguro de subscribirte al chat?').then((result) => {
if (result.isConfirmed) { if (result.isConfirmed) {
this._handleRequestSubscribeChatDeparment() this._handleRequestSubscribeChatDeparment()