From 6ea78fdecc7cde863e773ca16e780f608b89e9a3 Mon Sep 17 00:00:00 2001 From: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com> Date: Mon, 20 Sep 2021 18:49:17 +0530 Subject: [PATCH] [FIX] Omnichannel On hold chats being forwarded to offline agents (#23185) * Fix on-hold chats being forwarded to offline agents * Dispatch agent changed event for livechat upon resuming onhold chat + logger issues * Apply suggestions from code review Co-authored-by: Kevin Aleman Co-authored-by: Kevin Aleman --- ...uiry.js => checkAgentBeforeTakeInquiry.ts} | 54 +++++++++++++++---- .../server/hooks/onAgentAssignmentFailed.ts | 5 +- 2 files changed, 47 insertions(+), 12 deletions(-) rename ee/app/livechat-enterprise/server/hooks/{checkAgentBeforeTakeInquiry.js => checkAgentBeforeTakeInquiry.ts} (66%) diff --git a/ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.js b/ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.ts similarity index 66% rename from ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.js rename to ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.ts index 175c1bb1210..85d85b7167a 100644 --- a/ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.js +++ b/ee/app/livechat-enterprise/server/hooks/checkAgentBeforeTakeInquiry.ts @@ -1,32 +1,64 @@ import { Meteor } from 'meteor/meteor'; -import { callbacks } from '../../../../../app/callbacks'; +import { callbacks } from '../../../../../app/callbacks/server'; import { Users } from '../../../../../app/models/server/raw'; -import { settings } from '../../../../../app/settings'; +import { settings } from '../../../../../app/settings/server'; import { getMaxNumberSimultaneousChat } from '../lib/Helper'; import { allowAgentSkipQueue } from '../../../../../app/livechat/server/lib/Helper'; import { cbLogger } from '../lib/logger'; +import { Livechat } from '../../../../../app/livechat/server'; -callbacks.add('livechat.checkAgentBeforeTakeInquiry', async ({ agent, inquiry, options }) => { - if (!settings.get('Livechat_waiting_queue')) { - cbLogger.debug('Skipping callback. Disabled by setting'); - return agent; +callbacks.add('livechat.checkAgentBeforeTakeInquiry', async ({ + agent, + inquiry, + options, +}: { + agent: { + agentId: string; + }; + inquiry: { + _id: string; + department: string; + }; + options: { + forwardingToDepartment? : { + oldDepartmentId: string; + transferData: any; + }; + clientAction? : boolean; + }; +}) => { + if (!inquiry?._id || !agent?.agentId) { + cbLogger.debug('Callback with error. No inquiry or agent provided'); + return null; } + const { + agentId, + } = agent; - if (!inquiry || !agent) { - cbLogger.debug('Callback with error. No inquiry or agent provided'); + if (!Livechat.checkOnlineAgents(null, agent)) { + cbLogger.debug('Callback with error. provided agent is not online'); return null; } + if (!settings.get('Livechat_waiting_queue')) { + cbLogger.debug('Skipping callback. Disabled by setting'); + return agent; + } + if (allowAgentSkipQueue(agent)) { cbLogger.debug(`Callback success. Agent ${ agent.agentId } can skip queue`); return agent; } - const { department: departmentId } = inquiry; - const { agentId } = agent; + const { + department: departmentId, + } = inquiry; - const maxNumberSimultaneousChat = getMaxNumberSimultaneousChat({ agentId, departmentId }); + const maxNumberSimultaneousChat = getMaxNumberSimultaneousChat({ + agentId, + departmentId, + }); if (maxNumberSimultaneousChat === 0) { cbLogger.debug(`Callback success. Agent ${ agentId } max number simultaneous chats on range`); return agent; diff --git a/ee/app/livechat-enterprise/server/hooks/onAgentAssignmentFailed.ts b/ee/app/livechat-enterprise/server/hooks/onAgentAssignmentFailed.ts index 1defff8f75e..f8ebf12e436 100644 --- a/ee/app/livechat-enterprise/server/hooks/onAgentAssignmentFailed.ts +++ b/ee/app/livechat-enterprise/server/hooks/onAgentAssignmentFailed.ts @@ -3,6 +3,7 @@ import { LivechatInquiry, Subscriptions, LivechatRooms } from '../../../../../ap import { queueInquiry } from '../../../../../app/livechat/server/lib/QueueManager'; import { settings } from '../../../../../app/settings/server'; import { cbLogger } from '../lib/logger'; +import { dispatchAgentDelegated } from '../../../../../app/livechat/server/lib/Helper'; const handleOnAgentAssignmentFailed = async ({ inquiry, room, options }: { inquiry: any; room: any; options: { forwardingToDepartment?: { oldDepartmentId: string; transferData: any }; clientAction?: boolean} }): Promise => { if (!inquiry || !room) { @@ -15,10 +16,12 @@ const handleOnAgentAssignmentFailed = async ({ inquiry, room, options }: { inqui const { _id: roomId } = room; const { _id: inquiryId } = inquiry; - LivechatInquiry.readyInquiry(inquiryId); + LivechatInquiry.queueInquiry(inquiryId); LivechatInquiry.removeDefaultAgentById(inquiryId); LivechatRooms.removeAgentByRoomId(roomId); Subscriptions.removeByRoomId(roomId); + dispatchAgentDelegated(roomId, null); + const newInquiry = LivechatInquiry.findOneById(inquiryId); await queueInquiry(room, newInquiry);