From 4be01c439d0a169585e7fb2b2b8c7d152cd4fc10 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 18 Dec 2017 12:10:02 -0200 Subject: [PATCH 1/2] Fix closing livechat inquiry Closes #8399 --- .../server/lib/Livechat.js | 27 ++++++++++++------- .../server/methods/closeRoom.js | 6 ++++- .../server/models/LivechatInquiry.js | 19 +++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/packages/rocketchat-livechat/server/lib/Livechat.js b/packages/rocketchat-livechat/server/lib/Livechat.js index 11d85e9fe84a..81fe1b92d307 100644 --- a/packages/rocketchat-livechat/server/lib/Livechat.js +++ b/packages/rocketchat-livechat/server/lib/Livechat.js @@ -203,14 +203,18 @@ RocketChat.Livechat = { closeRoom({ user, room, comment }) { const now = new Date(); - RocketChat.models.Rooms.closeByRoomId(room._id, { + + const closeData = { user: { _id: user._id, username: user.username }, closedAt: now, chatDuration: (now.getTime() - room.ts) / 1000 - }); + }; + + RocketChat.models.Rooms.closeByRoomId(room._id, closeData); + RocketChat.models.LivechatInquiry.closeByRoomId(room._id, closeData); const message = { t: 'livechat-close', @@ -373,7 +377,7 @@ RocketChat.Livechat = { getLivechatRoomGuestInfo(room) { const visitor = RocketChat.models.Users.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); @@ -398,14 +402,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; @@ -418,10 +429,6 @@ RocketChat.Livechat = { postData.visitor.phone = visitor.phone[0].phoneNumber; } - if (agent.emails && agent.emails.length > 0) { - postData.agent.email = agent.emails[0].address; - } - return postData; }, diff --git a/packages/rocketchat-livechat/server/methods/closeRoom.js b/packages/rocketchat-livechat/server/methods/closeRoom.js index 19e6b9c1cab0..b61350ec9ff9 100644 --- a/packages/rocketchat-livechat/server/methods/closeRoom.js +++ b/packages/rocketchat-livechat/server/methods/closeRoom.js @@ -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' }); } diff --git a/packages/rocketchat-livechat/server/models/LivechatInquiry.js b/packages/rocketchat-livechat/server/models/LivechatInquiry.js index 29439c3a0dbd..3e590bf73d7d 100644 --- a/packages/rocketchat-livechat/server/models/LivechatInquiry.js +++ b/packages/rocketchat-livechat/server/models/LivechatInquiry.js @@ -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 */ From d6533aca20f20955517e4ee84746385626203bb0 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 19 Jan 2018 23:40:11 -0200 Subject: [PATCH 2/2] Fix promp transcript --- packages/rocketchat-livechat/server/lib/Livechat.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-livechat/server/lib/Livechat.js b/packages/rocketchat-livechat/server/lib/Livechat.js index dd22c7c3d552..8194c80ab2df 100644 --- a/packages/rocketchat-livechat/server/lib/Livechat.js +++ b/packages/rocketchat-livechat/server/lib/Livechat.js @@ -248,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);