From 6592600c4ac4ed7203ab63ae601508b29149fb6c Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Thu, 24 Oct 2024 15:23:27 -0600 Subject: [PATCH] restore waiting queue behavior --- .../app/livechat/server/lib/QueueManager.ts | 44 ++++++++++++++++--- .../app/livechat/server/lib/RoutingManager.ts | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/QueueManager.ts b/apps/meteor/app/livechat/server/lib/QueueManager.ts index 24be8d42b7a4..d2aae06f5b5f 100644 --- a/apps/meteor/app/livechat/server/lib/QueueManager.ts +++ b/apps/meteor/app/livechat/server/lib/QueueManager.ts @@ -133,11 +133,7 @@ export class QueueManager { return LivechatInquiryStatus.READY; } - static async queueInquiry(inquiry: ILivechatInquiryRecord, room: IOmnichannelRoom, defaultAgent?: SelectedAgent | null) { - if (inquiry.status === 'ready') { - return RoutingManager.delegateInquiry(inquiry, defaultAgent, undefined, room); - } - + static async notifyQueuedInquiry(inquiry: ILivechatInquiryRecord, room: IOmnichannelRoom, defaultAgent?: SelectedAgent | null) { await callbacks.run('livechat.afterInquiryQueued', inquiry); void callbacks.run('livechat.chatQueued', room); @@ -145,6 +141,34 @@ export class QueueManager { await this.dispatchInquiryQueued(inquiry, room, defaultAgent); } + static async queueInquiry(inquiry: ILivechatInquiryRecord, room: IOmnichannelRoom, defaultAgent?: SelectedAgent | null) { + if (!(await Omnichannel.isWithinMACLimit(room))) { + logger.error({ msg: 'MAC limit reached, not routing inquiry', inquiry }); + // We'll queue these inquiries so when new license is applied, they just start rolling again + // Minimizing disruption + await saveQueueInquiry(inquiry); + await this.notifyQueuedInquiry(inquiry, room, defaultAgent); + return; + } + + await callbacks.run('livechat.beforeRouteChat', inquiry, defaultAgent); + const dbInquiry = await LivechatInquiry.findOneById(inquiry._id); + + if (!dbInquiry) { + throw new Error('inquiry-not-found'); + } + + if (dbInquiry.status === 'ready') { + logger.debug({ msg: 'Inquiry is ready. Delegating', inquiry, defaultAgent }); + return RoutingManager.delegateInquiry(dbInquiry, defaultAgent, undefined, room); + } + + if (dbInquiry.status === 'queued') { + logger.debug(`Inquiry with id ${inquiry._id} is queued. Notifying`); + await this.notifyQueuedInquiry(inquiry, room, defaultAgent); + } + } + static async requestRoom({ guest, rid = Random.id(), @@ -252,7 +276,11 @@ export class QueueManager { throw new Error('room-not-found'); } - if (!newRoom.servedBy && settings.get('Omnichannel_calculate_dispatch_service_queue_statistics')) { + if ( + !newRoom.servedBy && + settings.get('Livechat_waiting_queue') && + settings.get('Omnichannel_calculate_dispatch_service_queue_statistics') + ) { const [inq] = await LivechatInquiry.getCurrentSortedQueueAsync({ inquiryId: inquiry._id, department, @@ -320,6 +348,10 @@ export class QueueManager { } private static dispatchInquiryQueued = async (inquiry: ILivechatInquiryRecord, room: IOmnichannelRoom, agent?: SelectedAgent | null) => { + if (RoutingManager.getConfig()?.autoAssignAgent) { + return; + } + logger.debug(`Notifying agents of new inquiry ${inquiry._id} queued`); const { department, rid, v } = inquiry; diff --git a/apps/meteor/app/livechat/server/lib/RoutingManager.ts b/apps/meteor/app/livechat/server/lib/RoutingManager.ts index 8781f675ebf9..ece6e05c33b5 100644 --- a/apps/meteor/app/livechat/server/lib/RoutingManager.ts +++ b/apps/meteor/app/livechat/server/lib/RoutingManager.ts @@ -57,7 +57,7 @@ type Routing = { room: IOmnichannelRoom, ): Promise; transferRoom(room: IOmnichannelRoom, guest: ILivechatVisitor, transferData: TransferData): Promise; - delegateAgent(agent: SelectedAgent | undefined, inquiry: ILivechatInquiryRecord): Promise; + delegateAgent(agent: SelectedAgent | undefined | null, inquiry: ILivechatInquiryRecord): Promise; removeAllRoomSubscriptions(room: Pick, ignoreUser?: { _id: string }): Promise; assignAgent(inquiry: InquiryWithAgentInfo, room: IOmnichannelRoom, agent: SelectedAgent): Promise;