diff --git a/apps/meteor/app/livechat/server/hooks/sendToCRM.ts b/apps/meteor/app/livechat/server/hooks/sendToCRM.ts index 6008accc7c7e6..483cbd757da0a 100644 --- a/apps/meteor/app/livechat/server/hooks/sendToCRM.ts +++ b/apps/meteor/app/livechat/server/hooks/sendToCRM.ts @@ -166,19 +166,6 @@ callbacks.add( 'livechat-send-crm-close-room', ); -callbacks.add( - 'livechat.afterTakeInquiry', - async ({ inquiry, room }) => { - if (!settings.get('Livechat_webhook_on_chat_taken')) { - return inquiry; - } - - return sendToCRM('LivechatSessionTaken', room); - }, - callbacks.priority.MEDIUM, - 'livechat-send-crm-room-taken', -); - callbacks.add( 'livechat.chatQueued', (room) => { diff --git a/apps/meteor/app/livechat/server/lib/RoutingManager.ts b/apps/meteor/app/livechat/server/lib/RoutingManager.ts index 6a72ffa94a727..8ff8a76a1b67a 100644 --- a/apps/meteor/app/livechat/server/lib/RoutingManager.ts +++ b/apps/meteor/app/livechat/server/lib/RoutingManager.ts @@ -28,7 +28,7 @@ import { updateChatDepartment, allowAgentSkipQueue, } from './Helper'; -import { beforeDelegateAgent } from './hooks'; +import { afterTakeInquiry, beforeDelegateAgent } from './hooks'; import { callbacks } from '../../../../lib/callbacks'; import { notifyOnLivechatInquiryChangedById, notifyOnLivechatInquiryChanged } from '../../../lib/server/lib/notifyListener'; import { settings } from '../../../settings/server'; @@ -265,14 +265,7 @@ export const RoutingManager: Routing = { } void Apps.self?.getBridges()?.getListenerBridge().livechatEvent(AppEvents.IPostLivechatAgentAssigned, { room: roomAfterUpdate, user }); - callbacks.runAsync( - 'livechat.afterTakeInquiry', - { - inquiry: returnedInquiry, - room: roomAfterUpdate, - }, - agent, - ); + void afterTakeInquiry({ inquiry: returnedInquiry, room: roomAfterUpdate, agent }); void notifyOnLivechatInquiryChangedById(inquiry._id, 'updated', { status: LivechatInquiryStatus.TAKEN, diff --git a/apps/meteor/app/livechat/server/lib/hooks.ts b/apps/meteor/app/livechat/server/lib/hooks.ts index eeb6a5a886608..1f06df202eefc 100644 --- a/apps/meteor/app/livechat/server/lib/hooks.ts +++ b/apps/meteor/app/livechat/server/lib/hooks.ts @@ -8,6 +8,7 @@ import type { IOmnichannelSource, IUser, SelectedAgent, + InquiryWithAgentInfo, } from '@rocket.chat/core-typings'; import { LivechatContacts, LivechatDepartmentAgents, LivechatVisitors, Users } from '@rocket.chat/models'; import { makeFunction } from '@rocket.chat/patch-injection'; @@ -109,3 +110,12 @@ export const onNewRoom = makeFunction(async (room: IOmnichannelRoom) => { await sendToCRM('LivechatSessionStart', room); } }); + +export const afterTakeInquiry = makeFunction( + async ({ room }: { inquiry: InquiryWithAgentInfo; room: IOmnichannelRoom; agent: { agentId: string; username: string } }) => { + if (settings.get('Livechat_webhook_on_chat_taken')) { + return; + } + await sendToCRM('LivechatSessionTaken', room); + }, +); diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterTakeInquiry.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterTakeInquiry.ts index 8b07a2086b315..7a0d02775e408 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterTakeInquiry.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterTakeInquiry.ts @@ -1,25 +1,54 @@ +import type { InquiryWithAgentInfo, IOmnichannelRoom } from '@rocket.chat/core-typings'; +import { LivechatVisitors } from '@rocket.chat/models'; + +import { RoutingManager } from '../../../../../app/livechat/server/lib/RoutingManager'; +import { afterTakeInquiry } from '../../../../../app/livechat/server/lib/hooks'; import { settings } from '../../../../../app/settings/server'; -import { callbacks } from '../../../../../lib/callbacks'; +import { AutoTransferChatScheduler } from '../lib/AutoTransferChatScheduler'; import { debouncedDispatchWaitingQueueStatus } from '../lib/Helper'; +import { OmnichannelQueueInactivityMonitor } from '../lib/QueueInactivityMonitor'; import { cbLogger } from '../lib/logger'; -callbacks.add( - 'livechat.afterTakeInquiry', - async ({ inquiry }) => { - if (!settings.get('Livechat_waiting_queue')) { - return inquiry; - } +afterTakeInquiry.patch( + async ( + originalFn, + { inquiry, room, agent }: { inquiry: InquiryWithAgentInfo; room: IOmnichannelRoom; agent: { agentId: string; username: string } }, + ) => { + await originalFn({ inquiry, room, agent }); if (!inquiry) { - return null; + return; + } + + if (settings.get('Livechat_waiting_queue')) { + const { department } = inquiry; + void debouncedDispatchWaitingQueueStatus(department); + cbLogger.debug({ + msg: 'Queue status updated', + queue: department || 'public', + }); } - const { department } = inquiry; - debouncedDispatchWaitingQueueStatus(department); + if (settings.get('Livechat_last_chatted_agent_routing') && agent && RoutingManager.getConfig()?.autoAssignAgent) { + const { v: { token } = {} } = inquiry; + if (token) { + await LivechatVisitors.updateLastAgentByToken(token, { ...agent, ts: new Date() }); + cbLogger.info({ + msg: 'Updated last agent of visitor', + token, + newAgent: { _id: agent.agentId, username: agent.username }, + }); + } + } + + const autoTransferTimeout = settings.get('Livechat_auto_transfer_chat_timeout'); + if (autoTransferTimeout && !room?.autoTransferredAt && !room?.autoTransferOngoing) { + await AutoTransferChatScheduler.scheduleRoom(inquiry.rid, autoTransferTimeout); + } - cbLogger.debug(`Statuses for queue ${department || 'Public'} updated successfully`); - return inquiry; + const maxQueueWaitTime = settings.get('Livechat_max_queue_wait_time'); + if (maxQueueWaitTime > 0) { + await OmnichannelQueueInactivityMonitor.stopInquiry(inquiry._id); + } }, - callbacks.priority.MEDIUM, - 'livechat-after-take-inquiry', ); diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/autoCloseQueued.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/autoCloseQueued.ts deleted file mode 100644 index 12988bc910b6a..0000000000000 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/autoCloseQueued.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { settings } from '../../../../../app/settings/server'; -import { callbacks } from '../../../../../lib/callbacks'; -import { OmnichannelQueueInactivityMonitor } from '../lib/QueueInactivityMonitor'; - -settings.watch('Livechat_max_queue_wait_time', (value: number) => { - if (!value || value < 0) { - callbacks.remove('livechat.afterTakeInquiry', 'livechat-after-inquiry-taken-remove-schedule'); - return; - } - callbacks.add( - 'livechat.afterTakeInquiry', - async ({ inquiry }): Promise => { - if (!inquiry?._id) { - return; - } - await OmnichannelQueueInactivityMonitor.stopInquiry(inquiry._id); - }, - callbacks.priority.HIGH, - 'livechat-after-inquiry-taken-remove-schedule', - ); -}); diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/handleNextAgentPreferredEvents.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/handleNextAgentPreferredEvents.ts index 985b832326480..75b6b3687a05f 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/handleNextAgentPreferredEvents.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/handleNextAgentPreferredEvents.ts @@ -42,34 +42,9 @@ const getDefaultAgent = async ({ username, id }: { username?: string; id?: strin settings.watch('Livechat_last_chatted_agent_routing', (value) => { if (!value) { callbacks.remove('livechat.onMaxNumberSimultaneousChatsReached', 'livechat-on-max-number-simultaneous-chats-reached'); - callbacks.remove('livechat.afterTakeInquiry', 'livechat-save-default-agent-after-take-inquiry'); return; } - callbacks.add( - 'livechat.afterTakeInquiry', - async ({ inquiry }, agent) => { - if (!inquiry || !agent) { - return inquiry; - } - - if (!RoutingManager.getConfig()?.autoAssignAgent) { - return inquiry; - } - - const { v: { token } = {} } = inquiry; - if (!token) { - return inquiry; - } - - await LivechatVisitors.updateLastAgentByToken(token, { ...agent, ts: new Date() }); - - return inquiry; - }, - callbacks.priority.MEDIUM, - 'livechat-save-default-agent-after-take-inquiry', - ); - callbacks.add( 'livechat.onMaxNumberSimultaneousChatsReached', async (inquiry) => { diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/index.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/index.ts index 4dbbb00ee1b97..598f682f8cc29 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/index.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/index.ts @@ -25,4 +25,3 @@ import './sendPdfTranscriptOnClose'; import './applyRoomRestrictions'; import './afterTagRemoved'; import './manageDepartmentUnit'; -import './autoCloseQueued'; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts index 3f0183687e849..4f078214c1e8f 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/scheduleAutoTransfer.ts @@ -4,7 +4,6 @@ import type { CloseRoomParams } from '../../../../../app/livechat/server/lib/loc import { settings } from '../../../../../app/settings/server'; import { callbacks } from '../../../../../lib/callbacks'; import { AutoTransferChatScheduler } from '../lib/AutoTransferChatScheduler'; -import { cbLogger } from '../lib/logger'; type LivechatCloseCallbackParams = { room: IOmnichannelRoom; @@ -37,32 +36,11 @@ const handleAfterCloseRoom = async (params: LivechatCloseCallbackParams): Promis settings.watch('Livechat_auto_transfer_chat_timeout', (value) => { autoTransferTimeout = value as number; if (!autoTransferTimeout || autoTransferTimeout === 0) { - callbacks.remove('livechat.afterTakeInquiry', 'livechat-auto-transfer-job-inquiry'); callbacks.remove('afterOmnichannelSaveMessage', 'livechat-cancel-auto-transfer-job-after-message'); callbacks.remove('livechat.closeRoom', 'livechat-cancel-auto-transfer-on-close-room'); return; } - callbacks.add( - 'livechat.afterTakeInquiry', - async ({ inquiry, room }): Promise => { - const { rid } = inquiry; - if (!rid?.trim()) { - return; - } - - if (room.autoTransferredAt || room.autoTransferOngoing) { - return inquiry; - } - - cbLogger.info(`Room ${room._id} will be scheduled to be auto transfered after ${autoTransferTimeout} seconds`); - await AutoTransferChatScheduler.scheduleRoom(rid, autoTransferTimeout as number); - - return inquiry; - }, - callbacks.priority.MEDIUM, - 'livechat-auto-transfer-job-inquiry', - ); callbacks.add( 'afterOmnichannelSaveMessage', async (message: IMessage, { room }): Promise => { diff --git a/apps/meteor/lib/callbacks.ts b/apps/meteor/lib/callbacks.ts index 9456a1bc9d1bc..e6b014e9d7cd0 100644 --- a/apps/meteor/lib/callbacks.ts +++ b/apps/meteor/lib/callbacks.ts @@ -13,7 +13,6 @@ import type { Username, IOmnichannelRoom, ILivechatTag, - InquiryWithAgentInfo, ILivechatTagRecord, TransferData, AtLeast, @@ -58,10 +57,6 @@ interface EventLikeCallbackSignatures { 'livechat.setUserStatusLivechat': (params: { userId: IUser['_id']; status: OmnichannelAgentStatus }) => void; 'livechat.agentStatusChanged': (params: { userId: IUser['_id']; status: UserStatus }) => void; 'livechat.onNewAgentCreated': (agentId: string) => void; - 'livechat.afterTakeInquiry': ( - params: { inquiry: InquiryWithAgentInfo; room: IOmnichannelRoom }, - agent: { agentId: string; username: string }, - ) => void; 'livechat.afterAgentRemoved': (params: { agent: Pick }) => void; 'afterAddedToRoom': (params: { user: IUser; inviter?: IUser }, room: IRoom) => void; 'beforeAddedToRoom': (params: {