import { Users } from '@rocket.chat/models'; import { callbacks } from '../../../../server/lib/callbacks'; import { allowAgentSkipQueue } from '../../../../server/lib/omnichannel/Helper'; import { checkOnlineAgents } from '../../../../server/lib/omnichannel/service-status'; import { settings } from '../../../../server/settings'; import { isAgentWithinChatLimits } from '../../lib/omnichannel/Helper'; import { cbLogger } from '../../lib/omnichannel/logger'; const validateMaxChats = async ({ agent, inquiry, }: { agent: { agentId: string; }; inquiry: { _id: string; department: string; }; options: { forwardingToDepartment?: { oldDepartmentId: string; transferData: any; }; clientAction?: boolean; }; }) => { if (!inquiry?._id || !agent?.agentId) { cbLogger.debug('No inquiry or agent provided'); throw new Error('No inquiry or agent provided'); } const { agentId } = agent; if (!(await checkOnlineAgents(undefined, agent))) { cbLogger.debug('Provided agent is not online'); throw new Error('Provided agent is not online'); } if (!settings.get('Livechat_waiting_queue')) { cbLogger.info({ msg: 'Chat can be taken by Agent: waiting queue is disabled', agentId }); return agent; } if (await allowAgentSkipQueue(agent)) { cbLogger.info({ msg: 'Chat can be taken by Agent: agent can skip queue', agentId }); return agent; } const { department: departmentId } = inquiry; const user = await Users.getAgentAndAmountOngoingChats(agentId, departmentId); if (!user) { cbLogger.debug({ msg: 'No valid agent found', agentId }); throw new Error('No valid agent found'); } const { queueInfo: { chats = 0, chatsForDepartment = 0 } = {} } = user; if (await isAgentWithinChatLimits({ agentId, departmentId, totalChats: chats, departmentChats: chatsForDepartment })) { return user; } throw new Error('error-max-number-simultaneous-chats-reached'); }; callbacks.add('livechat.checkAgentBeforeTakeInquiry', validateMaxChats, callbacks.priority.MEDIUM, 'livechat-before-take-inquiry');