From cc381e18b7a27b0eed4d46f1ea956903b919617c Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 24 Dec 2024 14:35:07 -0300 Subject: [PATCH] refactor: remove external deps from omni models (#34502) --- .../server/lib/analytics/departments.ts | 33 +++++++++++++++++-- .../app/livechat/server/lib/departmentsLib.ts | 4 +++ .../server/models/raw/LivechatDepartment.ts | 2 -- .../meteor/server/models/raw/LivechatRooms.ts | 10 +++--- .../server/models/raw/LivechatVisitors.ts | 3 -- .../src/models/ILivechatRoomsModel.ts | 8 +++-- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/analytics/departments.ts b/apps/meteor/app/livechat/server/lib/analytics/departments.ts index 6d44752f16cf..f505c6f17f9d 100644 --- a/apps/meteor/app/livechat/server/lib/analytics/departments.ts +++ b/apps/meteor/app/livechat/server/lib/analytics/departments.ts @@ -1,5 +1,7 @@ import { LivechatRooms, Messages } from '@rocket.chat/models'; +import { settings } from '../../../../settings/server'; + type Params = { start: Date; end: Date; @@ -139,9 +141,25 @@ export const findAllNumberOfAbandonedRoomsAsync = async ({ start, end, departmen if (!start || !end) { throw new Error('"start" and "end" must be provided'); } - const total = await (await LivechatRooms.findAllNumberOfAbandonedRooms({ start, end, departmentId, onlyCount: true })).toArray(); + const total = await ( + await LivechatRooms.findAllNumberOfAbandonedRooms({ + start, + end, + departmentId, + inactivityTimeout: settings.get('Livechat_visitor_inactivity_timeout'), + onlyCount: true, + }) + ).toArray(); return { - departments: await (await LivechatRooms.findAllNumberOfAbandonedRooms({ start, end, departmentId, options })).toArray(), + departments: await ( + await LivechatRooms.findAllNumberOfAbandonedRooms({ + start, + end, + departmentId, + inactivityTimeout: settings.get('Livechat_visitor_inactivity_timeout'), + options, + }) + ).toArray(), total: total.length ? total[0].total : 0, }; }; @@ -155,11 +173,20 @@ export const findPercentageOfAbandonedRoomsAsync = async ({ start, end, departme start, end, departmentId, + inactivityTimeout: settings.get('Livechat_visitor_inactivity_timeout'), onlyCount: true, }) ).toArray(); return { - departments: await (await LivechatRooms.findPercentageOfAbandonedRooms({ start, end, departmentId, options })).toArray(), + departments: await ( + await LivechatRooms.findPercentageOfAbandonedRooms({ + start, + end, + departmentId, + inactivityTimeout: settings.get('Livechat_visitor_inactivity_timeout'), + options, + }) + ).toArray(), total: total.length ? total[0].total : 0, }; }; diff --git a/apps/meteor/app/livechat/server/lib/departmentsLib.ts b/apps/meteor/app/livechat/server/lib/departmentsLib.ts index fdc85105ab07..7dec370768f0 100644 --- a/apps/meteor/app/livechat/server/lib/departmentsLib.ts +++ b/apps/meteor/app/livechat/server/lib/departmentsLib.ts @@ -126,6 +126,10 @@ export async function saveDepartment( await updateDepartmentAgents(departmentDB._id, departmentAgents, departmentDB.enabled); } + if (department?.enabled !== departmentData.enabled) { + void notifyOnLivechatDepartmentAgentChangedByDepartmentId(departmentDB._id, department ? 'updated' : 'inserted'); + } + // Disable event if (department?.enabled && !departmentDB?.enabled) { await callbacks.run('livechat.afterDepartmentDisabled', departmentDB); diff --git a/apps/meteor/server/models/raw/LivechatDepartment.ts b/apps/meteor/server/models/raw/LivechatDepartment.ts index 5ab3ab52be09..7e318ad33935 100644 --- a/apps/meteor/server/models/raw/LivechatDepartment.ts +++ b/apps/meteor/server/models/raw/LivechatDepartment.ts @@ -17,7 +17,6 @@ import type { } from 'mongodb'; import { BaseRaw } from './BaseRaw'; -import { notifyOnLivechatDepartmentAgentChangedByDepartmentId } from '../../../app/lib/server/lib/notifyListener'; export class LivechatDepartmentRaw extends BaseRaw implements ILivechatDepartmentModel { constructor(db: Db, trash?: Collection>) { @@ -260,7 +259,6 @@ export class LivechatDepartmentRaw extends BaseRaw implemen if (current?.enabled !== data.enabled) { await LivechatDepartmentAgents.setDepartmentEnabledByDepartmentId(_id, data.enabled); - void notifyOnLivechatDepartmentAgentChangedByDepartmentId(_id, current ? 'updated' : 'inserted'); } const latestDept = await this.findOneById(_id); diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index de18a8ec3d22..e1a4d31a2a38 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -33,7 +33,6 @@ import type { } from 'mongodb'; import { BaseRaw } from './BaseRaw'; -import { getValue } from '../../../app/settings/server/raw'; import { readSecondaryPreferred } from '../../database/readSecondaryPreferred'; /** @@ -220,11 +219,13 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive start, end, departmentId, + inactivityTimeout, onlyCount = false, options = {}, }: { start: Date; end: Date; + inactivityTimeout: number; departmentId?: string; onlyCount?: boolean; options?: { offset?: number; count?: number; sort?: { [k: string]: number } }; @@ -233,7 +234,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive $match: { 't': 'l', 'metrics.visitorInactivity': { - $gte: await getValue('Livechat_visitor_inactivity_timeout'), + $gte: inactivityTimeout, }, 'ts': { $gte: new Date(start) }, 'closedAt': { $lte: new Date(end) }, @@ -275,12 +276,14 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive async findPercentageOfAbandonedRooms({ start, end, + inactivityTimeout, departmentId, onlyCount = false, options = {}, }: { start: Date; end: Date; + inactivityTimeout: number; departmentId?: string; onlyCount?: boolean; options?: { offset?: number; count?: number; sort?: { [k: string]: number } }; @@ -305,8 +308,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive $and: [ { $ifNull: ['$metrics.visitorInactivity', false] }, { - // TODO: move these calls to outside model - $gte: ['$metrics.visitorInactivity', await getValue('Livechat_visitor_inactivity_timeout')], + $gte: ['$metrics.visitorInactivity', inactivityTimeout], }, ], }, diff --git a/apps/meteor/server/models/raw/LivechatVisitors.ts b/apps/meteor/server/models/raw/LivechatVisitors.ts index 9f8a28e41f84..27e46808bea9 100644 --- a/apps/meteor/server/models/raw/LivechatVisitors.ts +++ b/apps/meteor/server/models/raw/LivechatVisitors.ts @@ -20,7 +20,6 @@ import type { import { ObjectId } from 'mongodb'; import { BaseRaw } from './BaseRaw'; -import { notifyOnSettingChanged } from '../../../app/lib/server/lib/notifyListener'; export class LivechatVisitorsRaw extends BaseRaw implements ILivechatVisitorsModel { constructor(db: Db, trash?: Collection>) { @@ -130,8 +129,6 @@ export class LivechatVisitorsRaw extends BaseRaw implements IL throw new Error("Can't find Livechat_guest_count setting"); } - void notifyOnSettingChanged(livechatCount.value); - return `guest-${livechatCount.value.value}`; } diff --git a/packages/model-typings/src/models/ILivechatRoomsModel.ts b/packages/model-typings/src/models/ILivechatRoomsModel.ts index 00dea51969c7..4c575d02e10f 100644 --- a/packages/model-typings/src/models/ILivechatRoomsModel.ts +++ b/packages/model-typings/src/models/ILivechatRoomsModel.ts @@ -38,9 +38,13 @@ export interface ILivechatRoomsModel extends IBaseModel { getQueueMetrics(params: { departmentId: any; agentId: any; includeOfflineAgents: any; options?: any }): any; - findAllNumberOfAbandonedRooms(params: Period & WithDepartment & WithOnlyCount & WithOptions): Promise; + findAllNumberOfAbandonedRooms( + params: Period & WithDepartment & WithOnlyCount & WithOptions & { inactivityTimeout: number }, + ): Promise; - findPercentageOfAbandonedRooms(params: Period & WithDepartment & WithOnlyCount & WithOptions): Promise; + findPercentageOfAbandonedRooms( + params: Period & WithDepartment & WithOnlyCount & WithOptions & { inactivityTimeout: number }, + ): Promise; findAllAverageOfChatDurationTime(params: Period & WithDepartment & WithOnlyCount & WithOptions): any;