messages chat view

This commit is contained in:
amazuecos
2024-11-28 16:07:11 +01:00
parent 093aa42e67
commit c1aecb43f2
16 changed files with 661 additions and 396 deletions

View File

@ -81,7 +81,7 @@ class ChatModel extends Model
{
return $this->builder()->where("pedido_id", $pedido_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
}
public function getChatFactura(int $chat_department_id, int $factura_id)
public function getChatFactura(int $chat_department_id, int $factura_id)
{
return $this->builder()->where("factura_id", $factura_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
}
@ -93,15 +93,15 @@ class ChatModel extends Model
"chat_department_id" => $chat_department_id
]);
}
public function createChatPedido(int $chat_department_id, int $pedido_id) : int
public function createChatPedido(int $chat_department_id, int $pedido_id): int
{
return $this->insert([
return $this->insert([
"pedido_id" => $pedido_id,
"chat_department_id" => $chat_department_id
]);
}
public function createChatFactura(int $chat_department_id, int $factura_id) : int
public function createChatFactura(int $chat_department_id, int $factura_id): int
{
return $this->insert([
@ -109,7 +109,7 @@ class ChatModel extends Model
"chat_department_id" => $chat_department_id
]);
}
public function createChatSingle() : int
public function createChatSingle(): int
{
return $this->insert(["chat_department_id" => null]);
}
@ -138,82 +138,82 @@ class ChatModel extends Model
return $countChatPresupuesto > 0;
}
public function getChatPedidosChat() : array
public function getChatPedidosChat(): array
{
$query = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"pedidos.id as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("pedidos","pedidos.id = chats.pedido_id","left")
->get()->getResultObject();
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"pedidos.id as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("pedidos", "pedidos.id = chats.pedido_id", "left")
->get()->getResultObject();
return $query;
}
public function getChatPresupuestosChat() : array
public function getChatPresupuestosChat(): array
{
$query = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"presupuestos.titulo as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("presupuestos","presupuestos.id = chats.pedido_id","left")
->get()->getResultObject();
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"presupuestos.titulo as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("presupuestos", "presupuestos.id = chats.pedido_id", "left")
->get()->getResultObject();
return $query;
}
public function getChatFacturasChat() : array
public function getChatFacturasChat(): array
{
$query = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("facturas","facturas.id = chats.pedido_id","left")
->get()->getResultObject();
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("facturas", "facturas.id = chats.pedido_id", "left")
->get()->getResultObject();
return $query;
}
public function getChatSingleChat() : array
public function getChatSingleChat(): array
{
$query = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("facturas","facturas.id = chats.pedido_id","left")
->get()->getResultObject();
->select([
"chats.id as chatId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("facturas", "facturas.id = chats.pedido_id", "left")
->get()->getResultObject();
return $query;
}
public function getClienteChatPedidos(array $pedidos) : array
public function getClienteChatPedidos(array $pedidos): array
{
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"pedidos.id as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("pedidos","pedidos.id = chats.pedido_id","left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
if(count($pedidos)>0){
$q->whereIn("pedidos.id",$pedidos);
}else{
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"pedidos.id as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("pedidos", "pedidos.id = chats.pedido_id", "left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
if (count($pedidos) > 0) {
$q->whereIn("pedidos.id", $pedidos);
} else {
return [];
}
$results = $q->get()->getResultObject();
@ -222,32 +222,32 @@ class ChatModel extends Model
foreach ($results as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
if ($message->sender_id != auth()->user()->id && $message->viewed == false) {
$count++;
}
}
$row->uri = "/pedidos/edit/".$row->pedidoId;
$row->unreadMessages=$count;
$row->uri = "/pedidos/edit/" . $row->pedidoId;
$row->unreadMessages = $count;
}
return $results;
}
public function getClienteChatFacturas(array $facturas) : array
public function getClienteChatFacturas(array $facturas): array
{
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.factura_id as facturaId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("facturas","facturas.id = chats.factura_id","left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
->select([
"chats.id as chatId",
"chats.factura_id as facturaId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"facturas.numero as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("facturas", "facturas.id = chats.factura_id", "left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
if(count($facturas)>0){
$q->whereIn("facturas.id",$facturas);
}else{
if (count($facturas) > 0) {
$q->whereIn("facturas.id", $facturas);
} else {
return [];
}
$results = $q->get()->getResultObject();
@ -256,32 +256,32 @@ class ChatModel extends Model
foreach ($results as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
if ($message->sender_id != auth()->user()->id && $message->viewed == false) {
$count++;
}
}
$row->uri = "/facturas/edit/".$row->facturaId;
$row->unreadMessages=$count;
$row->uri = "/facturas/edit/" . $row->facturaId;
$row->unreadMessages = $count;
}
return $results;
}
public function getClienteChatPresupuestos(array $presupuestos) : array
public function getClienteChatPresupuestos(array $presupuestos): array
{
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.presupuesto_id as presupuestoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"presupuestos.titulo as title"
])
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
->join("presupuestos","presupuestos.id = chats.presupuesto_id","left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
->select([
"chats.id as chatId",
"chats.presupuesto_id as presupuestoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
"presupuestos.titulo as title"
])
->join("chat_departments", "chat_departments.id = chats.chat_department_id", "left")
->join("presupuestos", "presupuestos.id = chats.presupuesto_id", "left")
->where('chats.chat_department_id is NOT NULL', NULL, FALSE);
if(count($presupuestos)>0){
$q->whereIn("presupuestos.id",$presupuestos);
}else{
if (count($presupuestos) > 0) {
$q->whereIn("presupuestos.id", $presupuestos);
} else {
return [];
}
$results = $q->get()->getResultObject();
@ -289,15 +289,15 @@ class ChatModel extends Model
$count = 0;
foreach ($results as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
foreach ($messages as $key => $message ) {
if($message->sender_id != auth()->user()->id && $message->viewed == false){
foreach ($messages as $key => $message) {
if ($message->sender_id != auth()->user()->id && $message->viewed == false) {
$count++;
}
}
$row->uri = "/presupuestos/presupuestocliente/edit/".$row->presupuestoId;
$row->unreadMessages=$count;
$row->uri = "/presupuestos/presupuestocliente/edit/" . $row->presupuestoId;
$row->unreadMessages = $count;
}
return $results;
}
public function getChatDepartmentNotifications()
@ -309,54 +309,55 @@ class ChatModel extends Model
$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.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);
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"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();
$auth_user = auth()->user();
foreach ($rows as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
$count = 0;
$chatDeparmentUsers = $chatDeparmentModel->getChatDepartmentUsers($row->chatDepartmentId);
$chatDeparmentUsersId = array_map(fn($x) => $x->id,$chatDeparmentUsers);
$chatDeparmentUsersId = array_map(fn($x) => $x->id, $chatDeparmentUsers);
foreach ($messages as $m) {
if($m->viewed == false && $m->sender_id != auth()->user()->id && in_array($m->sender_id,$chatDeparmentUsersId) == false)
if ($m->viewed == false && $m->sender_id != auth()->user()->id && in_array($m->sender_id, $chatDeparmentUsersId) == false)
$count++;
}
if($row->presupuestoId){
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
$row->uri = "/presupuestoadmin/edit/".$row->presupuestoId;
if($auth_user->cliente_id){
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId;
}else{
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId;
}
$row->title = $row->presupuestoId;
$row->avatar = "PRE";
$row->unreadMessages = $count;
}
elseif($row->pedidoId){
} elseif ($row->pedidoId) {
$row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/".$row->pedidoId;
$row->uri = "/pedidos/edit/" . $row->pedidoId;
$row->title = $row->pedidoId;
$row->avatar = "P";
$row->unreadMessages = $count;
}
elseif($row->facturaId){
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/facturas/edit/".$row->facturaId;
$row->uri = "/facturas/edit/" . $row->facturaId;
$row->avatar = "F";
$row->title = $row->facturaId;
$row->unreadMessages = $count;
}
}
return $rows;
}
public function getChatInternalNotifications()
{
@ -367,42 +368,45 @@ class ChatModel extends Model
$chatNotificationModel = model(ChatNotification::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.title as chatDisplay",
])
->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);
->select([
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.title as chatDisplay",
])
->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);
$auth_user = auth()->user();
$rows = $q->get()->getResultObject();
$rows_new = [];
foreach ($rows as $row) {
$row->unreadMessages = 0;
if($row->presupuestoId){
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
$row->uri = "/presupuestoadmin/edit/".$row->presupuestoId;
if($auth_user->cliente_id){
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId;
}else{
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId;
}
$row->title = $row->presupuestoId;
$row->chatDisplay = $row->model->titulo;
$row->avatar = "PRE";
$row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId);
$rows_new[] = $row;
}
elseif($row->pedidoId){
} elseif ($row->pedidoId) {
$row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/".$row->pedidoId;
$row->uri = "/pedidos/edit/" . $row->pedidoId;
$row->title = $row->pedidoId;
$row->avatar = "P";
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
$rows_new[] = $row;
}
elseif($row->facturaId){
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/facturas/edit/".$row->facturaId;
$row->uri = "/facturas/edit/" . $row->facturaId;
$row->avatar = "F";
$row->title = $row->facturaId;
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
@ -411,39 +415,40 @@ class ChatModel extends Model
}
return $rows_new;
}
public function getChatDirectMessageNotifications(){
public function getChatDirectMessageNotifications()
{
$chatMessageModel = model(ChatMessageModel::class);
$chatNotificationModel = model(ChatNotification::class);
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.user_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("chats.presupuesto_id",null)
->where("chats.chat_department_id",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id);
->select([
"chats.id as chatId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.user_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("chats.presupuesto_id", null)
->where("chats.chat_department_id", null)
->where("chats.pedido_id", null)
->where("chats.factura_id", null)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id);
$rows = $q->get()->getResultObject();
$rows_new = [];
foreach ($rows as $row) {
if($row->chatId){
if ($row->chatId) {
$row->model = [];
$row->uri = "/mensajes/internos";
$row->uri = "/chat/direct/".$row->chatId;
$row->avatar = "MD";
$row->title = "MD";
$row->chatDisplay = $this->getSenderIdFromChatMessage($row->chatId)?->username ?? "Unknown";
// $row->chatDisplay = $this->getSenderIdFromChatMessage($row->chatId)?->username ?? "Unknown";
$rows_new[] = $row;
}
}
return $rows_new;
}
public function getChatInternalHebraPresupuesto(int $chat_id,int $presupuesto_id) : array
public function getChatInternalHebraPresupuesto(int $chat_id, int $presupuesto_id): array
{
$data = [];
@ -454,17 +459,18 @@ class ChatModel extends Model
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.presupuesto_id",$presupuesto_id);
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("users", "users.id = chat_messages.sender_id", "left")
->where("chats.id", $chat_id)
->where("chats.presupuesto_id", $presupuesto_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatInternalHebraPedido($chat_id,$pedido_id){
public function getChatInternalHebraPedido($chat_id, $pedido_id)
{
$data = [];
$query = $this->builder()->select([
"chats.id as chatId",
@ -473,17 +479,18 @@ class ChatModel extends Model
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.pedido_id",$pedido_id);
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("users", "users.id = chat_messages.sender_id", "left")
->where("chats.id", $chat_id)
->where("chats.pedido_id", $pedido_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatInternalHebraFactura($chat_id,$factura_id){
public function getChatInternalHebraFactura($chat_id, $factura_id)
{
$data = [];
$query = $this->builder()->select([
"chats.id as chatId",
@ -492,10 +499,10 @@ class ChatModel extends Model
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.factura_id",$factura_id);
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("users", "users.id = chat_messages.sender_id", "left")
->where("chats.id", $chat_id)
->where("chats.factura_id", $factura_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
@ -508,132 +515,131 @@ class ChatModel extends Model
"users.username",
"CONCAT(users.first_name,' ',users.last_name) as userFullName",
])
->join("chat_users","chat_users.chat_id = chats.id")
->join("users","users.id = chat_users.user_id","left")
->where("chats.id",$chat_id);
->join("chat_users", "chat_users.chat_id = chats.id")
->join("users", "users.id = chat_users.user_id", "left")
->where("chats.id", $chat_id);
return $query->get()->getResultObject();
}
public function getPresupuestoHebras($presupuesto_id) : array
public function getPresupuestoHebras($presupuesto_id): array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.presupuesto_id",$presupuesto_id)->get()->getResultObject();
->where("chats.chat_department_id", null)
->where("chats.presupuesto_id", $presupuesto_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraPresupuesto($chat->chatId,$presupuesto_id);
}
$data[$chat->chatId] = $this->getChatInternalHebraPresupuesto($chat->chatId, $presupuesto_id);
}
return $data;
}
public function getPedidoHebras($pedido_id) : array
public function getPedidoHebras($pedido_id): array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.pedido_id",$pedido_id)->get()->getResultObject();
->where("chats.chat_department_id", null)
->where("chats.pedido_id", $pedido_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraPedido($chat->chatId,$pedido_id);
}
$data[$chat->chatId] = $this->getChatInternalHebraPedido($chat->chatId, $pedido_id);
}
return $data;
}
public function getFacturaHebras($factura_id) : array
public function getFacturaHebras($factura_id): array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.factura_id",$factura_id)->get()->getResultObject();
->where("chats.chat_department_id", null)
->where("chats.factura_id", $factura_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraFactura($chat->chatId,$factura_id);
}
$data[$chat->chatId] = $this->getChatInternalHebraFactura($chat->chatId, $factura_id);
}
return $data;
}
public function countUnreadMessagePresupuesto($presupuesto_id) : int|string
public function countUnreadMessagePresupuesto($presupuesto_id): int|string
{
return $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.presupuesto_id",$presupuesto_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)
->countAllResults();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.presupuesto_id", $presupuesto_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)
->countAllResults();
}
public function countUnreadMessagePedido($pedido_id) : int|string
public function countUnreadMessagePedido($pedido_id): int|string
{
return $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.pedido_id",$pedido_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)
->countAllResults();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.pedido_id", $pedido_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)
->countAllResults();
}
public function countUnreadMessageFactura($factura_id) : int|string
public function countUnreadMessageFactura($factura_id): int|string
{
return $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.factura_id",$factura_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)->countAllResults();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.factura_id", $factura_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)->countAllResults();
}
public function countUnreadMessageDirectos(int $chat_id) : int|string
public function countUnreadMessageDirectos(int $chat_id): int|string
{
return $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.presupuesto_id",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chats.chat_department_id",null)
->where("chat_messages.chat_id",$chat_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)->countAllResults();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.presupuesto_id", null)
->where("chats.pedido_id", null)
->where("chats.factura_id", null)
->where("chats.chat_department_id", null)
->where("chat_messages.chat_id", $chat_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)->countAllResults();
}
public function getSenderIdFromChatMessage(int $chat_id)
{
$first_message = $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->where("chats.presupuesto_id",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chats.id",$chat_id)
->where("chat_messages.receiver_id",auth()->user()->id)->get()->getFirstRow();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->where("chats.presupuesto_id", null)
->where("chats.pedido_id", null)
->where("chats.factura_id", null)
->where("chats.id", $chat_id)
->where("chat_messages.receiver_id", auth()->user()->id)->get()->getFirstRow();
$userModel = model(UserModel::class);
return $userModel->find($first_message->sender_id);
}
public function getOpenChatCliente(int $user_id) : array
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);
->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();
}
public function getChatDepartmentMessagesCount(int $chat_id,int $chat_department_id) : int
public function getChatDepartmentMessagesCount(int $chat_id, int $chat_department_id): int
{
$q = $this->builder()->select([
"chat_messages.id"
])
->join("chat_messages","chat_messages.chat_id = chats.id",'left')
->where("chats.chat_department_id",$chat_department_id)
->where("chats.id",$chat_id)
->countAllResults();
->join("chat_messages", "chat_messages.chat_id = chats.id", 'left')
->where("chats.chat_department_id", $chat_department_id)
->where("chats.id", $chat_id)
->countAllResults();
return $q;
}
public function getChatFirstUser(int $chat_id) : object
public function getChatFirstUser(int $chat_id): object
{
$q = $this->builder()->select(["users.id",
"CONCAT(users.first_name,' ',users.last_name) as userFullName",
$q = $this->builder()->select([
"users.id",
"CONCAT(users.first_name,' ',users.last_name) as userFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id",'left')
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chat_messages.deleted_at",null)
->orderBy("chat_messages.created_at",'ASC');
->join("chat_messages", "chat_messages.chat_id = chats.id", 'left')
->join("users", "users.id = chat_messages.sender_id", "left")
->where("chats.id", $chat_id)
->where("chat_messages.deleted_at", null)
->orderBy("chat_messages.created_at", 'ASC');
return $q->get()->getFirstRow();
}
/**
@ -643,24 +649,24 @@ 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_id, int $user_id): bool
{
$q = $this->builder()->select(["chat_notifications.id"])
->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.viewed",false);
->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.viewed", false);
$unread_messages_count = $q->countAllResults();
if($unread_messages_count > 0){
if ($unread_messages_count > 0) {
$result = false;
}else{
} else {
$result = true;
}
return $result;
}
public function getQueryDatatable(): BaseBuilder
public function getQueryDatatable(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
@ -669,15 +675,17 @@ class ChatModel extends Model
"chats.updated_at",
"chats.title",
])
->where("chat_department_id",null)
->where("pedido_id",null)
->where("presupuesto_id",null)
->where("factura_id",null)
->where("title is NOT NULL",NULL,FALSE)
->join("chat_users","chat_users.chat_id = chats.id","left")
->where("chat_department_id", null)
->where("pedido_id", null)
->where("presupuesto_id", null)
->where("factura_id", null)
->where("title is NOT NULL", NULL, FALSE)
->where("chat_users.user_id",$user_id)
->orderBy("created_at", "DESC");
return $query;
}
public function createNewDirectChat(string $title,string $message,array $users)
public function createNewDirectChat(string $title, string $message, array $users)
{
$chatMessageModel = model(ChatMessageModel::class);
$chatNotificationModel = model(ChatNotification::class);
@ -690,10 +698,10 @@ class ChatModel extends Model
"receiver_id" => null,
"message" => $message
]);
$chatUserModel->insert(["chat_id" => $chat_id,"user_id" => $auth_user_id]);
$chatUserModel->insert(["chat_id" => $chat_id, "user_id" => $auth_user_id]);
foreach ($users as $key => $user_id) {
$chatUserModel->insert(["chat_id" => $chat_id,"user_id" => $user_id]);
$chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id" => $user_id]);
$chatUserModel->insert(["chat_id" => $chat_id, "user_id" => $user_id]);
$chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user_id]);
}
}
/**
@ -707,16 +715,17 @@ class ChatModel extends Model
* "users" => array
* ]
*/
public function getChatDirect(int $chat_id) : array
public function getChatDirect(int $chat_id): array
{
$auth_user = auth()->user()->id;
$chat = $this->find($chat_id);
$chatNotificationModel = model(ChatNotification::class);
$query = $this->builder()->select([
"users.*",
])
->join("chat_users","chat_users.chat_id = chats.id")
->join("users","users.id = chat_users.user_id","left")
->where("chats.id",$chat_id);
->join("chat_users", "chat_users.chat_id = chats.id")
->join("users", "users.id = chat_users.user_id", "left")
->where("chats.id", $chat_id);
$users = $query->get()->getResultObject();
$query = $this->builder()->select([
"chat_messages.*",
@ -724,17 +733,18 @@ class ChatModel extends Model
"users.last_name as sender_last_name",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","chat_messages.sender_id = users.id","left")
->where("chats.id",$chat_id);
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("users", "chat_messages.sender_id = users.id", "left")
->where("chats.id", $chat_id);
$messages = $query->get()->getResultObject();
$validatedMessages = [];
foreach ($messages as $key => $message) {
if($auth_user == $message->sender_id){
$message->viewed = $chatNotificationModel->isChatMessageViewed($message->id);
if ($auth_user == $message->sender_id) {
$message->pos = 'right';
$validatedMessages[] = $message;
}else{
} else {
$message->pos = 'left';
$validatedMessages[] = $message;
}
@ -746,4 +756,63 @@ class ChatModel extends Model
];
return $data;
}
public function setAsViewedChatUserNotifications(int $chat_id, int $user_id)
{
$query = $this->builder()
->select("chat_messages.id")
->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left')
->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]);
}
public function setAsViewedChatUserMessages(int $chat_id, int $user_id)
{
$this->db->table("chat_messages")
->where('chat_id', $chat_id)
->where('sender_id !=', $user_id)
->update(["viewed" => true]);
}
public function setAsUnviewedChatUserNotifications(int $chat_id, int $user_id)
{
$query = $this->builder()
->select("chat_messages.id")
->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left')
->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]);
}
public function setAsUnviewedChatUserMessages(int $chat_id, int $user_id)
{
$this->db->table("chat_messages")
->where('chat_id', $chat_id)
->where('sender_id !=', $user_id)
->update(["viewed" => false]);
}
public function createNotificationsToNewChatUser($chat_id,$user_id)
{
$query = $this->builder()
->select("chat_messages.id")
->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left')
->where('chat_messages.chat_id', $chat_id)
->get()->getResultObject();
$chat_notifications = array_map(fn($q) => ["chat_message_id" => $q->id,"user_id" => $user_id], $query);
$chatNotificationModel = model(ChatNotification::class);
$chatNotificationModel->insertBatch($chat_notifications);
}
}