diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 5a60b886..a0109cd4 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -906,6 +906,8 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $routes->get('hebra/presupuesto/(:num)', "ChatController::get_hebra_presupuesto/$1", ["as" => "getHebraPresupuesto"]); $routes->get('hebra/pedido/(:num)', "ChatController::get_hebra_pedido/$1", ["as" => "getHebraPedido"]); $routes->get('hebra/factura/(:num)', "ChatController::get_hebra_factura/$1", ["as" => "getHebraFactura"]); + + $routes->get('users/notifications-unviewed/(:num)','ChatController::get_notifications_not_viewed_from_chat/$1'); }); diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index 0be4e8ed..872235f4 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -60,7 +60,8 @@ class ChatController extends BaseController $data = $this->chatDeparmentModel->getChatDepartments(); return $this->response->setJSON($data); } - public function get_chat_department_select(){ + public function get_chat_department_select() + { $query = $this->chatDeparmentModel->getChatDepartmentSelect($this->request->getGet("q")); return $this->response->setJSON($query->get()->getResultObject()); } @@ -142,9 +143,9 @@ class ChatController extends BaseController $this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id); $this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id); - if($chat->chat_department_id){ + if ($chat->chat_department_id) { return view(static::$viewPath . 'messageChatPresupuesto', $this->viewData); - }else{ + } else { return view(static::$viewPath . 'messageChatInternal', $this->viewData); } } @@ -160,9 +161,9 @@ class ChatController extends BaseController $auth_user = auth()->user(); $this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id); $this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id); - if($chat->chat_department_id ){ + if ($chat->chat_department_id) { return view(static::$viewPath . 'messageChatPedido', $this->viewData); - }else{ + } else { return view(static::$viewPath . 'messageChatInternal', $this->viewData); } } @@ -179,9 +180,9 @@ class ChatController extends BaseController $this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id); $this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id); - if($chat->chat_department_id){ + if ($chat->chat_department_id) { return view(static::$viewPath . 'messageChatFactura', $this->viewData); - }else{ + } else { return view(static::$viewPath . 'messageChatInternal', $this->viewData); } } @@ -411,7 +412,7 @@ class ChatController extends BaseController ) ->where("deleted_at", null) ->whereNotIn("id", [auth()->user()->id]) - ->where("cliente_id",$p->cliente_id); + ->where("cliente_id", $p->cliente_id); if ($this->request->getGet("q")) { $query->groupStart() ->orLike("users.username", $this->request->getGet("q")) @@ -433,7 +434,7 @@ class ChatController extends BaseController ) ->where("deleted_at", null) ->whereNotIn("id", [auth()->user()->id]) - ->where("cliente_id",$p->cliente()->id); + ->where("cliente_id", $p->cliente()->id); if ($this->request->getGet("q")) { $query->groupStart() ->orLike("users.username", $this->request->getGet("q")) @@ -455,7 +456,7 @@ class ChatController extends BaseController ) ->where("deleted_at", null) ->whereNotIn("id", [auth()->user()->id]) - ->where("cliente_id",$f->cliente_id); + ->where("cliente_id", $f->cliente_id); if ($this->request->getGet("q")) { $query->groupStart() ->orLike("users.username", $this->request->getGet("q")) @@ -619,53 +620,79 @@ class ChatController extends BaseController public function datatable_messages() { $auth_user_id = auth()->user()->id; + $isAdmin = auth()->user()->inGroup('admin'); $query = $this->chatModel->getQueryDatatable($auth_user_id); return DataTable::of($query) ->edit('created_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i")) ->edit('updated_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i")) ->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName) ->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id)) - ->add("action", fn($q) => ["type" => "direct","modelId" => $q->id]) + ->add("action", fn($q) => [ + "type" => "direct", + "modelId" => $q->id, + "isAdmin" => $isAdmin, + "lang" => [ + "view_chat" => lang('Chat.view_chat'), + "view_by_alt_message" => lang('Chat.view_by_alt_message') + ] + ]) ->toJson(true); } public function datatable_presupuesto_messages() { $auth_user_id = auth()->user()->id; + $isAdmin = auth()->user()->inGroup('admin'); $query = $this->chatModel->getQueryDatatableMessagePresupuesto($auth_user_id); return DataTable::of($query) ->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "") ->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "") ->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName) ->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id)) - ->add("action", fn($q) => ["type" => "presupuesto","modelId" => $q->id]) + ->add("action", fn($q) => ["type" => "presupuesto", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [ + "view_chat" => lang('Chat.view_chat'), + "view_by_alt_message" => lang('Chat.view_by_alt_message') + ]]) ->toJson(true); } public function datatable_pedido_messages() { $auth_user_id = auth()->user()->id; + $isAdmin = auth()->user()->inGroup('admin'); $query = $this->chatModel->getQueryDatatableMessagePedido($auth_user_id); return DataTable::of($query) ->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "") ->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "") ->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName) ->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id)) - ->add("action", fn($q) => ["type" => "pedido","modelId" => $q->id]) + ->add("action", fn($q) => ["type" => "pedido", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [ + "view_chat" => lang('Chat.view_chat'), + "view_by_alt_message" => lang('Chat.view_by_alt_message') + ]]) ->toJson(true); } public function datatable_factura_messages() { $auth_user_id = auth()->user()->id; + $isAdmin = auth()->user()->inGroup('admin'); $query = $this->chatModel->getQueryDatatableMessageFactura($auth_user_id); return DataTable::of($query) ->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "") ->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "") ->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName) ->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id)) - ->add("action", fn($q) => ["type" => "factura","modelId" => $q->id]) + ->add("action", fn($q) => ["type" => "factura", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [ + "view_chat" => lang('Chat.view_chat'), + "view_by_alt_message" => lang('Chat.view_by_alt_message') + ]]) ->toJson(true); } + public function get_notifications_not_viewed_from_chat(int $chat_id) + { + $data = $this->chatModel->getUsersNotificationNotViewedFromChat($chat_id); + return $this->response->setJSON(["data" => $data, "chat_id" => $chat_id]); + } public function store_new_direct_message() { $bodyData = $this->request->getPost(); @@ -702,7 +729,7 @@ class ChatController extends BaseController ]); } $users = $this->chatDeparmentModel->getChatDepartmentUsers($bodyData["chat_department_id"]); - $bodyData["users"] = array_map(fn($q) => $q->id,$users); + $bodyData["users"] = array_map(fn($q) => $q->id, $users); dd(1); $this->chatModel->createNewDirectChat(...$bodyData); return $this->response->setJSON(["message" => lang("Chat.new_message_ok"), "status" => true]); @@ -770,7 +797,7 @@ class ChatController extends BaseController { $bodyData = $this->request->getPost(); $messageService = service('messages'); - $r = $messageService->createErrorMessagePresupuesto("Error",$bodyData['presupuesto_id']); - return $this->response->setJSON(["message" => "ok","data" => $r]); + $r = $messageService->createErrorMessagePresupuesto("Error", $bodyData['presupuesto_id']); + return $this->response->setJSON(["message" => "ok", "data" => $r]); } } diff --git a/ci4/app/Language/es/Chat.php b/ci4/app/Language/es/Chat.php index 62d3ad0c..e9f32692 100644 --- a/ci4/app/Language/es/Chat.php +++ b/ci4/app/Language/es/Chat.php @@ -33,5 +33,7 @@ return [ "check_as_unviewed" => "Marcar como no leídos", "add_notification_message" => "Envía a los usuarios añadidos una notificación con los mensajes presentes en el chat.", "chat_title_presupuesto" => 'Presupuesto[{title,string,0}][{id}]', - "no_messages_notification" => 'No hay mensajes que revisar por el momento' + "no_messages_notification" => 'No hay mensajes que revisar por el momento', + "view_chat" => 'Ver chat', + "view_by_alt_message" => 'Muestra los usuarios que han leído o no los mensajes' ]; \ No newline at end of file diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index 1681f814..f729ec04 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -11,7 +11,7 @@ class ChatDeparmentModel extends Model protected $primaryKey = 'id'; protected $useAutoIncrement = true; protected $returnType = 'object'; - protected $useSoftDeletes = false; + protected $useSoftDeletes = true; protected $protectFields = true; protected $allowedFields = [ "name", @@ -107,7 +107,7 @@ class ChatDeparmentModel extends Model } public function getChatDepartmentUsers(int $chat_deparment_id) { - $result = $this->db->table('chat_departments') + $result = $this->builder() ->select( [ "users.*" @@ -123,6 +123,8 @@ class ChatDeparmentModel extends Model "chat_department_users.user_id = users.id", 'left' )->where("chat_departments.id", $chat_deparment_id) + ->where("chat_department_users.deleted_at",null) + ->where("users.deleted_at",null) ->get()->getResultObject(); return $result; } diff --git a/ci4/app/Models/Chat/ChatDeparmentUserModel.php b/ci4/app/Models/Chat/ChatDeparmentUserModel.php index 717c738a..9dbde312 100644 --- a/ci4/app/Models/Chat/ChatDeparmentUserModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentUserModel.php @@ -52,7 +52,9 @@ class ChatDeparmentUserModel extends Model return $this->db->table($this->table." t1") ->select("chat_departments.*") ->join("chat_departments","t1.chat_department_id = chat_departments.id","left") - ->where("t1.user_id",$user_id)->get()->getResultObject(); + ->where("t1.user_id",$user_id) + ->where("t1.deleted_at",null) + ->get()->getResultObject(); } } diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index 0a110442..4055fca9 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -344,9 +344,9 @@ class ChatModel extends Model if ($row->presupuestoId) { $row->model = $presupuestoModel->find($row->presupuestoId); if ($auth_user->cliente_id) { - $row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto"; + $row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto"; } else { - $row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId."#accordionChatPresupuesto"; + $row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto"; } $row->title = $row->presupuestoId; if ($row->chatDepartmentId) { @@ -369,7 +369,7 @@ class ChatModel extends Model $rows_new[] = $row; } elseif ($row->facturaId) { $row->model = $facturaModel->find($row->facturaId); - $row->uri = "/chat/factura/" . $row->facturaId."#accordionChatFactura"; + $row->uri = "/chat/factura/" . $row->facturaId . "#accordionChatFactura"; $row->avatar = "F"; if ($row->chatDepartmentId) { $row->chatDisplay .= "[INTERNAL]"; @@ -409,7 +409,7 @@ class ChatModel extends Model if ($row->presupuestoId) { $row->model = $presupuestoModel->find($row->presupuestoId); if ($auth_user->cliente_id) { - $row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto"; + $row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto"; } else { $row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto"; } @@ -677,7 +677,7 @@ class ChatModel extends Model return $q->get()->getFirstRow(); } /** - * Check if all messages of a chat sended to an user have been viewed. + * Check if all messages of a chat sent to an user have been viewed. * * @param integer $chat_id * @param integer $user_id @@ -689,7 +689,7 @@ class ChatModel extends Model ->join("chat_messages", "chat_messages.chat_id = chats.id", 'left') ->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", 'left') ->where("chats.id", $chat_id) - ->where("chat_notifications.user_id", $user_id) + // ->where("chat_notifications.user_id", $user_id) ->where("chat_notifications.viewed", false); $unread_messages_count = $q->countAllResults(); if ($unread_messages_count > 0) { @@ -699,24 +699,54 @@ class ChatModel extends Model } return $result; } + /** + * Return users who did not read the message. + * + * @param integer $chat_id + * @return array True : All messages readed + */ + public function getUsersNotificationNotViewedFromChat(int $chat_id): array + { + $q = $this->builder()->distinct()->select( + [ + "CONCAT(users.first_name,' ',users.last_name) as userFullName", + "users.username as userName", + "SUM(chat_notifications.viewed) as viewed_count", + "COUNT(chat_notifications.id) as notification_count" + ] + + ) + ->join("chat_messages", "chat_messages.chat_id = chats.id", 'left') + ->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", 'left') + ->join("users", "users.id = chat_notifications.user_id", 'left') + ->where("chats.id", $chat_id) + ->where("chat_notifications.deleted_at", null) + ->get()->getResultArray(); + return $q; + } public function getQueryDatatable(int $user_id): BaseBuilder { $query = $this->builder() ->select([ "chats.id", "chats.created_at", - "chats.updated_at", + "cm.updated_at", "chats.title", ]) ->join("chat_users", "chat_users.chat_id = chats.id", "left") + ->join("chat_messages cm", "chats.id = cm.chat_id", "left") ->where("chat_department_id", null) ->where("pedido_id", null) ->where("presupuesto_id", null) ->where("factura_id", null) ->where("chat_users.user_id", $user_id) - ->orderBy("created_at", "DESC") - ->groupBy("chats.id"); + ->where("cm.updated_at = ( + SELECT cm2.updated_at + FROM chat_messages cm2 + WHERE cm2.chat_id = chats.id + ORDER BY cm2.updated_at DESC LIMIT 1 + )"); return $query; } @@ -726,18 +756,23 @@ class ChatModel extends Model ->select([ "chats.id", "chats.created_at", - "chats.updated_at", + "cm.updated_at", "chats.title", ]) ->join("chat_users", "chats.id = chat_users.chat_id", "left") ->join("presupuestos", "presupuestos.id = chats.presupuesto_id", 'left') - ->where("presupuesto_id is NOT NULL", NULL, FALSE) - ->groupBy("chats.id"); + ->join("chat_messages cm", "chats.id = cm.chat_id", "left") + ->where("chats.presupuesto_id is NOT NULL", NULL, FALSE) + ->where("cm.updated_at = ( + SELECT cm2.updated_at + FROM chat_messages cm2 + WHERE cm2.chat_id = chats.id + ORDER BY cm2.updated_at DESC LIMIT 1 + )"); if (auth()->user()->inGroup("admin") == false) { $query->where('presupuestos.cliente_id', auth()->user()->cliente_id) ->where("chat_department_id is NOT NULL", NULL, FALSE); } - $query->orderBy("created_at", "DESC"); return $query; } public function getQueryDatatableMessagePedido(int $user_id): BaseBuilder @@ -746,19 +781,24 @@ class ChatModel extends Model ->select([ "chats.id", "chats.created_at", - "chats.updated_at", + "cm.updated_at", "chats.title", ]) ->join("chat_users", "chat_users.chat_id = chats.id", "left") ->join("pedidos_linea", "pedidos_linea.pedido_id = chats.pedido_id", 'left') + ->join("chat_messages cm", "chats.id = cm.chat_id", "left") ->join("presupuestos", "presupuestos.id = pedidos_linea.presupuesto_id", 'left') ->where("chats.pedido_id is NOT NULL", NULL, FALSE) - ->groupBy("chats.id"); + ->where("cm.updated_at = ( + SELECT cm2.updated_at + FROM chat_messages cm2 + WHERE cm2.chat_id = chats.id + ORDER BY cm2.updated_at DESC LIMIT 1 + )"); if (auth()->user()->inGroup("admin") == false) { $query->where('presupuestos.cliente_id', auth()->user()->cliente_id) ->where("chat_department_id is NOT NULL", NULL, FALSE); } - $query->orderBy("created_at", "DESC"); return $query; } public function getQueryDatatableMessageFactura(int $user_id): BaseBuilder @@ -767,20 +807,25 @@ class ChatModel extends Model ->select([ "chats.id", "chats.created_at", - "chats.updated_at", + "cm.updated_at", "chats.title", ]) ->join("chat_users", "chat_users.chat_id = chats.id", "left") + ->join("chat_messages cm", "chats.id = cm.chat_id", "left") ->join("facturas", "facturas.id = chats.factura_id", "left") ->where("chats.factura_id is NOT NULL", NULL, FALSE) - ->groupBy("chats.id"); + ->where("cm.updated_at = ( + SELECT cm2.updated_at + FROM chat_messages cm2 + WHERE cm2.chat_id = chats.id + ORDER BY cm2.updated_at DESC LIMIT 1 + )"); if (auth()->user()->inGroup("admin") == false) { if (auth()->user()->inGroup("admin") == false) { $query->where('facturas.cliente_id', auth()->user()->cliente_id) ->where("chat_department_id is NOT NULL", NULL, FALSE); } } - $query->orderBy("created_at", "DESC"); return $query; } public function createNewDirectChat(string $title, string $message, array $users) diff --git a/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php b/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php index 2288d375..07428fcb 100644 --- a/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php +++ b/ci4/app/Views/themes/vuexy/form/mensajes/mensajesView.php @@ -66,6 +66,7 @@ endSection() ?> section("additionalExternalJs") ?> +