From 444d03f4f1871e233f593551e5ade77b8d08f95d Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 10 Dec 2024 11:44:45 -0300 Subject: [PATCH] refactor: remove external deps from omni models --- .../server/lib/analytics/departments.ts | 33 +++++++++++++++++-- .../meteor/server/models/raw/LivechatRooms.ts | 10 +++--- .../server/models/raw/LivechatVisitors.ts | 3 -- .../src/models/ILivechatRoomsModel.ts | 8 +++-- 4 files changed, 42 insertions(+), 12 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/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;