import type { IOmnichannelCustomAgent } from '@rocket.chat/core-typings'; import { Users } from '@rocket.chat/models'; import type { IRoutingManagerConfig } from './IRoutingManagerConfig'; import { RoutingManager } from '../../../../../server/lib/omnichannel/RoutingManager'; import { settings } from '../../../../../server/settings'; import { getChatLimitsQuery } from '../../../hooks/omnichannel/applySimultaneousChatsRestrictions'; import { logger } from '../logger'; /* Load Rotation Queuing method: * Routing method where the agent with the oldest routing time is the next agent to serve incoming chats */ class LoadRotation { private _config: IRoutingManagerConfig; constructor() { this._config = { previewRoom: false, showConnecting: false, showQueue: false, showQueueLink: false, returnQueue: false, enableTriggerAction: true, autoAssignAgent: true, }; } get config(): IRoutingManagerConfig { return this._config; } public async getNextAgent(department?: string, ignoreAgentId?: string): Promise { const enabledWhenIdle = settings.get('Livechat_enabled_when_agent_idle'); const enabledWhenOffline = settings.get('Livechat_accept_chats_with_no_agents'); const extraQuery = await getChatLimitsQuery(department); const unavailableUsers = await Users.getUnavailableAgents(department, extraQuery, enabledWhenIdle, enabledWhenOffline); logger.debug({ msg: 'Ignoring unavailable agents from assignment', unavailableUsers, department, enabledWhenIdle, enabledWhenOffline }); const nextAgent = await Users.getLastAvailableAgentRouted( department, ignoreAgentId, enabledWhenIdle, unavailableUsers.map((user) => user.username), enabledWhenOffline, ); if (!nextAgent?.username) { return; } const { agentId, username } = nextAgent; return { agentId, username }; } } RoutingManager.registerMethod('Load_Rotation', LoadRotation);