Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions apps/meteor/app/livechat/server/hooks/sendToCRM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
11 changes: 2 additions & 9 deletions apps/meteor/app/livechat/server/lib/RoutingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions apps/meteor/app/livechat/server/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
},
);
Original file line number Diff line number Diff line change
@@ -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<boolean>('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<number>('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<number>('Livechat_max_queue_wait_time');
if (maxQueueWaitTime > 0) {
await OmnichannelQueueInactivityMonitor.stopInquiry(inquiry._id);
}
},
callbacks.priority.MEDIUM,
'livechat-after-take-inquiry',
);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,9 @@ const getDefaultAgent = async ({ username, id }: { username?: string; id?: strin
settings.watch<boolean>('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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ import './sendPdfTranscriptOnClose';
import './applyRoomRestrictions';
import './afterTagRemoved';
import './manageDepartmentUnit';
import './autoCloseQueued';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<any> => {
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<IMessage> => {
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
Username,
IOmnichannelRoom,
ILivechatTag,
InquiryWithAgentInfo,
ILivechatTagRecord,
TransferData,
AtLeast,
Expand Down Expand Up @@ -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<IUser, '_id' | 'username'> }) => void;
'afterAddedToRoom': (params: { user: IUser; inviter?: IUser }, room: IRoom) => void;
'beforeAddedToRoom': (params: {
Expand Down
Loading