redirect notification message to model edit form

This commit is contained in:
amazuecos
2025-02-10 09:32:48 +01:00
parent 41783f5b25
commit 071d386d36

View File

@ -92,7 +92,7 @@ class ChatModel extends Model
$chatDeparmentModel = model(ChatDeparmentModel::class);
$presupuesto = $model->find($presupuesto_id);
return $this->insert([
"title" => $presupuesto->titulo."[".$chatDeparmentModel->getDisplay($chat_department_id)."]",
"title" => $presupuesto->titulo . "[" . $chatDeparmentModel->getDisplay($chat_department_id) . "]",
"presupuesto_id" => $presupuesto_id,
"chat_department_id" => $chat_department_id
]);
@ -104,7 +104,7 @@ class ChatModel extends Model
$chatDeparmentModel = model(ChatDeparmentModel::class);
$pedido = $model->getPedidoClientePresupuesto($pedido_id);
return $this->insert([
"title" => "Pedido ".$pedido->titulo."[".$chatDeparmentModel->getDisplay($chat_department_id)."]",
"title" => "Pedido " . $pedido->titulo . "[" . $chatDeparmentModel->getDisplay($chat_department_id) . "]",
"pedido_id" => $pedido_id,
"chat_department_id" => $chat_department_id
]);
@ -117,7 +117,7 @@ class ChatModel extends Model
$chatDeparmentModel = model(ChatDeparmentModel::class);
$factura = $model->find($factura_id);
return $this->insert([
"title" => "Factura ".$factura->numero."[".$chatDeparmentModel->getDisplay($chat_department_id)."]",
"title" => "Factura " . $factura->numero . "[" . $chatDeparmentModel->getDisplay($chat_department_id) . "]",
"factura_id" => $factura_id,
"chat_department_id" => $chat_department_id
]);
@ -239,7 +239,7 @@ class ChatModel extends Model
$count++;
}
}
$row->uri = "/pedidos/edit/" . $row->pedidoId;
$row->uri = "/pedidos/edit/" . $row->pedidoId . "#accordionChatPedido";
$row->unreadMessages = $count;
}
return $results;
@ -273,7 +273,7 @@ class ChatModel extends Model
$count++;
}
}
$row->uri = "/chat/factura/" . $row->chatId;
$row->uri = "/factura/edit/" . $row->facturaId . "#accordionChatFactura";
$row->unreadMessages = $count;
}
return $results;
@ -307,7 +307,7 @@ class ChatModel extends Model
$count++;
}
}
$row->uri = "/chat/presupuesto/" . $row->chatId;
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
$row->unreadMessages = $count;
}
@ -334,7 +334,7 @@ class ChatModel extends Model
->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);
$rows = $q->get()->getResultObject();
$auth_user = auth()->user();
@ -343,25 +343,25 @@ class ChatModel extends Model
$row->unreadMessages = 0;
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if($auth_user->cliente_id){
$row->uri = "/chat/presupuesto/" . $row->chatId;
}else{
$row->uri = "/chat/presupuesto/" . $row->chatId;
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){
if ($row->chatDepartmentId) {
$row->chatDisplay = $row->model->titulo;
}else{
$row->chatDisplay .="[INTERNAL]";
} else {
$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->chatId . "#accordionChatPedido";
$row->uri = "/pedidos/edit/" . $row->pedidoId . "#accordionChatPedido";
$row->title = $row->pedidoId;
if($row->chatDepartmentId){
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "P";
@ -369,9 +369,9 @@ class ChatModel extends Model
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/factura/" . $row->chatId;
$row->uri = "/chat/factura/" . $row->facturaId."#accordionChatFactura";
$row->avatar = "F";
if($row->chatDepartmentId){
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->title = $row->facturaId;
@ -400,7 +400,7 @@ class ChatModel extends Model
->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);
$auth_user = auth()->user();
$rows = $q->get()->getResultObject();
$rows_new = [];
@ -408,25 +408,25 @@ class ChatModel extends Model
$row->unreadMessages = 0;
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if($auth_user->cliente_id){
$row->uri = "/chat/presupuesto/" . $row->chatId;
}else{
$row->uri = "/chat/presupuesto/" . $row->chatId;
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){
if ($row->chatDepartmentId) {
$row->chatDisplay = $row->model->titulo;
}else{
$row->chatDisplay .="[INTERNAL]";
} else {
$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 = "/chat/pedido/" . $row->chatId;
$row->uri = "/pedidos/edit/" . $row->pedidoId . "#accordionChatFactura";
$row->title = $row->pedidoId;
if($row->chatDepartmentId){
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->avatar = "P";
@ -434,9 +434,9 @@ class ChatModel extends Model
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/factura/" . $row->chatId;
$row->uri = "/factura/edit/" . $row->facturaId . "#accordionChatFactura";
$row->avatar = "F";
if($row->chatDepartmentId){
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
}
$row->title = $row->facturaId;
@ -470,7 +470,7 @@ class ChatModel extends Model
foreach ($rows as $row) {
if ($row->chatId) {
$row->model = [];
$row->uri = "/chat/direct/".$row->chatId;
$row->uri = "/chat/direct/" . $row->chatId;
$row->avatar = "MD";
$row->title = "MD";
// $row->chatDisplay = $this->getSenderIdFromChatMessage($row->chatId)?->username ?? "Unknown";
@ -496,8 +496,8 @@ class ChatModel extends Model
->where("chats.presupuesto_id", $presupuesto_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = "Presupuesto"."[".$presupuesto_id."] - ";
$data["chatTitle"].= $this->find($chat_id)->title;
$data["chatTitle"] = "Presupuesto" . "[" . $presupuesto_id . "] - ";
$data["chatTitle"] .= $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
@ -517,8 +517,8 @@ class ChatModel extends Model
->where("chats.pedido_id", $pedido_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = "Pedido"."[".$pedido_id."] - ";
$data["chatTitle"].= $this->find($chat_id)->title;
$data["chatTitle"] = "Pedido" . "[" . $pedido_id . "] - ";
$data["chatTitle"] .= $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
@ -538,8 +538,8 @@ class ChatModel extends Model
->where("chats.factura_id", $factura_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = "Factura"."[".$factura_id."] - ";
$data["chatTitle"].= $this->find($chat_id)->title;
$data["chatTitle"] = "Factura" . "[" . $factura_id . "] - ";
$data["chatTitle"] .= $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
@ -709,12 +709,12 @@ class ChatModel extends Model
"chats.updated_at",
"chats.title",
])
->join("chat_users","chat_users.chat_id = chats.id","left")
->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("chat_users.user_id",$user_id)
->where("chat_users.user_id", $user_id)
->orderBy("created_at", "DESC")
->groupBy("chats.id");
@ -729,15 +729,15 @@ class ChatModel extends Model
"chats.updated_at",
"chats.title",
])
->join("chat_users","chats.id = chat_users.chat_id","left")
->join("presupuestos","presupuestos.id = chats.presupuesto_id",'left')
->join("chat_users", "chats.id = chat_users.chat_id", "left")
->join("presupuestos", "presupuestos.id = chats.presupuesto_id", 'left')
->where("presupuesto_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
if(auth()->user()->inGroup("admin") == false){
$query->where('presupuestos.cliente_id',auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL",NULL,FALSE);
}
$query->orderBy("created_at", "DESC");
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function getQueryDatatableMessagePedido(int $user_id): BaseBuilder
@ -749,16 +749,16 @@ class ChatModel extends Model
"chats.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("presupuestos","presupuestos.id = pedidos_linea.presupuesto_id",'left')
->join("chat_users", "chat_users.chat_id = chats.id", "left")
->join("pedidos_linea", "pedidos_linea.pedido_id = chats.pedido_id", 'left')
->join("presupuestos", "presupuestos.id = pedidos_linea.presupuesto_id", 'left')
->where("chats.pedido_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
if(auth()->user()->inGroup("admin") == false){
$query->where('presupuestos.cliente_id',auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL",NULL,FALSE);
}
$query->orderBy("created_at", "DESC");
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function getQueryDatatableMessageFactura(int $user_id): BaseBuilder
@ -770,17 +770,17 @@ class ChatModel extends Model
"chats.updated_at",
"chats.title",
])
->join("chat_users","chat_users.chat_id = chats.id","left")
->join("facturas","facturas.id = chats.factura_id","left")
->join("chat_users", "chat_users.chat_id = chats.id", "left")
->join("facturas", "facturas.id = chats.factura_id", "left")
->where("chats.factura_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
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);
}
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);
}
$query->orderBy("created_at", "DESC");
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function createNewDirectChat(string $title, string $message, array $users)
@ -802,7 +802,7 @@ class ChatModel extends Model
$chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user_id]);
}
}
/**
* Obtain users and messages from `chat_id`
*
@ -868,7 +868,6 @@ class ChatModel extends Model
->where("user_id", $user_id)
->whereIn("chat_message_id", $chat_messages_ids)
->update(["viewed" => true]);
}
public function setAsViewedChatUserMessages(int $chat_id, int $user_id)
{
@ -876,7 +875,6 @@ class ChatModel extends Model
->where('chat_id', $chat_id)
->where('sender_id !=', $user_id)
->update(["viewed" => true]);
}
public function setAsUnviewedChatUserNotifications(int $chat_id, int $user_id)
{
@ -899,19 +897,16 @@ class ChatModel extends Model
->where('chat_id', $chat_id)
->where('sender_id !=', $user_id)
->update(["viewed" => false]);
}
public function createNotificationsToNewChatUser($chat_id,$user_id)
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);
$chat_notifications = array_map(fn($q) => ["chat_message_id" => $q->id, "user_id" => $user_id], $query);
$chatNotificationModel = model(ChatNotification::class);
$chatNotificationModel->insertBatch($chat_notifications);
}
}