Skip to content

Commit

Permalink
[IMPROVE] Standarize queue behavior for managers and agents when subs…
Browse files Browse the repository at this point in the history
…cribing (#24837)

* Standarize queue behavior for managers and agents when subscribing to department/public queues

* Fix import

* Remove task from comment
  • Loading branch information
KevLehman authored and sampaiodiego committed Mar 21, 2022
1 parent 3df5cd0 commit 59244e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
11 changes: 7 additions & 4 deletions app/livechat/client/lib/stream/queueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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());
};
Expand Down Expand Up @@ -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();
};
};
Expand Down
26 changes: 26 additions & 0 deletions app/livechat/imports/server/rest/inquiries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 },
Expand Down
7 changes: 2 additions & 5 deletions app/livechat/server/api/lib/inquiries.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
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);
return (await LivechatDepartment.find({ _id: { $in: agentDepartments }, enabled: true }).toArray()).map(({ _id }) => _id);
};

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) {
Expand Down Expand Up @@ -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 } },
],
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59244e4

Please sign in to comment.