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

@ -108,7 +108,7 @@ class ChatDeparmentModel extends Model
public function getChatDeparmentUserQuery(int $chat_deparment_id)
{
$query = $this->builder()
$query = $this
->select(
[
"users.*"
@ -123,20 +123,21 @@ class ChatDeparmentModel extends Model
"users",
"chat_department_users.user_id = users.id",
'left'
)->where("chat_departments.id", $chat_deparment_id)
->where("chat_department_users.deleted_at", null)
->where("users.deleted_at", null);
)
->where("chat_department_users.chat_department_id", $chat_deparment_id)
->where("chat_department_users.deleted_at",null)
->where("users.deleted_at",null);
return $query;
}
public function getChatDepartmentUsers(int $chat_deparment_id)
{
$result = $this->getChatDeparmentUserQuery($chat_deparment_id)
->where('chat_department_users.presupuesto_id', null)
->where('chat_department_users.pedido_id', null)
->where('chat_department_users.factura_id', null)
->get()->getResultObject();
return $result;
->where('chat_department_users.presupuesto_id',null)
->where('chat_department_users.pedido_id',null)
->where('chat_department_users.factura_id',null)
->get();
return $result->getResultObject() ?: [];
}
public function getChatDeparmentPresupuestoUsers(int $chat_deparment_id, int $presupuesto_id)
{
@ -163,7 +164,7 @@ class ChatDeparmentModel extends Model
{
return $this->find($chat_deparment_id)->display;
}
public function getChatDepartmentSelect(string $query = null)
public function getChatDepartmentSelect(?string $query)
{
$q = $this->builder()->select([
"id",
@ -176,4 +177,8 @@ class ChatDeparmentModel extends Model
}
return $q;
}
public function datatableQuery()
{
return $this->select(['id','display','description'])->where('deleted_at',null);
}
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Chat;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Model;
class ChatDeparmentUserModel extends Model
@ -53,11 +53,55 @@ class ChatDeparmentUserModel extends Model
public function getChatDepartmentUser(int $user_id)
{
return $this->db->table($this->table." t1")
->select("chat_departments.*")
->join("chat_departments","t1.chat_department_id = chat_departments.id","left")
->where("t1.user_id",$user_id)
->where("t1.deleted_at",null)
->get()->getResultObject();
return $this->db->table($this->table . " t1")
->select("chat_departments.*")
->join("chat_departments", "t1.chat_department_id = chat_departments.id", "left")
->where("t1.user_id", $user_id)
->where("t1.deleted_at", null)
->get()->getResultObject();
}
public function datatableQuery(int $chat_department_id)
{
return $this->builder()->select(
[
'users.id as userId',
'CONCAT(users.first_name," ",users.last_name) as userFullName',
'users.username'
]
)
->join('users', 'users.id = chat_department_users.user_id', 'left')
->join('chat_departments', 'chat_departments.id = chat_department_users.chat_department_id', 'left')
->where('chat_departments.id', $chat_department_id)
->where('chat_department_users.pedido_id', null)
->where('chat_department_users.presupuesto_id', null)
->where('chat_department_users.factura_id', null)
->where('chat_department_users.deleted_at', null);
}
public function querySelectUsersNotInDepartment(int $chat_department_id, ?string $q): array
{
$query = $this->builder()->select([
'users.id as userId',
])
->join('users', 'users.id = chat_department_users.user_id', 'left')
->join('chat_departments', 'chat_departments.id = chat_department_users.chat_department_id', 'left')
->where('chat_departments.id', $chat_department_id)
->where('chat_department_users.deleted_at', null);
$usersInDepartment = array_map(fn($q) => $q->userId, $query->get()->getResultObject());
$userModel = model(UserModel::class);
$queryUser = $userModel->builder()->select([
'id',
'CONCAT(first_name," ",last_name) as name',
'username as description',
])
->where('cliente_id',null)
->where('deleted_at', null);
if ($usersInDepartment) {
$queryUser->whereNotIn("id", $usersInDepartment);
}
if ($q) {
$queryUser->like('CONCAT(first_name," ",last_name)', $q);
}
return $queryUser->get()->getResultArray();
}
}

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)