diff --git a/app/livechat/client/views/app/tabbar/visitorEdit.js b/app/livechat/client/views/app/tabbar/visitorEdit.js index 3a782f39305..741bd9417fc 100644 --- a/app/livechat/client/views/app/tabbar/visitorEdit.js +++ b/app/livechat/client/views/app/tabbar/visitorEdit.js @@ -7,8 +7,8 @@ import { t } from '../../../../../utils'; import { hasRole } from '../../../../../authorization'; import { LivechatVisitor } from '../../../collections/LivechatVisitor'; import { LivechatDepartmentAgents } from '../../../collections/LivechatDepartmentAgents'; -import { LivechatRoom } from '../../../collections/LivechatRoom'; import './visitorEdit.html'; +import { APIClient } from '../../../../../utils/client'; Template.visitorEdit.helpers({ visitor() { @@ -81,9 +81,8 @@ Template.visitorEdit.onCreated(function() { const rid = Template.currentData().roomId; - this.subscribe('livechat:rooms', { _id: rid }); - this.autorun(() => { - const room = LivechatRoom.findOne({ _id: rid }); + this.autorun(async () => { + const { room } = await APIClient.v1.get(`rooms.info?roomId=${ rid }`); this.room.set(room); this.tags.set((room && room.tags) || []); }); diff --git a/app/livechat/client/views/app/tabbar/visitorInfo.js b/app/livechat/client/views/app/tabbar/visitorInfo.js index 074ece84379..c51c8558deb 100644 --- a/app/livechat/client/views/app/tabbar/visitorInfo.js +++ b/app/livechat/client/views/app/tabbar/visitorInfo.js @@ -15,7 +15,6 @@ import { settings } from '../../../../../settings'; import { t, handleError, roomTypes } from '../../../../../utils'; import { hasRole, hasAllPermission, hasAtLeastOnePermission } from '../../../../../authorization'; import { LivechatVisitor } from '../../../collections/LivechatVisitor'; -import { LivechatRoom } from '../../../collections/LivechatRoom'; import './visitorInfo.html'; import { APIClient } from '../../../../../utils/client'; @@ -51,7 +50,7 @@ Template.visitorInfo.helpers({ }, room() { - return LivechatRoom.findOne({ _id: this.rid }); + return Template.instance().room.get(); }, department() { @@ -73,7 +72,7 @@ Template.visitorInfo.helpers({ const data = Template.currentData(); if (data && data.rid) { - const room = LivechatRoom.findOne(data.rid); + const room = Template.instance().room.get(); if (room) { livechatData = _.extend(livechatData, room.livechatData); } @@ -148,7 +147,7 @@ Template.visitorInfo.helpers({ }, roomOpen() { - const room = LivechatRoom.findOne({ _id: this.rid }); + const room = Template.instance().room.get(); const uid = Meteor.userId(); return room && room.open && ((room.servedBy && room.servedBy._id === uid) || hasRole(uid, 'livechat-manager')); }, @@ -259,7 +258,7 @@ Template.visitorInfo.events({ }, () => { Meteor.call('livechat:returnAsInquiry', this.rid, function(error/* , result*/) { if (error) { - console.log(error); + handleError(error); } else { Session.set('openedRoom'); FlowRouter.go('/home'); @@ -284,6 +283,7 @@ Template.visitorInfo.onCreated(function() { this.tags = new ReactiveVar(null); this.routingConfig = new ReactiveVar({}); this.department = new ReactiveVar({}); + this.room = new ReactiveVar({}); Meteor.call('livechat:getCustomFields', (err, customFields) => { if (customFields) { @@ -298,13 +298,20 @@ Template.visitorInfo.onCreated(function() { } }); + const loadRoomData = async (rid) => { + const { room } = await APIClient.v1.get(`rooms.info?roomId=${ rid }`); + this.visitorId.set(room && room.v && room.v._id); + this.departmentId.set(room && room.departmentId); + this.tags.set(room && room.tags); + this.room.set(room); + }; + if (rid) { - this.subscribe('livechat:rooms', { _id: rid }); this.autorun(() => { - 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); + const action = this.action.get(); + if (action === undefined) { + loadRoomData(rid); + } }); this.subscribe('livechat:visitorInfo', { rid }); diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 530b83e7f21..187311bc527 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -449,6 +449,10 @@ export const Livechat = { throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:returnRoomAsInquiry' }); } + if (!room.open) { + throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:returnRoomAsInquiry' }); + } + if (!room.servedBy) { return false; } diff --git a/app/livechat/server/methods/closeRoom.js b/app/livechat/server/methods/closeRoom.js index 14a7e1d3025..f7f2d18942d 100644 --- a/app/livechat/server/methods/closeRoom.js +++ b/app/livechat/server/methods/closeRoom.js @@ -11,6 +11,15 @@ Meteor.methods({ throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'livechat:closeRoom' }); } + const room = LivechatRooms.findOneById(roomId); + if (!room || room.t !== 'l') { + throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:closeRoom' }); + } + + if (!room.open) { + throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:closeRoom' }); + } + const user = Meteor.user(); const subscription = Subscriptions.findOneByRoomIdAndUserId(roomId, user._id, { _id: 1 }); diff --git a/app/livechat/server/methods/returnAsInquiry.js b/app/livechat/server/methods/returnAsInquiry.js index 6dae070a1d9..bb7aa9c8146 100644 --- a/app/livechat/server/methods/returnAsInquiry.js +++ b/app/livechat/server/methods/returnAsInquiry.js @@ -1,12 +1,22 @@ import { Meteor } from 'meteor/meteor'; import { hasPermission } from '../../../authorization'; +import { LivechatRooms } from '../../../models'; import { Livechat } from '../lib/Livechat'; Meteor.methods({ 'livechat:returnAsInquiry'(rid, departmentId) { if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-l-room')) { - throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveDepartment' }); + throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:returnAsInquiry' }); + } + + const room = LivechatRooms.findOneById(rid); + if (!room || room.t !== 'l') { + throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:returnAsInquiry' }); + } + + if (!room.open) { + throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:returnAsInquiry' }); } return Livechat.returnRoomAsInquiry(rid, departmentId); diff --git a/app/livechat/server/methods/transfer.js b/app/livechat/server/methods/transfer.js index a8e155c19b2..352cc149065 100644 --- a/app/livechat/server/methods/transfer.js +++ b/app/livechat/server/methods/transfer.js @@ -18,10 +18,14 @@ Meteor.methods({ }); const room = LivechatRooms.findOneById(transferData.roomId); - if (!room) { + if (!room || room.t !== 'l') { throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:transfer' }); } + if (!room.open) { + throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:transfer' }); + } + const subscription = Subscriptions.findOneByRoomIdAndUserId(room._id, Meteor.userId(), { fields: { _id: 1 } }); if (!subscription && !hasPermission(Meteor.userId(), 'transfer-livechat-guest')) { throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'livechat:transfer' }); diff --git a/app/models/server/raw/LivechatRooms.js b/app/models/server/raw/LivechatRooms.js new file mode 100644 index 00000000000..9b5f657d2aa --- /dev/null +++ b/app/models/server/raw/LivechatRooms.js @@ -0,0 +1,5 @@ +import { BaseRaw } from './BaseRaw'; + +export class LivechatRoomsRaw extends BaseRaw { + +} diff --git a/app/models/server/raw/index.js b/app/models/server/raw/index.js index 12b7ec7f556..1dd7f7bcda0 100644 --- a/app/models/server/raw/index.js +++ b/app/models/server/raw/index.js @@ -14,6 +14,8 @@ import LivechatDepartmentModel from '../models/LivechatDepartment'; import { LivechatDepartmentRaw } from './LivechatDepartment'; import LivechatDepartmentAgentsModel from '../models/LivechatDepartmentAgents'; import { LivechatDepartmentAgentsRaw } from './LivechatDepartmentAgents'; +import LivechatRoomsModel from '../models/LivechatRooms'; +import { LivechatRoomsRaw } from './LivechatRooms'; import MessagesModel from '../models/Messages'; import { MessagesRaw } from './Messages'; @@ -25,4 +27,5 @@ export const Users = new UsersRaw(UsersModel.model.rawCollection()); export const Rooms = new RoomsRaw(RoomsModel.model.rawCollection()); export const LivechatDepartment = new LivechatDepartmentRaw(LivechatDepartmentModel.model.rawCollection()); export const LivechatDepartmentAgents = new LivechatDepartmentAgentsRaw(LivechatDepartmentAgentsModel.model.rawCollection()); +export const LivechatRooms = new LivechatRoomsRaw(LivechatRoomsModel.model.rawCollection()); export const Messages = new MessagesRaw(MessagesModel.model.rawCollection());