diff --git a/app/livechat/client/collections/LivechatDepartmentAgents.js b/app/livechat/client/collections/LivechatDepartmentAgents.js deleted file mode 100644 index 85ed9326507..00000000000 --- a/app/livechat/client/collections/LivechatDepartmentAgents.js +++ /dev/null @@ -1,3 +0,0 @@ -import { Mongo } from 'meteor/mongo'; - -export const LivechatDepartmentAgents = new Mongo.Collection('rocketchat_livechat_department_agents'); diff --git a/app/livechat/client/views/app/tabbar/agentEdit.js b/app/livechat/client/views/app/tabbar/agentEdit.js index a8f3e0a075a..e46c66e3760 100644 --- a/app/livechat/client/views/app/tabbar/agentEdit.js +++ b/app/livechat/client/views/app/tabbar/agentEdit.js @@ -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); }); }); diff --git a/app/livechat/client/views/app/tabbar/agentInfo.js b/app/livechat/client/views/app/tabbar/agentInfo.js index 2c02bf856f0..bbec7972726 100644 --- a/app/livechat/client/views/app/tabbar/agentInfo.js +++ b/app/livechat/client/views/app/tabbar/agentInfo.js @@ -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); }; diff --git a/app/livechat/client/views/app/tabbar/visitorEdit.js b/app/livechat/client/views/app/tabbar/visitorEdit.js index 02c1a2a035f..79c9dc1886d 100644 --- a/app/livechat/client/views/app/tabbar/visitorEdit.js +++ b/app/livechat/client/views/app/tabbar/visitorEdit.js @@ -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) => { diff --git a/app/livechat/imports/server/rest/agent.js b/app/livechat/imports/server/rest/agent.js index e235edcfc85..650c79f4542 100644 --- a/app/livechat/imports/server/rest/agent.js +++ b/app/livechat/imports/server/rest/agent.js @@ -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); diff --git a/app/livechat/server/api/lib/agents.js b/app/livechat/server/api/lib/agents.js index badc7e59c64..2ad3ad9a0f2 100644 --- a/app/livechat/server/api/lib/agents.js +++ b/app/livechat/server/api/lib/agents.js @@ -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(), }; } diff --git a/tests/end-to-end/api/livechat/agents.js b/tests/end-to-end/api/livechat/agents.js index d9720cb5d82..965913c4eb9 100644 --- a/tests/end-to-end/api/livechat/agents.js +++ b/tests/end-to-end/api/livechat/agents.js @@ -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); });