Skip to content

Commit

Permalink
Merge pull request #9164 from RocketChat/livechat-fix-close-inquiries
Browse files Browse the repository at this point in the history
[FIX] Fix closing livechat inquiry
  • Loading branch information
rodrigok committed Jan 20, 2018
2 parents f3abfd8 + d6533ac commit 14754a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
30 changes: 20 additions & 10 deletions packages/rocketchat-livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ RocketChat.Livechat = {
const now = new Date();

const closeData = {
user: {
_id: user._id,
username: user.username
},
closedAt: now,
chatDuration: (now.getTime() - room.ts) / 1000
};
Expand All @@ -234,6 +238,7 @@ RocketChat.Livechat = {
}

RocketChat.models.Rooms.closeByRoomId(room._id, closeData);
RocketChat.models.LivechatInquiry.closeByRoomId(room._id, closeData);

const message = {
t: 'livechat-close',
Expand All @@ -243,8 +248,10 @@ RocketChat.Livechat = {

RocketChat.sendMessage(user, message, room);

RocketChat.models.Subscriptions.hideByRoomIdAndUserId(room._id, room.servedBy._id);
RocketChat.models.Messages.createCommandWithRoomIdAndUser('promptTranscript', room._id, room.servedBy);
if (room.servedBy) {
RocketChat.models.Subscriptions.hideByRoomIdAndUserId(room._id, room.servedBy._id);
}
RocketChat.models.Messages.createCommandWithRoomIdAndUser('promptTranscript', room._id, closeData.user);

Meteor.defer(() => {
RocketChat.callbacks.run('livechat.closeRoom', room);
Expand Down Expand Up @@ -396,7 +403,7 @@ RocketChat.Livechat = {

getLivechatRoomGuestInfo(room) {
const visitor = LivechatVisitors.findOneById(room.v._id);
const agent = RocketChat.models.Users.findOneById(room.servedBy._id);
const agent = RocketChat.models.Users.findOneById(room.servedBy && room.servedBy._id);

const ua = new UAParser();
ua.setUA(visitor.userAgent);
Expand All @@ -421,14 +428,21 @@ RocketChat.Livechat = {
os: ua.getOS().name && (`${ ua.getOS().name } ${ ua.getOS().version }`),
browser: ua.getBrowser().name && (`${ ua.getBrowser().name } ${ ua.getBrowser().version }`),
customFields: visitor.livechatData
},
agent: {
}
};

if (agent) {
postData.agent = {
_id: agent._id,
username: agent.username,
name: agent.name,
email: null
};

if (agent.emails && agent.emails.length > 0) {
postData.agent.email = agent.emails[0].address;
}
};
}

if (room.crmData) {
postData.crmData = room.crmData;
Expand All @@ -441,10 +455,6 @@ RocketChat.Livechat = {
postData.visitor.phone = visitor.phone;
}

if (agent.emails && agent.emails.length > 0) {
postData.agent.email = agent.emails;
}

return postData;
},

Expand Down
6 changes: 5 additions & 1 deletion packages/rocketchat-livechat/server/methods/closeRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ Meteor.methods({

const room = RocketChat.models.Rooms.findOneById(roomId);

if (!room || room.t !== 'l') {
throw new Meteor.Error('room-not-found', 'Room not found', { method: 'livechat:closeRoom' });
}

const user = Meteor.user();

if (room.usernames.indexOf(user.username) === -1 && !RocketChat.authz.hasPermission(Meteor.userId(), 'close-others-livechat-room')) {
if ((!room.usernames || room.usernames.indexOf(user.username) === -1) && !RocketChat.authz.hasPermission(Meteor.userId(), 'close-others-livechat-room')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'livechat:closeRoom' });
}

Expand Down
19 changes: 19 additions & 0 deletions packages/rocketchat-livechat/server/models/LivechatInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ class LivechatInquiry extends RocketChat.models._Base {
});
}

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

/*
* mark inquiry as open
*/
Expand Down

0 comments on commit 14754a7

Please sign in to comment.