messages departments section

This commit is contained in:
amazuecos
2025-03-20 08:20:50 +01:00
parent d417db18e2
commit 6e96beeec8
26 changed files with 1045 additions and 306 deletions

View File

@ -329,18 +329,19 @@ class ChatModel extends Model
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.viewed) as unreadMessages"
])
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chat_notifications.user_id", auth()->user()->id)
->where("chat_notifications.viewed", false)
->where("chats.chat_department_id is NOT NULL", NULL, FALSE);
->where("chats.chat_department_id is NOT NULL", NULL, FALSE)
->groupBy('chats.id');
$rows = $q->get()->getResultObject();
$auth_user = auth()->user();
$rows_new = [];
foreach ($rows as $row) {
$row->unreadMessages = 0;
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
@ -355,7 +356,6 @@ class ChatModel extends Model
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "PRE";
$row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId);
$rows_new[] = $row;
} elseif ($row->pedidoId) {
$row->model = $pedidoModel->find($row->pedidoId);
@ -365,7 +365,6 @@ class ChatModel extends Model
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "P";
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
@ -375,7 +374,6 @@ class ChatModel extends Model
$row->chatDisplay .= "[INTERNAL]";
}
$row->title = $row->facturaId;
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
$rows_new[] = $row;
}
}
@ -587,7 +585,7 @@ class ChatModel extends Model
}
return $data;
}
public function countUnreadMessagePresupuesto($presupuesto_id): int|string
public function countUnreadMessagePresupuesto(int $presupuesto_id,int $chat_department_id): int|string
{
return $this->builder()->select()
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
@ -595,6 +593,7 @@ class ChatModel extends Model
->where("chats.presupuesto_id", $presupuesto_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)
->where('chats.chat_department_id',$chat_department_id)
->countAllResults();
}
public function countUnreadMessagePedido($pedido_id): int|string
@ -711,9 +710,7 @@ class ChatModel extends Model
[
"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"
"chat_notifications.viewed"
]
)
@ -722,6 +719,26 @@ class ChatModel extends Model
->join("users", "users.id = chat_notifications.user_id", 'left')
->where("chats.id", $chat_id)
->where("chat_notifications.deleted_at", null)
->where('chat_notifications.viewed',false)
->get()->getResultArray();
return $q;
}
public function getUsersNotificationViewedFromChat(int $chat_id): array
{
$q = $this->builder()->distinct()->select(
[
"CONCAT(users.first_name,' ',users.last_name) as userFullName",
"users.username as userName",
"chat_notifications.viewed"
]
)
->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)
->where('chat_notifications.viewed',true)
->get()->getResultArray();
return $q;
}
@ -903,16 +920,17 @@ class ChatModel extends Model
public function setAsViewedChatUserNotifications(int $chat_id, int $user_id)
{
$query = $this->builder()
->select("chat_messages.id")
->select("chat_notifications.id as notificationId")
->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left')
->join('chat_notifications', 'chat_notifications.chat_message_id = chat_messages.id', 'left')
->where('chat_notifications.user_id',$user_id)
->where('chat_messages.chat_id', $chat_id)
->get()->getResultObject();
$chat_messages_ids = array_map(fn($q) => $q->id, $query);
$this->db->table("chat_notifications")
->where("user_id", $user_id)
->whereIn("chat_message_id", $chat_messages_ids)
->update(["viewed" => true]);
$chat_messages_ids = array_map(fn($q) => $q->notificationId, $query);
$chatNotificationModel = model(ChatNotification::class);
if($chat_messages_ids){
$chatNotificationModel->setNotificationsAsViewed($chat_messages_ids);
}
}
public function setAsViewedChatUserMessages(int $chat_id, int $user_id)
{
@ -924,16 +942,17 @@ class ChatModel extends Model
public function setAsUnviewedChatUserNotifications(int $chat_id, int $user_id)
{
$query = $this->builder()
->select("chat_messages.id")
->select("chat_notifications.id as notificationId")
->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left')
->join('chat_notifications', 'chat_notifications.chat_message_id = chat_messages.id', 'left')
->where('chat_notifications.user_id',$user_id)
->where('chat_messages.chat_id', $chat_id)
->get()->getResultObject();
$chat_messages_ids = array_map(fn($q) => $q->id, $query);
$this->db->table("chat_notifications")
->where("user_id", $user_id)
->whereIn("chat_message_id", $chat_messages_ids)
->update(["viewed" => false]);
$chat_messages_ids = array_map(fn($q) => $q->notificationId, $query);
$chatNotificationModel = model(ChatNotification::class);
if($chat_messages_ids){
$chatNotificationModel->setNotificationsAsUnViewed($chat_messages_ids);
}
}
public function setAsUnviewedChatUserMessages(int $chat_id, int $user_id)