diff --git a/packages/rocketchat-livechat/server/lib/Livechat.js b/packages/rocketchat-livechat/server/lib/Livechat.js index d4f26ccb8789..8194c80ab2df 100644 --- a/packages/rocketchat-livechat/server/lib/Livechat.js +++ b/packages/rocketchat-livechat/server/lib/Livechat.js @@ -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 }; @@ -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', @@ -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); @@ -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); @@ -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; @@ -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; }, 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 c7800535f61c..0e79f81e37e5 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 */