mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
fix chat department message total count
This commit is contained in:
@ -53,7 +53,7 @@ class ChatDeparmentModel extends Model
|
|||||||
public function getChatDepartments(string $type = "general"): array
|
public function getChatDepartments(string $type = "general"): array
|
||||||
{
|
{
|
||||||
$userModel = model(UserModel::class);
|
$userModel = model(UserModel::class);
|
||||||
$chatMessageModel = model(ChatMessageModel::class);
|
$chatModel = model(ChatModel::class);
|
||||||
|
|
||||||
$query = $this->builder()
|
$query = $this->builder()
|
||||||
->select(
|
->select(
|
||||||
@ -63,7 +63,6 @@ class ChatDeparmentModel extends Model
|
|||||||
'chat_departments.name',
|
'chat_departments.name',
|
||||||
'chat_departments.display',
|
'chat_departments.display',
|
||||||
'chat_department_users.user_id',
|
'chat_department_users.user_id',
|
||||||
'chats.id as chatId',
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
->join(
|
->join(
|
||||||
@ -71,18 +70,14 @@ class ChatDeparmentModel extends Model
|
|||||||
"chat_department_users.chat_department_id = chat_departments.id",
|
"chat_department_users.chat_department_id = chat_departments.id",
|
||||||
'left'
|
'left'
|
||||||
)
|
)
|
||||||
->join("chats", "chats.chat_department_id = chat_departments.id", "left")
|
|
||||||
->join(
|
->join(
|
||||||
"users",
|
"users",
|
||||||
"chat_department_users.user_id = users.id",
|
"chat_department_users.user_id = users.id",
|
||||||
'left'
|
'left'
|
||||||
)->join(
|
|
||||||
"chat_messages",
|
|
||||||
"chat_messages.chat_id = chats.id",
|
|
||||||
"left"
|
|
||||||
)
|
)
|
||||||
->where("chat_departments.type", $type);
|
->where("chat_departments.type", $type);
|
||||||
|
|
||||||
|
|
||||||
// if (auth()->user()->cliente_id == null) {
|
// if (auth()->user()->cliente_id == null) {
|
||||||
// $query->where("chat_department_users.user_id", auth()->user()->id);
|
// $query->where("chat_department_users.user_id", auth()->user()->id);
|
||||||
// }
|
// }
|
||||||
@ -92,20 +87,13 @@ class ChatDeparmentModel extends Model
|
|||||||
$departments = [];
|
$departments = [];
|
||||||
foreach ($results as $row) {
|
foreach ($results as $row) {
|
||||||
$departmentName = $row['name'];
|
$departmentName = $row['name'];
|
||||||
$totalMessages = 0;
|
|
||||||
|
|
||||||
// If the department is not yet added to the array, initialize it
|
// If the department is not yet added to the array, initialize it
|
||||||
if (!isset($departments[$departmentName])) {
|
if (!isset($departments[$departmentName])) {
|
||||||
if($row['chatId']){
|
|
||||||
$data["messages"] = $chatMessageModel->get_chat_messages($row['chatId']);
|
|
||||||
$totalMessages = count($data["messages"]);
|
|
||||||
|
|
||||||
}
|
|
||||||
$departments[$departmentName] = [
|
$departments[$departmentName] = [
|
||||||
'id' => $row['id'],
|
'id' => $row['id'],
|
||||||
'name' => $row['name'],
|
'name' => $row['name'],
|
||||||
'display' => $row['display'],
|
'display' => $row['display'],
|
||||||
'totalMessages' => $totalMessages,
|
|
||||||
'users' => [] // Initialize users as an empty array
|
'users' => [] // Initialize users as an empty array
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -611,5 +611,17 @@ class ChatModel extends Model
|
|||||||
return $q->get()->getResultObject();
|
return $q->get()->getResultObject();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
|
return $q;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -142,7 +142,8 @@ class Chat {
|
|||||||
_handleGetChatListSuccess(data) {
|
_handleGetChatListSuccess(data) {
|
||||||
Object.values(data).map(row => {
|
Object.values(data).map(row => {
|
||||||
this.chatList.append(this._getContact(row))
|
this.chatList.append(this._getContact(row))
|
||||||
|
this.chatDeparmentId = row.id
|
||||||
|
this._getChatTotalMessages(row.name)
|
||||||
this.chatList.find(`#chat_${row.name}`).on("click", (event) => {
|
this.chatList.find(`#chat_${row.name}`).on("click", (event) => {
|
||||||
$(".chat-contact-list-item").removeClass("active")
|
$(".chat-contact-list-item").removeClass("active")
|
||||||
$(event.currentTarget).parent().addClass("active")
|
$(event.currentTarget).parent().addClass("active")
|
||||||
@ -215,7 +216,7 @@ class Chat {
|
|||||||
${row.display}
|
${row.display}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<span class="badge badge-center rounded-pill bg-danger messages-unread-contact">${row.totalMessages}</span>
|
<span class="badge badge-center rounded-pill bg-danger messages-unread-contact">${row.totalMessages ?? 0}</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
`
|
`
|
||||||
@ -235,6 +236,24 @@ class Chat {
|
|||||||
);
|
);
|
||||||
ajax.get();
|
ajax.get();
|
||||||
}
|
}
|
||||||
|
_getChatDepartmentMessageCount()
|
||||||
|
{
|
||||||
|
let ajax = new Ajax(
|
||||||
|
`/chat/department/count/${this.chatType}/${this.chatDeparmentId}/${this.modelId}`,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
this._getChatDepartmentMessageCountSuccess.bind(this),
|
||||||
|
this._getChatDepartmentMessageCountError.bind(this),
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
|
ajax.get();
|
||||||
|
}
|
||||||
|
_getChatDepartmentMessageCountSuccess(data){
|
||||||
|
this.domItem.find(`chat_${data.name}`).find(".messages-unread-contact").text(data.count)
|
||||||
|
}
|
||||||
|
_getChatDepartmentMessageCountError(){}
|
||||||
|
|
||||||
_getChatMessage() {
|
_getChatMessage() {
|
||||||
let ajax = new Ajax(
|
let ajax = new Ajax(
|
||||||
`/chat/department/${this.chatType}/${this.chatDeparmentId}/${this.modelId}`,
|
`/chat/department/${this.chatType}/${this.chatDeparmentId}/${this.modelId}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user