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

Regression: New Livechat methods and processes #15242

Merged
merged 23 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
85bb4d5
Fixed some Livechat methods and processes.
renatobecker Aug 23, 2019
8e6c02a
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Aug 23, 2019
aa17775
Fixed missing const statement.
renatobecker Aug 23, 2019
71e96e6
Removed unused model.
renatobecker Aug 23, 2019
3ee3bb0
Add new checks to make sure the room is open.
renatobecker Aug 25, 2019
4e81dcd
Remove the Inquiry when the conversation closes.
renatobecker Aug 26, 2019
f095480
Removed invalid blank line.
renatobecker Aug 26, 2019
c43587c
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Aug 28, 2019
2ae7352
Replace old permissions on Livechat publications.
renatobecker Aug 28, 2019
2436f13
Add new methods to add/remove options from Settings.
renatobecker Aug 28, 2019
518c440
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Aug 28, 2019
310bb25
Fix missing trailing comma.
renatobecker Aug 28, 2019
d4ecee0
Use setFNameById method to rename Livechat Rooms.
renatobecker Aug 29, 2019
bda6922
Replaced Room model(client) with LivechatRoom model, because there wa…
renatobecker Aug 30, 2019
6715155
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Sep 6, 2019
85d09da
Improvements on the codebase.
renatobecker Sep 6, 2019
1c6560c
Add new validation to allow agents get access to rooms publication.
renatobecker Sep 6, 2019
46f5864
Improve check permission.
renatobecker Sep 7, 2019
d949e93
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Sep 9, 2019
3da6e22
Rename Livechat Routing Methods.
renatobecker Sep 9, 2019
5f2b8b3
Update package value.
renatobecker Sep 9, 2019
13a4870
Merge branch 'develop' into fix-livechat-new-methods
renatobecker Sep 9, 2019
6414a69
Remove old translations from English file.
renatobecker Sep 9, 2019
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
14 changes: 9 additions & 5 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,11 +79,15 @@ 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.room.set(room);
this.tags.set((room && room.tags) || []);
this.autorun(() => {
const sub = this.subscribe('livechat:rooms', { _id: rid });
renatobecker-zz marked this conversation as resolved.
Show resolved Hide resolved
if (sub.ready()) {
const room = LivechatRoom.findOne({ _id: rid });
this.room.set(room);
this.tags.set((room && room.tags) || []);
}
});

const uid = Meteor.userId();
Expand Down
22 changes: 12 additions & 10 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 @@ -299,11 +299,13 @@ Template.visitorInfo.onCreated(function() {

if (rid) {
this.autorun(() => {
const room = Rooms.findOne({ _id: rid });
this.room.set(room);
this.visitorId.set(room && room.v && room.v._id);
this.departmentId.set(room && room.departmentId);
this.tags.set(room && room.tags);
const sub = this.subscribe('livechat:rooms', { _id: rid });
renatobecker-zz marked this conversation as resolved.
Show resolved Hide resolved
if (sub.ready()) {
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);
}
});

this.subscribe('livechat:visitorInfo', { rid });
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
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;
renatobecker-zz marked this conversation as resolved.
Show resolved Hide resolved
};

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;
renatobecker-zz marked this conversation as resolved.
Show resolved Hide resolved
};
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