fix datatable messages

This commit is contained in:
amazuecos
2025-02-26 08:11:12 +01:00
parent c5f1f0e55a
commit ae561d1e19
8 changed files with 184 additions and 43 deletions

View File

@ -11,7 +11,7 @@ class ChatDeparmentModel extends Model
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $useSoftDeletes = true;
protected $protectFields = true;
protected $allowedFields = [
"name",
@ -107,7 +107,7 @@ class ChatDeparmentModel extends Model
}
public function getChatDepartmentUsers(int $chat_deparment_id)
{
$result = $this->db->table('chat_departments')
$result = $this->builder()
->select(
[
"users.*"
@ -123,6 +123,8 @@ class ChatDeparmentModel extends Model
"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)
->get()->getResultObject();
return $result;
}

View File

@ -52,7 +52,9 @@ class ChatDeparmentUserModel extends Model
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)->get()->getResultObject();
->where("t1.user_id",$user_id)
->where("t1.deleted_at",null)
->get()->getResultObject();
}
}

View File

@ -344,9 +344,9 @@ class ChatModel extends Model
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
$row->title = $row->presupuestoId;
if ($row->chatDepartmentId) {
@ -369,7 +369,7 @@ class ChatModel extends Model
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/factura/" . $row->facturaId."#accordionChatFactura";
$row->uri = "/chat/factura/" . $row->facturaId . "#accordionChatFactura";
$row->avatar = "F";
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
@ -409,7 +409,7 @@ class ChatModel extends Model
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
@ -677,7 +677,7 @@ class ChatModel extends Model
return $q->get()->getFirstRow();
}
/**
* Check if all messages of a chat sended to an user have been viewed.
* Check if all messages of a chat sent to an user have been viewed.
*
* @param integer $chat_id
* @param integer $user_id
@ -689,7 +689,7 @@ 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')
->where("chats.id", $chat_id)
->where("chat_notifications.user_id", $user_id)
// ->where("chat_notifications.user_id", $user_id)
->where("chat_notifications.viewed", false);
$unread_messages_count = $q->countAllResults();
if ($unread_messages_count > 0) {
@ -699,24 +699,54 @@ class ChatModel extends Model
}
return $result;
}
/**
* Return users who did not read the message.
*
* @param integer $chat_id
* @return array True : All messages readed
*/
public function getUsersNotificationNotViewedFromChat(int $chat_id): array
{
$q = $this->builder()->distinct()->select(
[
"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"
]
)
->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)
->get()->getResultArray();
return $q;
}
public function getQueryDatatable(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
"chats.id",
"chats.created_at",
"chats.updated_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")
->where("chat_department_id", null)
->where("pedido_id", null)
->where("presupuesto_id", null)
->where("factura_id", null)
->where("chat_users.user_id", $user_id)
->orderBy("created_at", "DESC")
->groupBy("chats.id");
->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
)");
return $query;
}
@ -726,18 +756,23 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_at",
"cm.updated_at",
"chats.title",
])
->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");
->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
)");
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
@ -746,19 +781,24 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_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)
->groupBy("chats.id");
->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
)");
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
@ -767,20 +807,25 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_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)
->groupBy("chats.id");
->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
)");
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");
return $query;
}
public function createNewDirectChat(string $title, string $message, array $users)