diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 6864861d93e6..027e163d6bb5 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -81,7 +81,9 @@ export const Livechat = { } } - return Livechat.checkOnlineAgents(department); + const agentsOnline = Livechat.checkOnlineAgents(department); + Livechat.logger.debug(`Are online agents ${ department ? `for department ${ department }` : '' }?: ${ agentsOnline }`); + return agentsOnline; }, getNextAgent(department) { diff --git a/app/livechat/server/lib/QueueManager.js b/app/livechat/server/lib/QueueManager.js index f8d1540e84bd..afd7ce615522 100644 --- a/app/livechat/server/lib/QueueManager.js +++ b/app/livechat/server/lib/QueueManager.js @@ -16,13 +16,13 @@ export const saveQueueInquiry = (inquiry) => { export const queueInquiry = async (room, inquiry, defaultAgent) => { const inquiryAgent = RoutingManager.delegateAgent(defaultAgent, inquiry); - logger.debug(`Delegating inquiry with id ${ inquiry._id } to agent ${ defaultAgent?._id }`); + logger.debug(`Delegating inquiry with id ${ inquiry._id } to agent ${ defaultAgent?.username }`); await callbacks.run('livechat.beforeRouteChat', inquiry, inquiryAgent); inquiry = LivechatInquiry.findOneById(inquiry._id); if (inquiry.status === 'ready') { - logger.debug(`Inquiry with id ${ inquiry._id } is ready. Delegating to agent ${ inquiryAgent?._id }`); + logger.debug(`Inquiry with id ${ inquiry._id } is ready. Delegating to agent ${ inquiryAgent?.username }`); return RoutingManager.delegateInquiry(inquiry, inquiryAgent); } }; diff --git a/app/livechat/server/lib/RoutingManager.js b/app/livechat/server/lib/RoutingManager.js index 52f9a0c858e0..c167cd99648b 100644 --- a/app/livechat/server/lib/RoutingManager.js +++ b/app/livechat/server/lib/RoutingManager.js @@ -45,7 +45,7 @@ export const RoutingManager = { }, async getNextAgent(department, ignoreAgentId) { - logger.debug(`Getting next available agent with method ${ this.name }`); + logger.debug(`Getting next available agent with method ${ this.methodName }`); return this.getMethod().getNextAgent(department, ignoreAgentId); }, @@ -55,6 +55,7 @@ export const RoutingManager = { if (!agent || (agent.username && !Users.findOneOnlineAgentByUserList(agent.username) && !allowAgentSkipQueue(agent))) { logger.debug(`Agent offline or invalid. Using routing method to get next agent for inquiry ${ inquiry._id }`); agent = await this.getNextAgent(department); + logger.debug(`Routing method returned agent ${ agent && agent.agentId } for inquiry ${ inquiry._id }`); } if (!agent) { @@ -62,7 +63,7 @@ export const RoutingManager = { return LivechatRooms.findOneById(rid); } - logger.debug(`Inquiry ${ inquiry._id } will be taken by agent ${ agent._id }`); + logger.debug(`Inquiry ${ inquiry._id } will be taken by agent ${ agent.agentId }`); return this.takeInquiry(inquiry, agent, options); }, @@ -195,7 +196,7 @@ export const RoutingManager = { const defaultAgent = callbacks.run('livechat.beforeDelegateAgent', agent, { department: inquiry?.department }); if (defaultAgent) { - logger.debug(`Delegating Inquiry ${ inquiry._id } to agent ${ defaultAgent._id }`); + logger.debug(`Delegating Inquiry ${ inquiry._id } to agent ${ defaultAgent.username }`); LivechatInquiry.setDefaultAgentById(inquiry._id, defaultAgent); } diff --git a/app/models/server/models/Users.js b/app/models/server/models/Users.js index 149978b18a6b..7f64a06605a2 100644 --- a/app/models/server/models/Users.js +++ b/app/models/server/models/Users.js @@ -9,12 +9,19 @@ import Subscriptions from './Subscriptions'; import { settings } from '../../../settings/server/functions/settings'; const queryStatusAgentOnline = (extraFilters = {}) => ({ - status: { - $exists: true, - $ne: 'offline', - }, statusLivechat: 'available', roles: 'livechat-agent', + $or: [{ + status: { + $exists: true, + $ne: 'offline', + }, + roles: { + $ne: 'bot', + }, + }, { + roles: 'bot', + }], ...extraFilters, ...settings.get('Livechat_enabled_when_agent_idle') === false && { statusConnection: { $ne: 'away' } }, }); diff --git a/ee/app/livechat-enterprise/server/lib/LivechatEnterprise.js b/ee/app/livechat-enterprise/server/lib/LivechatEnterprise.js index d8de93eb4fa9..2662bf4efe2e 100644 --- a/ee/app/livechat-enterprise/server/lib/LivechatEnterprise.js +++ b/ee/app/livechat-enterprise/server/lib/LivechatEnterprise.js @@ -259,7 +259,7 @@ const queueWorker = { }, }; -settings.onload('Livechat_Routing_Method', function() { +function shouldQueueStart() { const routingSupportsAutoAssign = RoutingManager.getConfig().autoAssignAgent; queueLogger.debug(`Routing method ${ RoutingManager.methodName } supports auto assignment: ${ routingSupportsAutoAssign }. ${ routingSupportsAutoAssign @@ -268,4 +268,10 @@ settings.onload('Livechat_Routing_Method', function() { } queue`); routingSupportsAutoAssign ? queueWorker.start() : queueWorker.stop(); +} + +settings.get('Livechat_enabled', (_, value) => { + value && settings.get('Livechat_Routing_Method') ? shouldQueueStart() : queueWorker.stop(); }); + +settings.onload('Livechat_Routing_Method', shouldQueueStart);