Skip to content

Commit

Permalink
Regression: New Livechat methods and processes (#15242)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatobecker authored and sampaiodiego committed Sep 9, 2019
1 parent a9a87c6 commit 7c798f5
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 140 deletions.
8 changes: 5 additions & 3 deletions app/livechat/client/views/app/tabbar/visitorEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { t } from '../../../../../utils';
import { hasRole } from '../../../../../authorization';
import { LivechatVisitor } from '../../../collections/LivechatVisitor';
import { LivechatDepartmentAgents } from '../../../collections/LivechatDepartmentAgents';
import { Rooms } from '../../../../../models';
import { LivechatRoom } from '../../../collections/LivechatRoom';
import './visitorEdit.html';

Template.visitorEdit.helpers({
Expand Down Expand Up @@ -79,9 +79,11 @@ Template.visitorEdit.onCreated(function() {
this.visitor.set(LivechatVisitor.findOne({ _id: Template.currentData().visitorId }));
});

this.autorun(() => {
const room = Rooms.findOne({ _id: Template.currentData().roomId });
const rid = Template.currentData().roomId;

this.subscribe('livechat:rooms', { _id: rid });
this.autorun(() => {
const room = LivechatRoom.findOne({ _id: rid });
this.room.set(room);
this.tags.set((room && room.tags) || []);
});
Expand Down
14 changes: 7 additions & 7 deletions app/livechat/client/views/app/tabbar/visitorInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import moment from 'moment';
import UAParser from 'ua-parser-js';

import { modal } from '../../../../../ui-utils';
import { Rooms, Subscriptions } from '../../../../../models';
import { Subscriptions } from '../../../../../models';
import { settings } from '../../../../../settings';
import { t, handleError, roomTypes } from '../../../../../utils';
import { hasRole, hasAllPermission, hasAtLeastOnePermission } from '../../../../../authorization';
import { LivechatVisitor } from '../../../collections/LivechatVisitor';
import { LivechatDepartment } from '../../../collections/LivechatDepartment';
import { LivechatRoom } from '../../../collections/LivechatRoom';
import './visitorInfo.html';

const isSubscribedToRoom = () => {
Expand Down Expand Up @@ -50,7 +51,7 @@ Template.visitorInfo.helpers({
},

room() {
return Template.instance().room.get();
return LivechatRoom.findOne({ _id: this.rid });
},

department() {
Expand All @@ -72,7 +73,7 @@ Template.visitorInfo.helpers({

const data = Template.currentData();
if (data && data.rid) {
const room = Rooms.findOne(data.rid);
const room = LivechatRoom.findOne(data.rid);
if (room) {
livechatData = _.extend(livechatData, room.livechatData);
}
Expand Down Expand Up @@ -147,7 +148,7 @@ Template.visitorInfo.helpers({
},

roomOpen() {
const room = Template.instance().room.get();
const room = LivechatRoom.findOne({ _id: this.rid });
const uid = Meteor.userId();
return room && room.open && ((room.servedBy && room.servedBy._id === uid) || hasRole(uid, 'livechat-manager'));
},
Expand Down Expand Up @@ -281,7 +282,6 @@ Template.visitorInfo.onCreated(function() {
this.user = new ReactiveVar();
this.departmentId = new ReactiveVar(null);
this.tags = new ReactiveVar(null);
this.room = new ReactiveVar(null);
this.routingConfig = new ReactiveVar({});

Meteor.call('livechat:getCustomFields', (err, customFields) => {
Expand All @@ -298,9 +298,9 @@ Template.visitorInfo.onCreated(function() {
});

if (rid) {
this.subscribe('livechat:rooms', { _id: rid });
this.autorun(() => {
const room = Rooms.findOne({ _id: rid });
this.room.set(room);
const room = LivechatRoom.findOne({ _id: rid });
this.visitorId.set(room && room.v && room.v._id);
this.departmentId.set(room && room.departmentId);
this.tags.set(room && room.tags);
Expand Down
39 changes: 7 additions & 32 deletions app/livechat/lib/LivechatInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ if (Meteor.isServer) {
this.tryEnsureIndex({ name: 1 }); // name of the inquiry (client name for now)
this.tryEnsureIndex({ message: 1 }); // message sent by the client
this.tryEnsureIndex({ ts: 1 }); // timestamp
this.tryEnsureIndex({ agents: 1 }); // Id's of the agents who can see the inquiry (handle departments)
this.tryEnsureIndex({ department: 1 });
this.tryEnsureIndex({ status: 1 }); // 'ready', 'queued', 'taken'
}
Expand Down Expand Up @@ -56,23 +55,6 @@ if (Meteor.isServer) {
});
}

/*
* mark the inquiry as closed
*/
closeByRoomId(roomId, closeInfo) {
return this.update({
rid: roomId,
}, {
$set: {
status: 'closed',
closer: closeInfo.closer,
closedBy: closeInfo.closedBy,
closedAt: closeInfo.closedAt,
'metrics.chatDuration': closeInfo.chatDuration,
},
});
}

/*
* mark inquiry as open
*/
Expand All @@ -95,20 +77,6 @@ if (Meteor.isServer) {
});
}

/*
* mark inquiry as open and set agents
*/
queueInquiryWithAgents(inquiryId, agentIds) {
return this.update({
_id: inquiryId,
}, {
$set: {
status: 'queued',
agents: agentIds,
},
});
}

changeDepartmentIdByRoomId(rid, department) {
const query = {
rid,
Expand Down Expand Up @@ -199,6 +167,13 @@ if (Meteor.isServer) {

return collectionObj.aggregate(aggregate).toArray();
}

/*
* remove the inquiry by roomId
*/
removeByRoomId(rid) {
return this.remove({ rid });
}
}

LivechatInquiry = new LivechatInquiryClass();
Expand Down
2 changes: 1 addition & 1 deletion app/livechat/server/api/v1/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ API.v1.addRoute('livechat/room.transfer', {
// update visited page history to not expire
Messages.keepHistoryForToken(token);

if (!Livechat.transfer(room, guest, { roomId: rid, departmentId: department })) {
if (!Promise.await(Livechat.transfer(room, guest, { roomId: rid, departmentId: department }))) {
return API.v1.failure();
}

Expand Down
8 changes: 4 additions & 4 deletions app/livechat/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,15 @@ Meteor.startup(function() {
i18nLabel: 'RDStation_Token',
});

settings.add('Livechat_Routing_Method', 'Least_Amount', {
settings.add('Livechat_Routing_Method', 'Auto_Selection', {
type: 'select',
group: 'Livechat',
public: true,
section: 'Routing',
values: [
{ key: 'External', i18nLabel: 'External_Service' },
{ key: 'Least_Amount', i18nLabel: 'Least_Amount' },
{ key: 'Guest_Pool', i18nLabel: 'Guest_Pool' },
{ key: 'Auto_Selection', i18nLabel: 'Auto_Selection' },
{ key: 'Manual_Selection', i18nLabel: 'Manual_Selection' },
],
});

Expand All @@ -388,7 +388,7 @@ Meteor.startup(function() {
section: 'Routing',
i18nLabel: 'Max_number_incoming_livechats_displayed',
i18nDescription: 'Max_number_incoming_livechats_displayed_description',
enableQuery: { _id: 'Livechat_Routing_Method', value: 'Guest_Pool' },
enableQuery: { _id: 'Livechat_Routing_Method', value: 'Manual_Selection' },
});

settings.add('Livechat_show_queue_list_link', false, {
Expand Down
4 changes: 2 additions & 2 deletions app/livechat/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ import './lib/QueueManager';
import './lib/OfficeClock';
import './lib/RoutingManager';
import './lib/routing/External';
import './lib/routing/GuestPool';
import './lib/routing/LeastAmount';
import './lib/routing/ManualSelection';
import './lib/routing/AutoSelection';
import './sendMessageBySMS';
import './unclosedLivechats';
import './publications/customFields';
Expand Down
86 changes: 51 additions & 35 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Messages, LivechatRooms, Rooms, Subscriptions, Users } from '../../../m
import { LivechatInquiry } from '../../lib/LivechatInquiry';
import { Livechat } from './Livechat';
import { RoutingManager } from './RoutingManager';
import { callbacks } from '../../../callbacks/server';

export const createLivechatRoom = (rid, name, guest, extraData) => {
check(rid, String);
Expand Down Expand Up @@ -39,7 +40,9 @@ export const createLivechatRoom = (rid, name, guest, extraData) => {
waitingResponse: true,
}, extraData);

return Rooms.insert(room);
const roomId = Rooms.insert(room);
callbacks.run('livechat.newRoom', room);
return roomId;
};

export const createLivechatInquiry = (rid, name, guest, message, initialStatus) => {
Expand Down Expand Up @@ -146,6 +149,11 @@ export const createLivechatQueueView = () => {
});
};

export const removeAgentFromSubscription = (rid, { _id, username }) => {
Subscriptions.removeByRoomIdAndUserId(rid, _id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id, username });
};

export const dispatchAgentDelegated = (rid, agentId) => {
const agent = agentId && Users.getAgentInfo(agentId);
Livechat.stream.emit(rid, {
Expand All @@ -155,6 +163,10 @@ export const dispatchAgentDelegated = (rid, agentId) => {
};

export const forwardRoomToAgent = async (room, agentId) => {
if (!room || !room.open) {
return false;
}

const user = Users.findOneOnlineAgentById(agentId);
if (!user) {
return false;
Expand All @@ -166,31 +178,36 @@ export const forwardRoomToAgent = async (room, agentId) => {
throw new Meteor.Error('error-transferring-inquiry');
}

if (oldServedBy && agentId === oldServedBy._id) {
return false;
}

const { username } = user;
const agent = { agentId, username };
// There are some Enterprise features that may interrupt the fowarding process
// Due to that we need to check whether the agent has been changed or not
const roomTaken = await RoutingManager.takeInquiry(inquiry, agent);
if (!roomTaken) {
return false;
}

if (oldServedBy && agent.agentId !== oldServedBy._id) {
// There are some Enterprise features that may interrupt the fowarding process
// Due to that we need to check whether the agent has been changed or not
const room = await RoutingManager.takeInquiry(inquiry, agent);
if (!room) {
return false;
const { servedBy } = roomTaken;
if (servedBy) {
if (oldServedBy && servedBy._id !== oldServedBy._id) {
removeAgentFromSubscription(rid, oldServedBy);
}

const { servedBy } = room;
if (servedBy && servedBy._id !== oldServedBy._id) {
Subscriptions.removeByRoomIdAndUserId(rid, oldServedBy._id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id: oldServedBy._id, username: oldServedBy.username });
Messages.createUserJoinWithRoomIdAndUser(rid, { _id: agent.agentId, username });
return true;
}
Messages.createUserJoinWithRoomIdAndUser(rid, { _id: servedBy._id, username: servedBy.username });
}

return false;
return true;
};

export const forwardRoomToDepartment = async (room, guest, departmentId) => {
if (!room || !room.open) {
return false;
}

const { _id: rid, servedBy: oldServedBy } = room;

const inquiry = LivechatInquiry.findOneByRoomId(rid);
Expand All @@ -205,30 +222,29 @@ export const forwardRoomToDepartment = async (room, guest, departmentId) => {
// 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;
room = await RoutingManager.delegateInquiry(inquiry);
if (!room) {
const roomTaken = await RoutingManager.delegateInquiry(inquiry);
if (!roomTaken) {
return false;
}

const { servedBy } = room;
// if there was an agent assigned to the chat and there is no new agent assigned
// or the new agent is not the same, then the fowarding process successed
if (oldServedBy && (!servedBy || oldServedBy._id !== servedBy._id)) {
Subscriptions.removeByRoomIdAndUserId(rid, oldServedBy._id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id: oldServedBy._id, username: oldServedBy.username });
LivechatRooms.changeDepartmentIdByRoomId(rid, departmentId);
LivechatInquiry.changeDepartmentIdByRoomId(rid, departmentId);
// Update the visitor's department
const { token } = guest;
Livechat.setDepartmentForGuest({ token, department: departmentId });

if (servedBy) {
const { _id, username } = servedBy;
Messages.createUserJoinWithRoomIdAndUser(rid, { _id, username });
}
const { servedBy } = roomTaken;
if (oldServedBy && servedBy && oldServedBy._id === servedBy._id) {
return false;
}

if (oldServedBy) {
removeAgentFromSubscription(rid, oldServedBy);
}

return true;
if (servedBy) {
Messages.createUserJoinWithRoomIdAndUser(rid, servedBy);
}

return false;
LivechatRooms.changeDepartmentIdByRoomId(rid, departmentId);
LivechatInquiry.changeDepartmentIdByRoomId(rid, departmentId);

const { token } = guest;
Livechat.setDepartmentForGuest({ token, department: departmentId });

return true;
};
16 changes: 9 additions & 7 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Messages,
Subscriptions,
Settings,
Rooms,
LivechatDepartmentAgents,
LivechatDepartment,
LivechatCustomField,
Expand Down Expand Up @@ -294,8 +295,9 @@ export const Livechat = {
};
}

LivechatRooms.closeByRoomId(room._id, closeData);
LivechatInquiry.closeByRoomId(room._id, closeData);
const { _id: rid, servedBy } = room;
LivechatRooms.closeByRoomId(rid, closeData);
LivechatInquiry.removeByRoomId(rid);

const message = {
t: 'livechat-close',
Expand All @@ -304,14 +306,14 @@ export const Livechat = {
};

// Retreive the closed room
room = LivechatRooms.findOneByIdOrName(room._id);
room = LivechatRooms.findOneByIdOrName(rid);

sendMessage(user, message, room);

if (room.servedBy) {
Subscriptions.hideByRoomIdAndUserId(room._id, room.servedBy._id);
if (servedBy) {
Subscriptions.hideByRoomIdAndUserId(rid, servedBy._id);
}
Messages.createCommandWithRoomIdAndUser('promptTranscript', room._id, closeData.closedBy);
Messages.createCommandWithRoomIdAndUser('promptTranscript', rid, closeData.closedBy);

Meteor.defer(() => {
callbacks.run('livechat.closeRoom', room);
Expand Down Expand Up @@ -388,7 +390,7 @@ export const Livechat = {
});

if (!_.isEmpty(guestData.name)) {
return LivechatRooms.setNameById(roomData._id, guestData.name, guestData.name) && Subscriptions.updateDisplayNameByRoomId(roomData._id, guestData.name);
return Rooms.setFnameById(roomData._id, guestData.name) && Subscriptions.updateDisplayNameByRoomId(roomData._id, guestData.name);
}
},

Expand Down
Loading

0 comments on commit 7c798f5

Please sign in to comment.