The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/models/server/raw/LivechatRooms.js

106 lines
2.4 KiB

import { BaseRaw } from './BaseRaw';
export class LivechatRoomsRaw extends BaseRaw {
getQueueMetrics({ departmentId, agentId, includeOfflineAgents, options = {} }) {
const match = { $match: { t: 'l', open: true, servedBy: { $exists: true } } };
const matchUsers = { $match: {} };
if (departmentId) {
match.$match.departmentId = departmentId;
}
if (agentId) {
matchUsers.$match['user._id'] = agentId;
}
if (!includeOfflineAgents) {
matchUsers.$match['user.status'] = { $ne: 'offline' };
matchUsers.$match['user.statusLivechat'] = { $eq: 'available' };
}
const departmentsLookup = {
$lookup: {
from: 'rocketchat_livechat_department',
localField: 'departmentId',
foreignField: '_id',
as: 'departments',
},
};
const departmentsUnwind = {
$unwind: {
path: '$departments',
preserveNullAndEmptyArrays: true,
},
};
const departmentsGroup = {
$group: {
_id: {
departmentId: '$departmentId',
name: '$departments.name',
room: '$$ROOT',
},
},
};
const usersLookup = {
$lookup: {
from: 'users',
localField: '_id.room.servedBy._id',
foreignField: '_id',
as: 'user',
},
};
const usersUnwind = {
$unwind: {
path: '$user',
preserveNullAndEmptyArrays: true,
},
};
const usersGroup = {
$group: {
_id: {
userId: '$user._id',
username: '$user.username',
status: '$user.status',
departmentId: '$_id.departmentId',
departmentName: '$_id.name',
},
chats: { $sum: 1 },
},
};
const project = {
$project: {
_id: 0,
user: {
_id: '$_id.userId',
username: '$_id.username',
status: '$_id.status',
},
department: {
_id: { $ifNull: ['$_id.departmentId', null] },
name: { $ifNull: ['$_id.departmentName', null] },
},
chats: 1,
},
};
const firstParams = [match, departmentsLookup, departmentsUnwind, departmentsGroup, usersLookup, usersUnwind];
if (Object.keys(matchUsers.$match)) {
firstParams.push(matchUsers);
}
const params = [...firstParams, usersGroup, project];
if (options.offset) {
params.push({ $skip: options.offset });
}
if (options.count) {
params.push({ $limit: options.count });
}
if (options.sort) {
params.push({ $sort: { name: 1 } });
}
return this.col.aggregate(params).toArray();
}
findByVisitorId(visitorId, options) {
const query = {
t: 'l',
'v._id': visitorId,
};
return this.find(query, options);
}
}