Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EE] Improve Forwarding Department behaviour with Waiting queue feature #22043

Merged
merged 15 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions app/livechat/client/views/app/tabbar/visitorForward.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Template.visitorForward.events({
const transferData = {
roomId: instance.room.get()._id,
comment: event.target.comment.value,
clientAction: true,
};

const [user] = instance.selectedAgents.get();
Expand Down
12 changes: 7 additions & 5 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export const forwardRoomToAgent = async (room, transferData) => {
return false;
}

const { userId: agentId } = transferData;
const { userId: agentId, clientAction } = transferData;
const user = Users.findOneOnlineAgentById(agentId);
if (!user) {
throw new Meteor.Error('error-user-is-offline', 'User is offline', { function: 'forwardRoomToAgent' });
Expand All @@ -291,9 +291,11 @@ export const forwardRoomToAgent = async (room, transferData) => {

const { username } = user;
const agent = { agentId, username };
// There are some Enterprise features that may interrupt the fowarding process
// Remove department from inquiry to make sure the routing algorithm treat this as forwarding to agent and not as forwarding to department
inquiry.department = undefined;
// There are some Enterprise features that may interrupt the forwarding process
// Due to that we need to check whether the agent has been changed or not
const roomTaken = await RoutingManager.takeInquiry(inquiry, agent);
const roomTaken = await RoutingManager.takeInquiry(inquiry, agent, { ...clientAction && { clientAction } });
if (!roomTaken) {
return false;
}
Expand Down Expand Up @@ -356,7 +358,7 @@ export const forwardRoomToDepartment = async (room, guest, transferData) => {
throw new Meteor.Error('error-forwarding-chat-same-department', 'The selected department and the current room department are the same', { function: 'forwardRoomToDepartment' });
}

const { userId: agentId } = transferData;
const { userId: agentId, clientAction } = transferData;
if (agentId) {
let user = Users.findOneOnlineAgentById(agentId);
if (!user) {
Expand All @@ -378,7 +380,7 @@ export const forwardRoomToDepartment = async (room, guest, transferData) => {
// Fake the department to forward the inquiry - Case the forward process does not success
// the inquiry will stay in the same original department
inquiry.department = departmentId;
const roomTaken = await RoutingManager.delegateInquiry(inquiry, agent);
const roomTaken = await RoutingManager.delegateInquiry(inquiry, agent, { forwardingToDepartment: { oldDepartmentId, transferData }, ...clientAction && { clientAction } });
if (!roomTaken) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions app/livechat/server/lib/RoutingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const RoutingManager = {
return this.getMethod().getNextAgent(department, ignoreAgentId);
},

async delegateInquiry(inquiry, agent) {
async delegateInquiry(inquiry, agent, options = {}) {
const { department, rid } = inquiry;
if (!agent || (agent.username && !Users.findOneOnlineAgentByUsername(agent.username) && !allowAgentSkipQueue(agent))) {
agent = await this.getNextAgent(department);
Expand All @@ -53,7 +53,7 @@ export const RoutingManager = {
return LivechatRooms.findOneById(rid);
}

return this.takeInquiry(inquiry, agent);
return this.takeInquiry(inquiry, agent, options);
},

assignAgent(inquiry, agent) {
Expand Down
1 change: 1 addition & 0 deletions app/livechat/server/methods/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Meteor.methods({
userId: Match.Optional(String),
departmentId: Match.Optional(String),
comment: Match.Optional(String),
clientAction: Match.Optional(Boolean),
});

const room = LivechatRooms.findOneById(transferData.roomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ const QuickActions: FC<QuickActionsProps> = ({ room, className }) => {
}
const transferData: {
roomId: string;
clientAction: boolean;
comment?: string;
departmentId?: string;
userId?: string;
} = {
roomId: rid,
comment,
clientAction: true,
};

if (departmentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ callbacks.add('livechat.checkAgentBeforeTakeInquiry', async ({ agent, inquiry, o
const { queueInfo: { chats = 0 } = {} } = user;
if (maxNumberSimultaneousChat <= chats) {
callbacks.run('livechat.onMaxNumberSimultaneousChatsReached', inquiry);
if (options.clientAction) {
if (options.clientAction && !options.forwardingToDepartment) {
throw new Meteor.Error('error-max-number-simultaneous-chats-reached', 'Not allowed');
}

Expand Down
54 changes: 44 additions & 10 deletions ee/app/livechat-enterprise/server/hooks/onAgentAssignmentFailed.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
import { callbacks } from '../../../../../app/callbacks/server';
import { LivechatInquiry, Subscriptions, LivechatRooms } from '../../../../../app/models/server';
import { queueInquiry } from '../../../../../app/livechat/server/lib/QueueManager';
import { RoutingManager } from '../../../../../app/livechat/server/lib/RoutingManager';
import { settings } from '../../../../../app/settings/server';
import { Livechat } from '../../../../../app/livechat/server/lib/Livechat';

const handleOnAgentAssignmentFailed = async ({ inquiry, room }: { inquiry: any; room: any }): Promise<any> => {
if (!inquiry || !room || !room.onHold) {
const handleOnAgentAssignmentFailed = async ({ inquiry, room, options }: { inquiry: any; room: any; options: { forwardingToDepartment?: { oldDepartmentId: string; transferData: any }; clienAction?: boolean} }): Promise<any> => {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
if (!inquiry || !room) {
return;
}

const { _id: roomId, servedBy } = room;
if (room.onHold) {
const { _id: roomId, servedBy } = room;

const { _id: inquiryId } = inquiry;
LivechatInquiry.readyInquiry(inquiryId);
LivechatInquiry.removeDefaultAgentById(inquiryId);
LivechatRooms.removeAgentByRoomId(roomId);
if (servedBy?._id) {
Subscriptions.removeByRoomIdAndUserId(roomId, servedBy._id);
const { _id: inquiryId } = inquiry;
LivechatInquiry.readyInquiry(inquiryId);
LivechatInquiry.removeDefaultAgentById(inquiryId);
LivechatRooms.removeAgentByRoomId(roomId);
if (servedBy?._id) {
Subscriptions.removeByRoomIdAndUserId(roomId, servedBy._id);
}

const newInquiry = LivechatInquiry.findOneById(inquiryId);

await queueInquiry(room, newInquiry);

return;
}

const newInquiry = LivechatInquiry.findOneById(inquiryId);
if (!settings.get('Livechat_waiting_queue')) {
return;
}

const { forwardingToDepartment: { oldDepartmentId, transferData } = {}, forwardingToDepartment } = options;
if (!forwardingToDepartment) {
return;
}

const { department: newDepartmentId } = inquiry;

if (!newDepartmentId || !oldDepartmentId || newDepartmentId === oldDepartmentId) {
return;
}

// Undo the FAKE Department we did before RoutingManager.delegateInquiry()
inquiry.department = oldDepartmentId;
RoutingManager.unassignAgent(inquiry, newDepartmentId);

LivechatInquiry.readyInquiry(inquiry._id);

const newInquiry = LivechatInquiry.findOneById(inquiry._id);

await queueInquiry(room, newInquiry);

Livechat.saveTransferHistory(room, transferData);
};

callbacks.add('livechat.onAgentAssignmentFailed', handleOnAgentAssignmentFailed, callbacks.priority.HIGH, 'livechat-agent-assignment-failed');