mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
feat : mensajes internos module
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models\Chat;
|
||||
|
||||
use App\Models\ChatNotification;
|
||||
use App\Models\Facturas\FacturaModel;
|
||||
use App\Models\Pedidos\PedidoModel;
|
||||
use App\Models\Presupuestos\PresupuestoModel;
|
||||
@ -22,6 +23,7 @@ class ChatModel extends Model
|
||||
"chat_department_id",
|
||||
"presupuesto_id",
|
||||
"factura_id",
|
||||
"title"
|
||||
];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
@ -279,6 +281,7 @@ class ChatModel extends Model
|
||||
}
|
||||
public function getChatDepartmentNotifications()
|
||||
{
|
||||
$chatDeparmentModel = model(ChatDeparmentModel::class);
|
||||
$chatMessageModel = model(ChatMessageModel::class);
|
||||
$presupuestoModel = model(PresupuestoModel::class);
|
||||
$facturaModel = model(FacturaModel::class);
|
||||
@ -299,11 +302,14 @@ class ChatModel extends Model
|
||||
->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;
|
||||
$chatDeparmentUsers = $chatDeparmentModel->getChatDepartmentUsers($row->chatDepartmentId);
|
||||
$chatDeparmentUsersId = array_map(fn($x) => $x->id,$chatDeparmentUsers);
|
||||
foreach ($messages as $m) {
|
||||
if($m->viewed == false && $m->sender_id != auth()->user()->id)
|
||||
if($m->viewed == false && $m->sender_id != auth()->user()->id && in_array($m->sender_id,$chatDeparmentUsersId) == false)
|
||||
$count++;
|
||||
}
|
||||
if($row->presupuestoId){
|
||||
@ -335,25 +341,184 @@ class ChatModel extends Model
|
||||
public function getChatInternalNotifications()
|
||||
{
|
||||
$chatMessageModel = model(ChatMessageModel::class);
|
||||
$userModel = model(UserModel::class);
|
||||
$internalMessages = $chatMessageModel->builder()
|
||||
$presupuestoModel = model(PresupuestoModel::class);
|
||||
$facturaModel = model(FacturaModel::class);
|
||||
$pedidoModel = model(PedidoModel::class);
|
||||
$chatNotificationModel = model(ChatNotification::class);
|
||||
|
||||
$q = $this->db->table("chats")
|
||||
->select([
|
||||
"chat_id as chatId",
|
||||
"sender_id",
|
||||
"COUNT(viewed) as unreadMessages",
|
||||
"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.title as chatDisplay"
|
||||
])
|
||||
->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 = "#";
|
||||
->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);
|
||||
$rows = $q->get()->getResultObject();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
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 = $this->countUnreadMessagePresupuesto($row->presupuestoId);
|
||||
}
|
||||
elseif($row->pedidoId){
|
||||
$row->model = $pedidoModel->find($row->pedidoId);
|
||||
$row->uri = "/pedidos/edit/".$row->pedidoId;
|
||||
$row->title = $row->pedidoId;
|
||||
$row->avatar = "P";
|
||||
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
|
||||
|
||||
|
||||
}
|
||||
elseif($row->facturaId){
|
||||
$row->model = $facturaModel->find($row->facturaId);
|
||||
$row->uri = "/facturas/edit/".$row->facturaId;
|
||||
$row->avatar = "F";
|
||||
$row->title = $row->facturaId;
|
||||
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
|
||||
|
||||
}
|
||||
}
|
||||
return $internalMessages;
|
||||
return $rows;
|
||||
}
|
||||
public function getChatInternalHebraPresupuesto(int $chat_id,int $presupuesto_id) : array
|
||||
{
|
||||
|
||||
$data = [];
|
||||
$query = $this->builder()->select([
|
||||
"chats.id as chatId",
|
||||
"chat_messages.message",
|
||||
"users.username as senderUserName",
|
||||
"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);
|
||||
$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){
|
||||
$data = [];
|
||||
$query = $this->builder()->select([
|
||||
"chats.id as chatId",
|
||||
"chat_messages.message",
|
||||
"users.username as senderUserName",
|
||||
"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);
|
||||
$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){
|
||||
$data = [];
|
||||
$query = $this->builder()->select([
|
||||
"chats.id as chatId",
|
||||
"chat_messages.message",
|
||||
"users.username as senderUserName",
|
||||
"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);
|
||||
$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 getChatUsers(int $chat_id)
|
||||
{
|
||||
$query = $this->builder()->select([
|
||||
"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);
|
||||
return $query->get()->getResultObject();
|
||||
}
|
||||
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();
|
||||
foreach ($chats as $chat) {
|
||||
$data[$chat->chatId] = $this->getChatInternalHebraPresupuesto($chat->chatId,$presupuesto_id);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
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();
|
||||
foreach ($chats as $chat) {
|
||||
$data[$chat->chatId] = $this->getChatInternalHebraPedido($chat->chatId,$pedido_id);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
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();
|
||||
foreach ($chats as $chat) {
|
||||
$data[$chat->chatId] = $this->getChatInternalHebraFactura($chat->chatId,$factura_id);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user