import type { ILivechatDepartment, AvailableAgentsAggregation } from '@rocket.chat/core-typings'; import { LivechatDepartment } from '@rocket.chat/models'; import type { Filter } from 'mongodb'; import { callbacks } from '../../../../server/lib/callbacks'; import { settings } from '../../../../server/settings'; export async function getChatLimitsQuery(departmentId?: string): Promise> { const limitFilter: Filter = []; if (departmentId) { const departmentLimit = ( await LivechatDepartment.findOneById>(departmentId, { projection: { maxNumberSimultaneousChat: 1 }, }) )?.maxNumberSimultaneousChat || 0; if (departmentLimit > 0) { limitFilter.push({ 'queueInfo.chatsForDepartment': { $gte: Number(departmentLimit) } }); } } limitFilter.push({ $and: [{ maxChatsForAgent: { $gt: 0 } }, { $expr: { $gte: ['$queueInfo.chats', '$maxChatsForAgent'] } }], }); const maxChatsPerSetting = settings.get('Livechat_maximum_chats_per_agent'); if (maxChatsPerSetting > 0) { limitFilter.push({ $and: [{ maxChatsForAgent: { $eq: 0 } }, { 'queueInfo.chats': { $gte: maxChatsPerSetting } }], }); } return { $match: { $or: limitFilter } }; } callbacks.add( 'livechat.applySimultaneousChatRestrictions', async (_: any, { departmentId }: { departmentId?: string } = {}) => { return getChatLimitsQuery(departmentId); }, callbacks.priority.HIGH, 'livechat-apply-simultaneous-restrictions', );