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
12 changes: 11 additions & 1 deletion packages/rocketchat-livechat/.app/client/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ const api = {
data.token = Random.id();
}

if (data.department) {
api.setDepartment(data.department);
}

Meteor.call('livechat:registerGuest', data, function(error, result) {
if (!error && result.visitor && result.visitor.token) {
if (!error) {
visitor.reset();
}

if (result && result.visitor && result.visitor.token) {
visitor.setToken(result.visitor.token);
visitor.setId(result.userId);
visitor.setData(result.visitor);
}
});
},
Expand Down
27 changes: 17 additions & 10 deletions packages/rocketchat-livechat/.app/client/views/livechatWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Template.livechatWindow.onCreated(function() {

this.autorun(() => {
// get all needed live chat info for the user
Meteor.call('livechat:getInitialData', visitor.getToken(), (err, result) => {
Meteor.call('livechat:getInitialData', visitor.getToken(), Livechat.department, (err, result) => {
if (err) {
return console.error(err);
}
Expand Down Expand Up @@ -131,24 +131,31 @@ Template.livechatWindow.onCreated(function() {
Livechat.registrationForm = result.registrationForm;
Livechat.nameFieldRegistrationForm = result.nameFieldRegistrationForm;
Livechat.emailFieldRegistrationForm = result.emailFieldRegistrationForm;
if (result.room) {
Livechat.room = result.room._id;

visitor.setConnected();
}

if (result.visitor) {
visitor.setData(result.visitor);

if (visitor.name) {
Livechat.guestName = visitor.name;
if (result.visitor.name) {
Livechat.guestName = result.visitor.name;
}

if (result.visitor.visitorEmails && result.visitor.visitorEmails.length > 0) {
Livechat.guestEmail = result.visitor.visitorEmails[0].address;
}

if (visitor.visitorEmails && visitor.visitorEmails.length > 0) {
Livechat.guestEmail = visitor.visitorEmails[0].address;
if (!Livechat.department) {
Livechat.department = result.visitor.department;
}
}

let room;
if (result.room && (!result.room.departmentId || !Livechat.department || result.room.departmentId === Livechat.department)) {
room = result.room._id;

visitor.setConnected();
}
Livechat.room = room;

if (result.agentData) {
Livechat.agent = result.agentData;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/rocketchat-livechat/.app/client/views/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ Template.register.events({
}
}

if (departmentId) {
Livechat.department = departmentId;
}

const guest = {
token: visitor.getToken(),
name,
email,
department: Livechat.department || departmentId,
department: Livechat.department,
};

Meteor.call('livechat:registerGuest', guest, function(error, result) {
if (error != null) {
return instance.showError(error.reason);
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-livechat/.app/imports/client/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export default {
return;
}

msgStream.unsubscribe(this.roomSubscribed);

this.roomSubscribed = roomId;

const msgTypesNotDisplayed = ['livechat_video_call', 'livechat_navigation_history', 'au'];
Expand Down
15 changes: 12 additions & 3 deletions packages/rocketchat-livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ RocketChat.Livechat = {
} else {
const userData = {
username,
department,
};

if (this.connection) {
Expand Down Expand Up @@ -175,6 +174,10 @@ RocketChat.Livechat = {
updateUser.$set.name = name;
}

if (department) {
updateUser.$set.department = department;
}

LivechatVisitors.updateById(userId, updateUser);

return userId;
Expand Down Expand Up @@ -371,6 +374,10 @@ RocketChat.Livechat = {
if (agent && agent.agentId !== servedBy._id) {
RocketChat.models.Rooms.changeAgentByRoomId(room._id, agent);

if (transferData.departmentId) {
RocketChat.models.Rooms.changeDepartmentIdByRoomId(room._id, transferData.departmentId);
}

const subscriptionData = {
rid: room._id,
name: guest.name || guest.username,
Expand Down Expand Up @@ -438,13 +445,15 @@ RocketChat.Livechat = {
agents.forEach((agent) => {
agentIds.push(agent.agentId);
});

RocketChat.models.Rooms.changeDepartmentIdByRoomId(room._id, departmentId);
}

// delete agent and room subscription
RocketChat.models.Subscriptions.removeByRoomId(rid);

// remove user from room
RocketChat.models.Rooms.removeUsernameById(rid, user.username);
// remove agent from room
RocketChat.models.Rooms.removeAgentByRoomId(rid);

// find inquiry corresponding to room
const inquiry = RocketChat.models.LivechatInquiry.findOne({ rid });
Expand Down
8 changes: 8 additions & 0 deletions packages/rocketchat-livechat/server/lib/QueueMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ RocketChat.QueueMethods = {
emailNotifications: 'all',
};

if (guest.department) {
room.departmentId = guest.department;
}

RocketChat.models.Rooms.insert(room);

RocketChat.models.Subscriptions.insert(subscriptionData);
Expand Down Expand Up @@ -138,6 +142,10 @@ RocketChat.QueueMethods = {
waitingResponse: true,
}, roomInfo);

if (guest.department) {
room.departmentId = guest.department;
}

RocketChat.models.LivechatInquiry.insert(inquiry);
RocketChat.models.Rooms.insert(room);

Expand Down
10 changes: 6 additions & 4 deletions packages/rocketchat-livechat/server/methods/getInitialData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'underscore';
import LivechatVisitors from '../models/LivechatVisitors';

Meteor.methods({
'livechat:getInitialData'(visitorToken) {
'livechat:getInitialData'(visitorToken, departmentId) {
const info = {
enabled: null,
title: null,
Expand All @@ -27,7 +27,7 @@ Meteor.methods({
emailFieldRegistrationForm: null,
};

const room = RocketChat.models.Rooms.findOpenByVisitorToken(visitorToken, {
const options = {
fields: {
name: 1,
t: 1,
Expand All @@ -36,9 +36,10 @@ Meteor.methods({
usernames: 1,
v: 1,
servedBy: 1,
departmentId: 1,
},
}).fetch();

};
const room = (departmentId) ? RocketChat.models.Rooms.findOpenByVisitorTokenAndDepartmentId(visitorToken, departmentId, options).fetch() : RocketChat.models.Rooms.findOpenByVisitorToken(visitorToken, options).fetch();
if (room && room.length > 0) {
info.room = room[0];
}
Expand All @@ -48,6 +49,7 @@ Meteor.methods({
name: 1,
username: 1,
visitorEmails: 1,
department: 1,
},
});

Expand Down
16 changes: 15 additions & 1 deletion packages/rocketchat-livechat/server/methods/registerGuest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import LivechatVisitors from '../models/LivechatVisitors';

Meteor.methods({
'livechat:registerGuest'({ token, name, email, department } = {}) {
'livechat:registerGuest'({ token, name, email, department, customFields } = {}) {
const userId = RocketChat.Livechat.registerGuest.call(this, {
token,
name,
Expand All @@ -18,6 +18,7 @@ Meteor.methods({
name: 1,
username: 1,
visitorEmails: 1,
department: 1,
},
});

Expand All @@ -27,6 +28,19 @@ Meteor.methods({
RocketChat.Livechat.saveRoomInfo(room, visitor);
});

if (customFields && customFields instanceof Array) {
customFields.forEach((customField) => {
if (typeof customField !== 'object') {
return;
}

if (!customField.scope || customField.scope !== 'room') {
const { key, value, overwrite } = customField;
LivechatVisitors.updateLivechatDataByToken(token, key, value, overwrite);
}
});
}

return {
userId,
visitor,
Expand Down
36 changes: 36 additions & 0 deletions packages/rocketchat-livechat/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ RocketChat.models.Rooms.findOpenByVisitorToken = function(visitorToken, options)
return this.find(query, options);
};

RocketChat.models.Rooms.findOpenByVisitorTokenAndDepartmentId = function(visitorToken, departmentId, options) {
const query = {
open: true,
'v.token': visitorToken,
departmentId,
};

return this.find(query, options);
};

RocketChat.models.Rooms.findByVisitorToken = function(visitorToken) {
const query = {
'v.token': visitorToken,
Expand Down Expand Up @@ -196,6 +206,19 @@ RocketChat.models.Rooms.changeAgentByRoomId = function(roomId, newAgent) {
this.update(query, update);
};

RocketChat.models.Rooms.changeDepartmentIdByRoomId = function(roomId, departmentId) {
const query = {
_id: roomId,
};
const update = {
$set: {
departmentId,
},
};

this.update(query, update);
};

RocketChat.models.Rooms.saveCRMDataByRoomId = function(roomId, crmData) {
const query = {
_id: roomId,
Expand Down Expand Up @@ -223,3 +246,16 @@ RocketChat.models.Rooms.updateVisitorStatus = function(token, status) {

return this.update(query, update);
};

RocketChat.models.Rooms.removeAgentByRoomId = function(roomId) {
const query = {
_id: roomId,
};
const update = {
$unset: {
servedBy: 1,
},
};

this.update(query, update);
};
1 change: 1 addition & 0 deletions packages/rocketchat-livechat/server/models/indexes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Meteor.startup(function() {
RocketChat.models.Rooms.tryEnsureIndex({ open: 1 }, { sparse: 1 });
RocketChat.models.Rooms.tryEnsureIndex({ departmentId: 1 }, { sparse: 1 });
RocketChat.models.Users.tryEnsureIndex({ 'visitorEmails.address': 1 });
});