message refactor

This commit is contained in:
amazuecos
2025-03-24 08:12:06 +01:00
parent 61af547135
commit c0d54e28b7
26 changed files with 994 additions and 677 deletions

View File

@ -2,6 +2,7 @@
namespace App\Models\Chat;
use App\Entities\Chat\ChatEntity;
use App\Models\ChatNotification;
use App\Models\ChatUser;
use App\Models\Facturas\FacturaModel;
@ -16,7 +17,7 @@ class ChatModel extends Model
protected $table = 'chats';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'object';
protected $returnType = ChatEntity::class;
protected $useSoftDeletes = true;
protected $protectFields = true;
protected $allowedFields = [
@ -315,12 +316,6 @@ class ChatModel extends Model
}
public function getChatDepartmentNotifications()
{
$chatDeparmentModel = model(ChatDeparmentModel::class);
$chatMessageModel = model(ChatMessageModel::class);
$presupuestoModel = model(PresupuestoModel::class);
$facturaModel = model(FacturaModel::class);
$pedidoModel = model(PedidoModel::class);
$q = $this->builder()
->select([
"chats.id as chatId",
@ -329,7 +324,7 @@ class ChatModel extends Model
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.viewed) as unreadMessages"
"COUNT(chat_notifications.id) as unreadMessages"
])
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
@ -343,36 +338,28 @@ class ChatModel extends Model
$rows_new = [];
foreach ($rows as $row) {
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
// $row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
$row->title = $row->presupuestoId;
if ($row->chatDepartmentId) {
$row->chatDisplay = $row->model->titulo;
} else {
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "PRE";
$rows_new[] = $row;
} elseif ($row->pedidoId) {
$row->model = $pedidoModel->find($row->pedidoId);
// $row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/" . $row->pedidoId . "#accordionChatPedido";
$row->title = $row->pedidoId;
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "P";
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
// $row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/factura/" . $row->facturaId . "#accordionChatFactura";
$row->avatar = "F";
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->title = $row->facturaId;
$rows_new[] = $row;
}
@ -381,10 +368,6 @@ class ChatModel extends Model
}
public function getChatInternalNotifications()
{
$presupuestoModel = model(PresupuestoModel::class);
$facturaModel = model(FacturaModel::class);
$pedidoModel = model(PedidoModel::class);
$q = $this->builder()
->select([
"chats.id as chatId",
@ -393,52 +376,41 @@ class ChatModel extends Model
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.title as chatDisplay",
"COUNT(chat_messages.id) 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", null);
->where("chats.chat_department_id", null)
->groupBy('chats.id');
$auth_user = auth()->user();
$rows = $q->get()->getResultObject();
$rows_new = [];
foreach ($rows as $row) {
$row->unreadMessages = 0;
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
$row->title = $row->presupuestoId;
if ($row->chatDepartmentId) {
$row->chatDisplay = $row->model->titulo;
} else {
$row->chatDisplay .= "[INTERNAL]";
}
$row->chatDisplay .= "[INTERNAL]";
$row->avatar = "PRE";
$row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId);
$rows_new[] = $row;
} elseif ($row->pedidoId) {
$row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/" . $row->pedidoId . "#accordionChatFactura";
$row->title = $row->pedidoId;
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->chatDisplay .= "[INTERNAL]";
$row->avatar = "P";
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/factura/edit/" . $row->facturaId . "#accordionChatFactura";
$row->avatar = "F";
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->chatDisplay .= "[INTERNAL]";
$row->title = $row->facturaId;
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
$rows_new[] = $row;
}
}
@ -585,7 +557,7 @@ class ChatModel extends Model
}
return $data;
}
public function countUnreadMessagePresupuesto(int $presupuesto_id,int $chat_department_id): int|string
public function countUnreadMessagePresupuesto(int $presupuesto_id): int|string
{
return $this->builder()->select()
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
@ -593,7 +565,6 @@ 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
@ -675,6 +646,7 @@ class ChatModel extends Model
->orderBy("chat_messages.created_at", 'ASC');
return $q->get()->getFirstRow();
}
/**
* Check if all messages of a chat sent to an user have been viewed.
*
@ -682,13 +654,12 @@ class ChatModel extends Model
* @param integer $user_id
* @return boolean True : All messages readed
*/
public function isMessageChatViewed(int $chat_id, int $user_id): bool
public function isMessageChatViewed(int $chat_message_id): bool
{
$q = $this->builder()->select(["chat_notifications.id"])
$q = $this->builder()->select(["chat_notifications.id as cnId"])
->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_messages.id", $chat_message_id)
->where("chat_notifications.viewed", false);
$unread_messages_count = $q->countAllResults();
if ($unread_messages_count > 0) {
@ -704,7 +675,7 @@ class ChatModel extends Model
* @param integer $chat_id
* @return array True : All messages readed
*/
public function getUsersNotificationNotViewedFromChat(int $chat_id): array
public function getUsersNotificationNotViewedFromChat(int $chat_message_id): array
{
$q = $this->builder()->distinct()->select(
[
@ -717,13 +688,12 @@ 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')
->join("users", "users.id = chat_notifications.user_id", 'left')
->where("chats.id", $chat_id)
->where("chat_messages.id", $chat_message_id)
->where("chat_notifications.deleted_at", null)
->where('chat_notifications.viewed',false)
->get()->getResultArray();
return $q;
}
public function getUsersNotificationViewedFromChat(int $chat_id): array
public function getUsersNotificationViewedFromChat(int $chat_message_id): array
{
$q = $this->builder()->distinct()->select(
[
@ -736,9 +706,9 @@ 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')
->join("users", "users.id = chat_notifications.user_id", 'left')
->where("chats.id", $chat_id)
->where("chat_messages.id", $chat_message_id)
->where("chat_notifications.deleted_at", null)
->where('chat_notifications.viewed',true)
->where('chat_notifications.viewed', true)
->get()->getResultArray();
return $q;
}
@ -772,78 +742,93 @@ class ChatModel extends Model
$query = $this->builder()
->select([
"chats.id",
"cm.id as chatMessageId",
"u.id as userId",
"cm.message",
"chats.created_at",
"cm.updated_at",
"chats.title",
"
(
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
) as updated_at",
"CONCAT(u.first_name,' ',u.last_name) as creator",
"CONCAT('[',chats.title,']',' ',presupuestos.titulo) as title",
])
->join("chat_users", "chats.id = chat_users.chat_id", "left")
->join("presupuestos", "presupuestos.id = chats.presupuesto_id", 'left')
->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
)");
->join("users u", "u.id = cm.sender_id", 'left')
->where("chats.presupuesto_id is NOT NULL", NULL, FALSE);
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
->where("chats.chat_department_id is NOT NULL", NULL, FALSE);
}
return $query;
return $query->groupBy('chatMessageId');
}
public function getQueryDatatableMessagePedido(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
"chats.id",
"cm.id as chatMessageId",
"u.id as userId",
"cm.message",
"chats.created_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)
->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
)");
) as updated_at",
"CONCAT(u.first_name,' ',u.last_name) as creator",
"CONCAT('[',chats.title,']',' ',presupuestos.titulo) as title",
])
->join("pedidos_linea", "pedidos_linea.pedido_id = chats.pedido_id", 'left')
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->join("users u", "u.id = cm.sender_id", 'left')
->join("presupuestos", "presupuestos.id = pedidos_linea.presupuesto_id", 'left')
->where("chats.pedido_id is NOT NULL", NULL, FALSE);
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
->where("chats.chat_department_id is NOT NULL", NULL, FALSE);
}
return $query;
return $query->groupBy('chatMessageId');
}
public function getQueryDatatableMessageFactura(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
"chats.id",
"cm.id as chatMessageId",
"u.id as userId",
"cm.message",
"chats.created_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)
->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
)");
) as updated_at",
"CONCAT(u.first_name,' ',u.last_name) as creator",
"chats.title",
])
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->join("users u", "u.id = cm.sender_id", 'left')
->join("facturas", "facturas.id = chats.factura_id", "left")
->where("chats.factura_id is NOT NULL", NULL, FALSE);
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);
->where("chats.chat_department_id is NOT NULL", NULL, FALSE);
}
}
return $query;
return $query->groupBy('chatMessageId');
}
public function createNewDirectChat(string $title, string $message, array $users)
{
@ -923,12 +908,12 @@ class ChatModel extends Model
->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_notifications.user_id', $user_id)
->where('chat_messages.chat_id', $chat_id)
->get()->getResultObject();
$chat_messages_ids = array_map(fn($q) => $q->notificationId, $query);
$chatNotificationModel = model(ChatNotification::class);
if($chat_messages_ids){
if ($chat_messages_ids) {
$chatNotificationModel->setNotificationsAsViewed($chat_messages_ids);
}
}
@ -945,12 +930,12 @@ class ChatModel extends Model
->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_notifications.user_id', $user_id)
->where('chat_messages.chat_id', $chat_id)
->get()->getResultObject();
$chat_messages_ids = array_map(fn($q) => $q->notificationId, $query);
$chatNotificationModel = model(ChatNotification::class);
if($chat_messages_ids){
if ($chat_messages_ids) {
$chatNotificationModel->setNotificationsAsUnViewed($chat_messages_ids);
}
}