From eb604f329344a13c1fe5ace87d726919063b2408 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 13 Nov 2024 00:47:47 +0100 Subject: [PATCH] fix : mensajes internos, arreglar total numero mensajes --- ci4/app/Controllers/Chat/ChatController.php | 29 ++++++++++++------- ci4/app/Models/Chat/ChatDeparmentModel.php | 11 +++++-- ci4/app/Models/Chat/ChatMessageModel.php | 7 +++++ ci4/app/Models/Chat/ChatModel.php | 11 +++++++ .../themes/vuexy/components/chat_general.php | 7 +++-- .../themes/vuexy/main/menus/clientes_menu.php | 22 +++++++------- .../themes/vuexy/main/menus/mensajes_menu.php | 2 +- httpdocs/assets/js/safekat/components/chat.js | 5 ++-- 8 files changed, 64 insertions(+), 30 deletions(-) diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index 4633aed8..93c5bb58 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -178,20 +178,21 @@ class ChatController extends BaseController "receiver_id" => $data["receiver_id"], ] ); - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id" => auth()->user()->id]); + $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id" =>$data["receiver_id"]]); $dataResponse = $this->chatMessageModel->find($chat_message_id); return $this->response->setJSON($dataResponse); } public function get_chat_internal_contacts() { - if (auth()->user()->cliente_id) { - return $this->response->setJSON([]); - } - $users = $this->userModel->builder() - ->where("cliente_id", null) - ->whereNotIn("id", [auth()->user()->id]) + $auth_user = auth()->user(); + if ($auth_user->cliente_id) { + $users = $this->chatModel->getOpenChatCliente($auth_user->id); + }else{ + $users = $this->userModel->builder() + ->whereNotIn("id", [$auth_user->id]) ->where("deleted_at", null) ->get()->getResultObject(); + } foreach ($users as $user) { $user->unreadMessages = $this->chatMessageModel->get_chat_messages_count($user->id); } @@ -200,11 +201,11 @@ class ChatController extends BaseController } public function get_chat_internal_contact(int $user_id) { - if (auth()->user()->cliente_id) { - return $this->response->setJSON([]); - } + $auth_user = auth()->user(); + // if ($auth_user->cliente_id) { + // return $this->response->setJSON([]); + // } $users = $this->userModel->builder() - ->where("cliente_id", null) ->where("deleted_at", null) ->where("id", $user_id) ->get()->getFirstRow(); @@ -224,6 +225,8 @@ class ChatController extends BaseController $data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id); $response["totalMessages"] = 0; $response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]); + $mensajes_directos = $this->chatModel->getChatDirectMessageNotifications(); + foreach ($response["chatFacturas"] as $key => $value) { $response["totalMessages"] += $value->unreadMessages; } @@ -231,6 +234,10 @@ class ChatController extends BaseController foreach ($response["chatPresupuestos"] as $key => $value) { $response["totalMessages"] += $value->unreadMessages; } + foreach ($mensajes_directos as $value) { + $response["internals"][] = $value; + $response["totalMessages"] += $value->unreadMessages; + } $response["chatPedidos"] = $this->chatModel->getClienteChatPedidos($data["pedidos"]); foreach ($response["chatPedidos"] as $key => $value) { $response["totalMessages"] += $value->unreadMessages; diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index 55a46179..51af2f67 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -55,7 +55,7 @@ class ChatDeparmentModel extends Model $userModel = model(UserModel::class); $chatMessageModel = model(ChatMessageModel::class); - $query = $this->db->table('chat_departments') + $query = $this->builder() ->select( [ @@ -82,6 +82,7 @@ class ChatDeparmentModel extends Model "left" ) ->where("chat_departments.type", $type); + // if (auth()->user()->cliente_id == null) { // $query->where("chat_department_users.user_id", auth()->user()->id); // } @@ -89,16 +90,22 @@ class ChatDeparmentModel extends Model $results = $query->get()->getResultArray(); // Create the desired structure $departments = []; - foreach ($results as $row) { $departmentName = $row['name']; + $totalMessages = 0; // If the department is not yet added to the array, initialize it if (!isset($departments[$departmentName])) { + if($row['chatId']){ + $data["messages"] = $chatMessageModel->get_chat_messages($row['chatId']); + $totalMessages = count($data["messages"]); + + } $departments[$departmentName] = [ 'id' => $row['id'], 'name' => $row['name'], 'display' => $row['display'], + 'totalMessages' => $totalMessages, 'users' => [] // Initialize users as an empty array ]; } diff --git a/ci4/app/Models/Chat/ChatMessageModel.php b/ci4/app/Models/Chat/ChatMessageModel.php index 2e1fc469..ce3b2b0a 100644 --- a/ci4/app/Models/Chat/ChatMessageModel.php +++ b/ci4/app/Models/Chat/ChatMessageModel.php @@ -115,6 +115,13 @@ class ChatMessageModel extends Model ->where("receiver_id", auth()->user()->id)->countAllResults(); return $messagesFromReceiver; } + public function get_chat_department_messages_count(int $chat_id) : int + { + $chatDepartmentMessagesCount = $this->builder() + ->where("id",$chat_id) + ->countAllResults(); + return $chatDepartmentMessagesCount; + } public function get_chat_messages_count(int $sender_id): int { $messagesFromReceiver = $this->builder() diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index dc11eb6a..d296be7e 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -599,6 +599,17 @@ class ChatModel extends Model $userModel = model(UserModel::class); return $userModel->find($first_message->sender_id); } + public function getOpenChatCliente(int $user_id) : array + { + $q = $this->builder()->distinct()->select([ + "users.*" + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("users","users.id = chat_messages.sender_id","left") + ->where("chat_messages.receiver_id",$user_id); + return $q->get()->getResultObject(); + + } } diff --git a/ci4/app/Views/themes/vuexy/components/chat_general.php b/ci4/app/Views/themes/vuexy/components/chat_general.php index 617523e7..b35914ce 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_general.php +++ b/ci4/app/Views/themes/vuexy/components/chat_general.php @@ -37,7 +37,7 @@
@@ -83,13 +84,13 @@ aria-expanded="false"> - + --> diff --git a/ci4/app/Views/themes/vuexy/main/menus/clientes_menu.php b/ci4/app/Views/themes/vuexy/main/menus/clientes_menu.php index 71e53116..59ee8395 100644 --- a/ci4/app/Views/themes/vuexy/main/menus/clientes_menu.php +++ b/ci4/app/Views/themes/vuexy/main/menus/clientes_menu.php @@ -1,11 +1,12 @@ user()->inGroup('cliente-admin') || auth()->user()->inGroup('cliente-editor')) { if (auth()->user()->can('perfil.edit') || auth()->user()->can('direcciones.menu')) { - ?> +?> + - user()->can('clientes.menu') || - auth()->user()->can('plantilla-tarifa.menu')) { - ?> + if ( + auth()->user()->can('clientes.menu') || + auth()->user()->can('plantilla-tarifa.menu') + ) { + ?> + - - - - +?> \ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/main/menus/mensajes_menu.php b/ci4/app/Views/themes/vuexy/main/menus/mensajes_menu.php index 44913967..7faa33da 100644 --- a/ci4/app/Views/themes/vuexy/main/menus/mensajes_menu.php +++ b/ci4/app/Views/themes/vuexy/main/menus/mensajes_menu.php @@ -2,7 +2,7 @@ /** * MENU MENSAJES */ -if (auth()->user()->inGroup('beta')) { +if (auth()->user()->inGroup('beta') || auth()->user()->inGroup('cliente-editor')) { ?> ` @@ -344,7 +344,6 @@ class Chat { try { if (contacts.length) { - console.log(contacts) contacts.map((c, index) => { this._addContactToList(c) @@ -469,7 +468,7 @@ class Chat {
${contact?.first_name ?? "" + " " + contact?.last_name ?? ""}

- ${contact.username} + ${contact?.cliente_id ? "[CLIENTE]" : ""}${contact.username}

${contact.unreadMessages ? `