From 129b09dd67f6ed626d1b5601e869239984dc953d Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Thu, 17 Mar 2022 08:22:29 -0600 Subject: [PATCH] [IMPROVE] Standarize queue behavior for managers and agents when subscribing (#24837) * Standarize queue behavior for managers and agents when subscribing to department/public queues * Fix import * Remove task from comment --- .../client/lib/stream/queueManager.js | 11 +++++--- app/livechat/imports/server/rest/inquiries.js | 26 +++++++++++++++++++ app/livechat/server/api/lib/inquiries.js | 7 ++--- package-lock.json | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app/livechat/client/lib/stream/queueManager.js b/app/livechat/client/lib/stream/queueManager.js index a63979ff19a..f76e5679d27 100644 --- a/app/livechat/client/lib/stream/queueManager.js +++ b/app/livechat/client/lib/stream/queueManager.js @@ -45,7 +45,7 @@ const updateCollection = (inquiry) => { }; const getInquiriesFromAPI = async () => { - const { inquiries } = await APIClient.v1.get('livechat/inquiries.queued?sort={"ts": 1}'); + const { inquiries } = await APIClient.v1.get('livechat/inquiries.queuedForUser?sort={"ts": 1}'); return inquiries; }; @@ -59,7 +59,7 @@ const appendListenerToDepartment = (departmentId) => { inquiryDataStream.on(`department/${departmentId}`, updateCollection); return () => removeListenerOfDepartment(departmentId); }; -const addListenerForeachDepartment = async (departments = []) => { +const addListenerForeachDepartment = (departments = []) => { const cleanupFunctions = departments.map((department) => appendListenerToDepartment(department)); return () => cleanupFunctions.forEach((cleanup) => cleanup()); }; @@ -87,14 +87,17 @@ const subscribe = async (userId) => { const agentDepartments = (await getAgentsDepartments(userId)).map((department) => department.departmentId); - const cleanUp = agentDepartments.length ? await addListenerForeachDepartment(agentDepartments) : addGlobalListener(); + // Register to all depts + public queue always to match the inquiry list returned by backend + const cleanDepartmentListeners = addListenerForeachDepartment(agentDepartments); + const globalCleanup = addGlobalListener(); updateInquiries(await getInquiriesFromAPI()); return () => { LivechatInquiry.remove({}); removeGlobalListener(); - cleanUp && cleanUp(); + cleanDepartmentListeners && cleanDepartmentListeners(); + globalCleanup && globalCleanup(); departments.clear(); }; }; diff --git a/app/livechat/imports/server/rest/inquiries.js b/app/livechat/imports/server/rest/inquiries.js index 41f6c421ea3..09c92c681a1 100644 --- a/app/livechat/imports/server/rest/inquiries.js +++ b/app/livechat/imports/server/rest/inquiries.js @@ -5,6 +5,7 @@ import { API } from '../../../../api/server'; import { hasPermission } from '../../../../authorization'; import { Users, LivechatDepartment, LivechatInquiry } from '../../../../models'; import { findInquiries, findOneInquiryByRoomId } from '../../../server/api/lib/inquiries'; +import { LivechatInquiryStatus } from '../../../../../definition/IInquiry'; API.v1.addRoute( 'livechat/inquiries.list', @@ -101,6 +102,31 @@ API.v1.addRoute( }, ); +API.v1.addRoute( + 'livechat/inquiries.queuedForUser', + { authRequired: true }, + { + async get() { + const { offset, count } = this.getPaginationItems(); + const { sort } = this.parseJsonQuery(); + const { department } = this.requestParams(); + + return API.v1.success( + await findInquiries({ + userId: this.userId, + filterDepartment: department, + status: LivechatInquiryStatus.QUEUED, + pagination: { + offset, + count, + sort, + }, + }), + ); + }, + }, +); + API.v1.addRoute( 'livechat/inquiries.getOne', { authRequired: true }, diff --git a/app/livechat/server/api/lib/inquiries.js b/app/livechat/server/api/lib/inquiries.js index b83f8631754..2b113433fe0 100644 --- a/app/livechat/server/api/lib/inquiries.js +++ b/app/livechat/server/api/lib/inquiries.js @@ -1,6 +1,5 @@ import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission'; import { LivechatDepartmentAgents, LivechatDepartment, LivechatInquiry } from '../../../../models/server/raw'; -import { hasAnyRoleAsync } from '../../../../authorization/server/functions/hasRole'; const agentDepartments = async (userId) => { const agentDepartments = (await LivechatDepartmentAgents.findByAgentId(userId).toArray()).map(({ departmentId }) => departmentId); @@ -8,10 +7,6 @@ const agentDepartments = async (userId) => { }; const applyDepartmentRestrictions = async (userId, filterDepartment) => { - if (await hasAnyRoleAsync(userId, ['livechat-manager'])) { - return filterDepartment; - } - const allowedDepartments = await agentDepartments(userId); if (allowedDepartments && Array.isArray(allowedDepartments) && allowedDepartments.length > 0) { if (!filterDepartment) { @@ -48,6 +43,8 @@ export async function findInquiries({ userId, department: filterDepartment, stat $and: [{ defaultAgent: { $exists: true } }, { 'defaultAgent.agentId': userId }], }, { ...(department && { department }) }, + // Add _always_ the "public queue" to returned list of inquiries, even if agent already has departments + { department: { $exists: false } }, ], }; diff --git a/package-lock.json b/package-lock.json index e042f196f9f..417fa617b32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24382,7 +24382,7 @@ "dev": true }, "lamejs": { - "version": "git+https://github.com/zhuker/lamejs.git#564612b5b57336238a5920ba4c301b49f7cb2bab", + "version": "git+https://github.com/zhuker/lamejs.git#582bbba6a12f981b984d8fb9e1874499fed85675", "from": "git+https://github.com/zhuker/lamejs.git", "requires": { "use-strict": "1.0.1"