Merge branch 'dev/chat' into 'main'

Dev/chat

See merge request jjimenez/safekat!341
This commit is contained in:
Alvaro
2024-09-26 13:14:14 +00:00
10 changed files with 220 additions and 47 deletions

View File

@ -59,6 +59,7 @@ class ChatController extends BaseController
$chat = $this->chatModel->getChatPresupuesto($chat_department_id, $presupuesto_id);
if ($chat) {
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
}
$data["chat"] = $chat;
return $this->response->setJSON($data);
@ -73,6 +74,8 @@ class ChatController extends BaseController
$chat = $this->chatModel->getChatPedido($chat_department_id, $pedido_id);
if ($chat) {
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
}
$data["chat"] = $chat;
return $this->response->setJSON($data);
@ -87,6 +90,8 @@ class ChatController extends BaseController
$chat = $this->chatModel->getChatFactura($chat_department_id, $factura_id);
if ($chat) {
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
}
$data["chat"] = $chat;
return $this->response->setJSON($data);
@ -177,6 +182,7 @@ class ChatController extends BaseController
foreach ($users as $user) {
$user->unreadMessages = $this->chatMessageModel->get_chat_unread_messages_count($user->id);
}
usort($users, fn($a, $b) => $a->unreadMessages < $b->unreadMessages);
return $this->response->setJSON($users);
}
public function get_chat_internal_contact(int $user_id)
@ -203,14 +209,35 @@ class ChatController extends BaseController
$response = [];
if($cliente_id){
$data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id);
$response["totalMessages"] = 0;
$response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]);
foreach ($response["chatFacturas"] as $key => $value) {
$response["totalMessages"] += $value->unreadMessages;
}
$response["chatPresupuestos"] = $this->chatModel->getClienteChatPresupuestos($data["presupuestos"]);
foreach ($response["chatPresupuestos"] as $key => $value) {
$response["totalMessages"] += $value->unreadMessages;
}
$response["chatPedidos"] = $this->chatModel->getClienteChatPedidos($data["pedidos"]);
foreach ($response["chatPedidos"] as $key => $value) {
$response["totalMessages"] += $value->unreadMessages;
}
$response["data"] = $data;
}else{
$response["internals"] = $this->chatModel->getChatDepartmentNotifications();
$internal_notifications = $this->chatModel->getChatInternalNotifications();
foreach ($internal_notifications as $value) {
$response["internals"][] = $value;
}
$response["totalMessages"] = 0;
foreach ($response["internals"] as $key => $value) {
$response["totalMessages"] += $value->unreadMessages;
}
}
return $this->response->setJSON($response);
}
public function get_chat_department_users(int $chat_department_id)
{
$data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);

View File

@ -59,7 +59,7 @@ class ChatDeparmentModel extends Model
'chat_departments.id',
'chat_departments.name',
'chat_departments.display',
'chat_department_users.user_id'
'chat_department_users.user_id',
]
)
->join(

View File

@ -118,4 +118,27 @@ class ChatMessageModel extends Model
->where("receiver_id", auth()->user()->id)->update();
return $messagesFromReceiver;
}
public function set_chat_department_messages_as_read(int $chat_id): int
{
$chatDepartmentModel = model(ChatDeparmentModel::class);
$chatModel = model(ChatModel::class);
if(auth()->user()->cliente_id){
$messagesFromReceiver = $this->builder()
->set("viewed", true)
->where("chat_id", $chat_id)
->whereNotIn("sender_id", [auth()->user()->id])->update();
}else{
$chat_department_id = $chatModel->find($chat_id)->chat_department_id;
$users_in_chat = array_map(fn($x) => $x->id, $chatDepartmentModel->getChatDepartmentUsers($chat_department_id));
$messagesFromReceiver = $this->builder()
->set("viewed", true)
->where("chat_id", $chat_id)
->whereNotIn("sender_id", $users_in_chat)
->update();
}
return $messagesFromReceiver;
}
}

View File

@ -2,6 +2,9 @@
namespace App\Models\Chat;
use App\Models\Facturas\FacturaModel;
use App\Models\Pedidos\PedidoModel;
use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Model;
use stdClass;
@ -11,7 +14,7 @@ class ChatModel extends Model
protected $table = 'chats';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
@ -204,14 +207,18 @@ class ChatModel extends Model
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("pedidos","pedidos.id = chats.pedido_id","left")
->join("chat_messages","pedidos.id = chats.pedido_id","left")
->whereNotIn("chat_messages.sender_id",[auth()->user()->id])
->whereIn("pedidos.id",$pedidos)
->get()->getResultObject();
$chatMessageModel = model(ChatMessageModel::class);
$count = 0;
foreach ($results as $row) {
$row->messages = $chatMessageModel->get_chat_messages($row->chatId);
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
$count++;
}
}
$row->unreadMessages=$count;
}
return $results;
}
@ -227,14 +234,18 @@ class ChatModel extends Model
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("facturas","facturas.id = chats.factura_id","left")
->join("chat_messages","chats.id = chat_messages.chat_id","left")
->whereNotIn("chat_messages.sender_id",[auth()->user()->id])
->whereIn("facturas.id",$facturas)
->get()->getResultObject();
$chatMessageModel = model(ChatMessageModel::class);
$count = 0;
foreach ($results as $row) {
$row->messages = $chatMessageModel->get_chat_messages($row->chatId);
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
$count++;
}
}
$row->unreadMessages=$count;
}
return $results;
}
@ -250,16 +261,99 @@ class ChatModel extends Model
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("presupuestos","presupuestos.id = chats.presupuesto_id","left")
->join("chat_messages","chats.id = chat_messages.chat_id","left")
->whereNotIn("chat_messages.sender_id",[auth()->user()->id])
->whereIn("presupuestos.id",$presupuestos)
->get()->getResultObject();
$chatMessageModel = model(ChatMessageModel::class);
$count = 0;
foreach ($results as $row) {
$row->messages = $chatMessageModel->get_chat_messages($row->chatId);
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message ) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
$count++;
}
}
$row->unreadMessages=$count;
}
return $results;
}
public function getChatDepartmentNotifications()
{
$chatMessageModel = model(ChatMessageModel::class);
$presupuestoModel = model(PresupuestoModel::class);
$facturaModel = model(FacturaModel::class);
$pedidoModel = model(PedidoModel::class);
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.presupuesto_id as presupuestoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("chat_department_users","chat_department_users.chat_department_id = chats.chat_department_id","left")
->where("chat_department_users.user_id",auth()->user()->id);
$rows = $q->get()->getResultObject();
foreach ($rows as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
$count = 0;
foreach ($messages as $m) {
if($m->viewed == false && $m->sender_id != auth()->user()->id)
$count++;
}
if($row->presupuestoId){
$row->model = $presupuestoModel->find($row->presupuestoId);
$row->uri = "/presupuestos/cosidotapablanda/edit/".$row->presupuestoId;
$row->title = $row->presupuestoId;
$row->avatar = "PRE";
$row->unreadMessages = $count;
}
elseif($row->pedidoId){
$row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/".$row->pedidoId;
$row->title = $row->pedidoId;
$row->avatar = "P";
$row->unreadMessages = $count;
}
elseif($row->facturaId){
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/facturas/edit/".$row->facturaId;
$row->avatar = "F";
$row->title = $row->facturaId;
$row->unreadMessages = $count;
}
}
return $rows;
}
public function getChatInternalNotifications()
{
$chatMessageModel = model(ChatMessageModel::class);
$userModel = model(UserModel::class);
$internalMessages = $chatMessageModel->builder()
->select([
"chat_id as chatId",
"sender_id",
"COUNT(viewed) as unreadMessages",
])
->where("receiver_id",auth()->user()->id)
->where("viewed",false)->get()->getResultObject();
foreach ($internalMessages as $m) {
if($m->sender_id){
$sender = $userModel->find($m->sender_id);
$m->model = $sender;
$m->title = $sender->username;
$m->chatDisplay = ($sender->first_name ?? "")." ".($sender->last_name ?? "");
$m->avatar = strtoupper(mb_substr($sender->first_name, 0, 1).mb_substr($sender->last_name, 0, 1));
$m->uri = "#";
}
}
return $internalMessages;
}
}

View File

@ -144,5 +144,4 @@
<?= $this->section("additionalExternalJs") ?>
<script type="module" src="<?= site_url('assets/js/safekat/pages/chatFactura.js') ?>"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/perfect-scrollbar/perfect-scrollbar.js') ?>"></script>
<!-- <script src="<?= site_url('js_loader/chat_js') ?>"></script> -->
<?= $this->endSection() ?>

View File

@ -146,5 +146,4 @@
<?= $this->section("additionalExternalJs") ?>
<script type="module" src="<?= site_url('assets/js/safekat/pages/chatPedido.js') ?>"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/perfect-scrollbar/perfect-scrollbar.js') ?>"></script>
<!-- <script src="<?= site_url('js_loader/chat_js') ?>"></script> -->
<?= $this->endSection() ?>

View File

@ -146,5 +146,4 @@
<?= $this->section("additionalExternalJs") ?>
<script type="module" src="<?= site_url('assets/js/safekat/pages/chatPresupuesto.js') ?>"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/perfect-scrollbar/perfect-scrollbar.js') ?>"></script>
<!-- <script src="<?= site_url('js_loader/chat_js') ?>"></script> -->
<?= $this->endSection() ?>

View File

@ -113,9 +113,11 @@ $picture = "/assets/img/default-user.png";
href="javascript:void(0);"
data-bs-toggle="dropdown"
data-bs-auto-close="outside"
id="message-notification-dropdown"
aria-expanded="false">
<span class="tf-icons ti-md ti ti-message-circle"></span>
<span id="chat-notification-number" class="badge bg-danger text-white badge-notifications"></span>
</a>
<div class="dropdown-menu dropdown-menu-end py-0">
<div class="dropdown-menu-header border-bottom">

View File

@ -70,23 +70,28 @@ class Chat {
this._handleGetChatList()
this.sendBtnMessageDepartment.on("click", this._sendMessage.bind(this))
}
initPresupuesto() {
this.chatType = "presupuesto"
this._handleGetChatList()
this.sendBtnMessageDepartment.on("click", this._sendMessage.bind(this))
this.messageInput.on("keypress", this._sendMessagePressKey.bind(this))
}
initPedido() {
this.chatType = "pedido"
this._handleGetChatList()
this.sendBtnMessageDepartment.on("click", this._sendMessage.bind(this))
this.messageInput.on("keypress", this._sendMessagePressKey.bind(this))
}
initFactura() {
this.chatType = "factura"
this._handleGetChatList()
this.sendBtnMessageDepartment.on("click", this._sendMessage.bind(this))
this.messageInput.on("keypress", this._sendMessagePressKey.bind(this))
}
initContacts() {
@ -136,6 +141,7 @@ class Chat {
_handleGetChatListSuccess(data) {
Object.values(data).map(row => {
this.chatList.append(this._getContact(row))
this.chatList.find(`#chat_${row.name}`).on("click", (event) => {
$(".chat-contact-list-item").removeClass("active")
$(event.currentTarget).parent().addClass("active")
@ -207,6 +213,8 @@ class Chat {
${row.display}
</p>
</div>
${row.unreadMessages ? `<span
class="badge badge-center rounded-pill bg-primary messages-unread-contact">${row.unreadMessages}</span>` : ""}
</a>
</li>
`
@ -267,7 +275,15 @@ class Chat {
this.chatHistory.append(chatItem)
return chatItem
}
_sendMessagePressKey(e){
if ( e.which == 13 ) {
e.preventDefault();
this._sendMessage()
}
}
_sendMessage() {
let messageText = this.messageInput.val()
const body = {
message: messageText,
@ -297,6 +313,7 @@ class Chat {
_handleListContacts() {
this.sideBar.find("#contact-list").removeClass("d-none")
this.sendBtnMessageInternal.on("click", this._sendMessageInternal.bind(this))
this.sendBtnMessageInternal.on("keypress", this._sendMessageInternalPressKey.bind(this))
let ajax = new Ajax(
"/chat/contacts",
null,
@ -318,15 +335,16 @@ class Chat {
this.sideBar.find("#contact-list").addClass("d-none")
}
this.sideBar.find(".contact-chat").on("click", (e) => {
$(e.currentTarget).find(".messages-unread-contact").empty()
$(".contact-chat").parent().removeClass("active")
$(".chat-contact-list-item").removeClass("active")
this.chatHistory.empty()
this.domItem.find("#chat-header-dropdown-users").addClass("d-none")
let userId = $(e.currentTarget).data("id")
$(e.currentTarget).parent().addClass('active')
this.receiverId = userId
this._handleGetSingleContact(userId)
this._setBtnInternal()
this.chatHistory.empty()
})
}
_handleListContactsError(err) {
@ -366,6 +384,12 @@ class Chat {
_handleGetSingleContactError(err) {
}
_sendMessageInternalPressKey(e){
if ( e.which == 13 ) {
e.preventDefault();
this._sendMessageInternal()
}
}
_sendMessageInternal() {
let messageText = this.messageInput.val()
@ -387,6 +411,7 @@ class Chat {
}
_sendMessageInternalSuccess(message) {
this.messageInput.val("")
this.chatHistory.empty()
this._handleGetSingleContact(this.receiverId)
}
_sendMessageInternalError(err) {
@ -413,7 +438,7 @@ class Chat {
</p>
</div>
${contact.unreadMessages ? `<span
class="badge badge-center rounded-pill bg-primary">${contact.unreadMessages}</span>` : ""}
class="badge badge-center rounded-pill bg-primary messages-unread-contact">${contact.unreadMessages}</span>` : ""}
</a>
</li>
`
@ -437,15 +462,30 @@ export const showNotificationMessages = (dom) => {
null,
null,
(data) => {
let totalMessages = 0
dom.empty()
$("#chat-notification-number").text(data.totalMessages ?? 0)
data?.internals?.map((e) => {
let numberOfMessages = e.unreadMessages
if(numberOfMessages > 0){
dom.append(
`
<li class="mb-2">
<a href="${e.uri}" class="d-flex align-items-center flex-grow">
<div class="avatar d-block flex-shrink-0">
<span class="avatar-initial rounded-circle bg-label-primary">${e.avatar}</span>
</div>
<div class="chat-contact-info flex-grow-1 ms-2">
<h6 class="chat-contact-name text-truncate m-0">[${e.title}] ${e.chatDisplay}</h6>
</div>
<span class="badge badge-center rounded-pill bg-primary p-1">${numberOfMessages}</span>
</a>
</li>
`
)
}
})
data?.chatPresupuestos?.map((e) => {
console.log(e)
let numberOfMessages = 0
e.messages.forEach(m => {
m.viewed == "1" ? numberOfMessages++ : null
});
totalMessages+= numberOfMessages
let numberOfMessages = e.unreadMessages
if(numberOfMessages > 0){
dom.append(
`
@ -465,18 +505,12 @@ export const showNotificationMessages = (dom) => {
}
})
data?.chatFacturas?.map((e) => {
console.log(e)
let numberOfMessages = 0
e.messages.forEach(m => {
m.viewed == "1" ? numberOfMessages++ : null
});
totalMessages+= numberOfMessages
let numberOfMessages = e.unreadMessages
if(numberOfMessages > 0){
dom.append(
`
<li class="">
<a href="/presupuestos/cosidotapablanda/edit/${e.facturaId}" class="d-flex align-items-center flex-grow">
<a href="/facturas/edit/${e.facturaId}" class="d-flex align-items-center flex-grow">
<div class="avatar d-block flex-shrink-0">
<span class="avatar-initial rounded-circle bg-label-primary">${e.facturaId}</span>
</div>
@ -491,19 +525,12 @@ export const showNotificationMessages = (dom) => {
}
})
data?.chatPedidos?.map((e) => {
console.log(e)
let numberOfMessages = 0
e.messages.forEach(m => {
m.viewed == "1" ? numberOfMessages++ : null
});
$("#chat-notification-number").text(numberOfMessages)
totalMessages+= numberOfMessages
let numberOfMessages = e.unreadMessages
if(numberOfMessages > 0){
dom.append(
`
<li class="">
<a href="/presupuestos/cosidotapablanda/edit/${e.pedidoId}" class="d-flex align-items-center flex-grow">
<a href="/pedidos/edit/${e.pedidoId}" class="d-flex align-items-center flex-grow">
<div class="avatar d-block flex-shrink-0">
<span class="avatar-initial rounded-circle bg-label-primary">${e.pedidoId}</span>
</div>
@ -517,7 +544,7 @@ export const showNotificationMessages = (dom) => {
)
}
})
$("#chat-notification-number").text(totalMessages)
},
(err) => { }
)

View File

@ -1,3 +1,6 @@
import {showNotificationMessages} from "../components/chat.js";
showNotificationMessages($("#chat-notification-list"))
showNotificationMessages($("#chat-notification-list"))
$("#message-notification-dropdown").on("click",(e) => {
showNotificationMessages($("#chat-notification-list"))
})