[IMPROVE] Replace some livechat:rooms subscriptions (#15532)

pull/15547/head
Marcos Spessatto Defendi 6 years ago committed by Diego Sampaio
parent 1b001879a6
commit b6c00a6532
  1. 7
      app/livechat/client/views/app/tabbar/visitorEdit.js
  2. 27
      app/livechat/client/views/app/tabbar/visitorInfo.js
  3. 4
      app/livechat/server/lib/Livechat.js
  4. 9
      app/livechat/server/methods/closeRoom.js
  5. 12
      app/livechat/server/methods/returnAsInquiry.js
  6. 6
      app/livechat/server/methods/transfer.js
  7. 5
      app/models/server/raw/LivechatRooms.js
  8. 3
      app/models/server/raw/index.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) || []);
});

@ -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 });

@ -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;
}

@ -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 });

@ -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);

@ -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' });

@ -0,0 +1,5 @@
import { BaseRaw } from './BaseRaw';
export class LivechatRoomsRaw extends BaseRaw {
}

@ -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());

Loading…
Cancel
Save