From 649fd1468d64416e21ddbba5aaf5b65841edba4c Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 13 Oct 2023 10:10:14 -0600 Subject: [PATCH] new filters --- .../app/livechat/imports/server/rest/users.ts | 4 +- .../app/livechat/server/api/lib/users.ts | 41 ++++++++++++++++++- packages/rest-typings/src/v1/omnichannel.ts | 16 +++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/apps/meteor/app/livechat/imports/server/rest/users.ts b/apps/meteor/app/livechat/imports/server/rest/users.ts index ebd368613ef5..196970583249 100644 --- a/apps/meteor/app/livechat/imports/server/rest/users.ts +++ b/apps/meteor/app/livechat/imports/server/rest/users.ts @@ -38,11 +38,13 @@ API.v1.addRoute( return API.v1.unauthorized(); } - const { onlyAvailable } = this.queryParams; + const { onlyAvailable, excludeId, showIdleAgents } = this.queryParams; return API.v1.success( await findAgents({ text, onlyAvailable, + excludeId, + showIdleAgents, pagination: { offset, count, diff --git a/apps/meteor/app/livechat/server/api/lib/users.ts b/apps/meteor/app/livechat/server/api/lib/users.ts index a697962ca841..2fc947f9ca45 100644 --- a/apps/meteor/app/livechat/server/api/lib/users.ts +++ b/apps/meteor/app/livechat/server/api/lib/users.ts @@ -1,6 +1,7 @@ import type { ILivechatAgent, IRole } from '@rocket.chat/core-typings'; import { Users } from '@rocket.chat/models'; import { escapeRegExp } from '@rocket.chat/string-helpers'; +import type { FilterOperators } from 'mongodb'; /** * @param {IRole['_id']} role the role id @@ -11,14 +12,18 @@ async function findUsers({ role, text, onlyAvailable = false, + excludeId, + showIdleAgents = true, pagination: { offset, count, sort }, }: { role: IRole['_id']; text?: string; onlyAvailable?: boolean; + excludeId?: string; + showIdleAgents?: boolean; pagination: { offset: number; count: number; sort: any }; }): Promise<{ users: ILivechatAgent[]; count: number; offset: number; total: number }> { - const query = {}; + const query: FilterOperators = {}; if (text) { const filterReg = new RegExp(escapeRegExp(text), 'i'); Object.assign(query, { @@ -32,6 +37,34 @@ async function findUsers({ }); } + if (excludeId) { + Object.assign(query, { + _id: { $ne: excludeId }, + }); + } + + if (!showIdleAgents) { + const oldOr = query.$or; + + if (oldOr) { + delete query.$or; + Object.assign(query, { + $and: [ + { + $or: oldOr, + }, + { + $or: [{ status: { $exists: true, $ne: 'offline' }, roles: { $ne: 'bot' } }, { roles: 'bot' }], + }, + ], + }); + } else { + Object.assign(query, { + $or: [{ status: { $exists: true, $ne: 'offline' }, roles: { $ne: 'bot' } }, { roles: 'bot' }], + }); + } + } + const [ { sortedResults, @@ -61,16 +94,22 @@ async function findUsers({ export async function findAgents({ text, onlyAvailable = false, + excludeId, + showIdleAgents = true, pagination: { offset, count, sort }, }: { text?: string; onlyAvailable: boolean; + excludeId?: string; + showIdleAgents?: boolean; pagination: { offset: number; count: number; sort: any }; }): Promise> { return findUsers({ role: 'livechat-agent', text, onlyAvailable, + excludeId, + showIdleAgents, pagination: { offset, count, diff --git a/packages/rest-typings/src/v1/omnichannel.ts b/packages/rest-typings/src/v1/omnichannel.ts index 71fdfbcab559..3adf8b1d6f37 100644 --- a/packages/rest-typings/src/v1/omnichannel.ts +++ b/packages/rest-typings/src/v1/omnichannel.ts @@ -749,7 +749,13 @@ const LivechatDepartmentsByUnitIdSchema = { export const isLivechatDepartmentsByUnitIdProps = ajv.compile(LivechatDepartmentsByUnitIdSchema); -type LivechatUsersManagerGETProps = PaginatedRequest<{ text?: string; fields?: string; onlyAvailable?: boolean }>; +type LivechatUsersManagerGETProps = PaginatedRequest<{ + text?: string; + fields?: string; + onlyAvailable?: boolean; + excludeId?: string; + showIdleAgents?: boolean; +}>; const LivechatUsersManagerGETSchema = { type: 'object', @@ -762,6 +768,14 @@ const LivechatUsersManagerGETSchema = { type: 'string', nullable: true, }, + excludeId: { + type: 'string', + nullable: true, + }, + showIdleAgents: { + type: 'boolean', + nullable: true, + }, count: { type: 'number', nullable: true,