diff --git a/apps/meteor/app/livechat/server/api/lib/users.ts b/apps/meteor/app/livechat/server/api/lib/users.ts index 66d9d091c262..49ac5682a6c0 100644 --- a/apps/meteor/app/livechat/server/api/lib/users.ts +++ b/apps/meteor/app/livechat/server/api/lib/users.ts @@ -24,47 +24,26 @@ async function findUsers({ pagination: { offset: number; count: number; sort: any }; }): Promise<{ users: ILivechatAgent[]; count: number; offset: number; total: number }> { const query: FilterOperators = {}; + const orConditions: FilterOperators['$or'] = []; if (text) { const filterReg = new RegExp(escapeRegExp(text), 'i'); - Object.assign(query, { - $or: [{ username: filterReg }, { name: filterReg }, { 'emails.address': filterReg }], - }); + orConditions.push({ $or: [{ username: filterReg }, { name: filterReg }, { 'emails.address': filterReg }] }); } if (onlyAvailable) { - Object.assign(query, { - statusLivechat: 'available', - }); + query.statusLivechat = 'available'; } if (excludeId) { - Object.assign(query, { - _id: { $ne: excludeId }, - }); + query._id = { $ne: excludeId }; } if (!showIdleAgents) { - const oldOr = query.$or; - - const newOr = [{ status: { $exists: true, $ne: 'offline' }, roles: { $ne: 'bot' } }, { roles: 'bot' }]; + orConditions.push({ $or: [{ status: { $exists: true, $ne: 'offline' }, roles: { $ne: 'bot' } }, { roles: 'bot' }] }); + } - if (oldOr) { - delete query.$or; - Object.assign(query, { - $and: [ - { - $or: oldOr, - }, - { - $or: newOr, - }, - ], - }); - } else { - Object.assign(query, { - $or: newOr, - }); - } + if (orConditions.length) { + query.$and = orConditions; } const [