[IMPROVE] Replace forgotten livechat:departmentAgents subscriptions (#15970)

* Replace forgotten livechat:departmentAgents subscriptions

* Get agent id from room object

* Regression: Continue using the userId to fetch the agent departments.
pull/15503/head
Marcos Spessatto Defendi 6 years ago committed by Renato Becker
parent a4c60b5e32
commit 569279ec2d
  1. 3
      app/livechat/client/collections/LivechatDepartmentAgents.js
  2. 10
      app/livechat/client/views/app/tabbar/agentEdit.js
  3. 10
      app/livechat/client/views/app/tabbar/agentInfo.js
  4. 2
      app/livechat/client/views/app/tabbar/visitorEdit.js
  5. 9
      app/livechat/imports/server/rest/agent.js
  6. 17
      app/livechat/server/api/lib/agents.js
  7. 14
      tests/end-to-end/api/livechat/agents.js

@ -1,3 +0,0 @@
import { Mongo } from 'meteor/mongo';
export const LivechatDepartmentAgents = new Mongo.Collection('rocketchat_livechat_department_agents');

@ -1,12 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import toastr from 'toastr';
import { getCustomFormTemplate } from '../customTemplates/register';
import './agentEdit.html';
import { LivechatDepartmentAgents } from '../../../collections/LivechatDepartmentAgents';
import { hasPermission } from '../../../../../authorization';
import { t, handleError, APIClient } from '../../../../../utils/client';
@ -144,13 +142,9 @@ Template.agentEdit.onCreated(async function() {
}
const { user } = await APIClient.v1.get(`livechat/users/agent/${ agentId }`);
const { departments } = await APIClient.v1.get(`livechat/agents/${ agentId }/departments`);
this.agent.set(user);
Tracker.nonreactive(() => this.subscribe('livechat:departmentAgents', null, agentId, () => {
this.agentDepartments.set(LivechatDepartmentAgents.find({ agentId }).map((deptAgent) => deptAgent.departmentId));
}));
this.agentDepartments.set((departments || []).map((department) => department.departmentId));
this.ready.set(true);
});
});

@ -1,5 +1,4 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
@ -12,7 +11,6 @@ import './agentInfo.html';
import { modal } from '../../../../../ui-utils';
import { t, handleError, APIClient } from '../../../../../utils/client';
import { hasPermission } from '../../../../../authorization';
import { LivechatDepartmentAgents } from '../../../collections/LivechatDepartmentAgents';
const customFieldsTemplate = () => getCustomFormTemplate('livechatAgentInfoForm');
@ -159,13 +157,9 @@ Template.agentInfo.onCreated(async function() {
const loadAgentData = async (agentId) => {
this.ready.set(false);
const { user } = await APIClient.v1.get(`livechat/users/agent/${ agentId }`);
const { departments } = await APIClient.v1.get(`livechat/agents/${ agentId }/departments`);
this.agent.set(user);
// TODO: Need to replace the following subscribe by the REST approach
Tracker.nonreactive(() => this.subscribe('livechat:departmentAgents', null, agentId, () => {
this.agentDepartments.set(LivechatDepartmentAgents.find({ agentId }).map((deptAgent) => deptAgent.departmentId));
}));
this.agentDepartments.set((departments || []).map((department) => department.departmentId));
this.ready.set(true);
};

@ -79,7 +79,7 @@ Template.visitorEdit.onCreated(async function() {
});
const uid = Meteor.userId();
const { departments } = await APIClient.v1.get(`livechat/agent/${ uid }/departments`);
const { departments } = await APIClient.v1.get(`livechat/agents/${ uid }/departments`);
const agentDepartments = departments.map((dept) => dept.departmentId);
this.agentDepartments.set(agentDepartments);
Meteor.call('livechat:getTagsList', (err, tagsList) => {

@ -3,22 +3,15 @@ import { check } from 'meteor/check';
import { API } from '../../../../api';
import { findAgentDepartments } from '../../../server/api/lib/agents';
API.v1.addRoute('livechat/agent/:agentId/departments', { authRequired: true }, {
API.v1.addRoute('livechat/agents/:agentId/departments', { authRequired: true }, {
get() {
check(this.urlParams, {
agentId: String,
});
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();
const departments = Promise.await(findAgentDepartments({
userId: this.userId,
agentId: this.urlParams.agentId,
pagination: {
offset,
count,
sort,
},
}));
return API.v1.success(departments);

@ -1,25 +1,12 @@
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { LivechatDepartmentAgents } from '../../../../models/server/raw';
export async function findAgentDepartments({ userId, agentId, pagination: { offset, count, sort } }) {
export async function findAgentDepartments({ userId, agentId }) {
if (!await hasPermissionAsync(userId, 'view-l-room')) {
throw new Error('error-not-authorized');
}
const cursor = LivechatDepartmentAgents.find({ agentId }, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
});
const total = await cursor.count();
const departments = await cursor.toArray();
return {
departments,
count: departments.length,
offset,
total,
departments: await LivechatDepartmentAgents.find({ agentId }).toArray(),
};
}

@ -95,11 +95,11 @@ describe('LIVECHAT - Agents', function() {
});
});
describe('livechat/agent/:agentId/departments', () => {
describe('livechat/agents/:agentId/departments', () => {
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => {
updatePermission('view-l-room', [])
.then(() => {
request.get(api(`livechat/agent/${ agent._id }/departments`))
request.get(api(`livechat/agents/${ agent._id }/departments`))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
@ -113,16 +113,13 @@ describe('LIVECHAT - Agents', function() {
it('should return an empty array of departments when the agentId is invalid', (done) => {
updatePermission('view-l-room', ['admin'])
.then(() => {
request.get(api('livechat/agent/invalid-id/departments'))
request.get(api('livechat/agents/invalid-id/departments'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('departments').and.to.be.an('array');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
})
.end(done);
});
@ -130,16 +127,13 @@ describe('LIVECHAT - Agents', function() {
it('should return an array of departments when the agentId is valid', (done) => {
updatePermission('view-l-room', ['admin'])
.then(() => {
request.get(api(`livechat/agent/${ agent._id }/departments`))
request.get(api(`livechat/agents/${ agent._id }/departments`))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('departments').and.to.be.an('array');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
res.body.departments.forEach((department) => {
expect(department.agentId).to.be.equal(agent._id);
});

Loading…
Cancel
Save