Skip to content

Commit

Permalink
refactor: remove external deps from omni models
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Dec 24, 2024
1 parent b31e7f9 commit 444d03f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
33 changes: 30 additions & 3 deletions apps/meteor/app/livechat/server/lib/analytics/departments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LivechatRooms, Messages } from '@rocket.chat/models';

import { settings } from '../../../../settings/server';

type Params = {
start: Date;
end: Date;
Expand Down Expand Up @@ -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,
};
};
Expand All @@ -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,
};
};
10 changes: 6 additions & 4 deletions apps/meteor/server/models/raw/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import type {
} from 'mongodb';

import { BaseRaw } from './BaseRaw';
import { getValue } from '../../../app/settings/server/raw';
import { readSecondaryPreferred } from '../../database/readSecondaryPreferred';

/**
Expand Down Expand Up @@ -220,11 +219,13 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> 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 } };
Expand All @@ -233,7 +234,7 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> 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) },
Expand Down Expand Up @@ -275,12 +276,14 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> 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 } };
Expand All @@ -305,8 +308,7 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> 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],
},
],
},
Expand Down
3 changes: 0 additions & 3 deletions apps/meteor/server/models/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILivechatVisitor> implements ILivechatVisitorsModel {
constructor(db: Db, trash?: Collection<RocketChatRecordDeleted<ILivechatVisitor>>) {
Expand Down Expand Up @@ -130,8 +129,6 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
throw new Error("Can't find Livechat_guest_count setting");
}

void notifyOnSettingChanged(livechatCount.value);

return `guest-${livechatCount.value.value}`;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/model-typings/src/models/ILivechatRoomsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ export interface ILivechatRoomsModel extends IBaseModel<IOmnichannelRoom> {

getQueueMetrics(params: { departmentId: any; agentId: any; includeOfflineAgents: any; options?: any }): any;

findAllNumberOfAbandonedRooms(params: Period & WithDepartment & WithOnlyCount & WithOptions): Promise<any>;
findAllNumberOfAbandonedRooms(
params: Period & WithDepartment & WithOnlyCount & WithOptions & { inactivityTimeout: number },
): Promise<any>;

findPercentageOfAbandonedRooms(params: Period & WithDepartment & WithOnlyCount & WithOptions): Promise<any>;
findPercentageOfAbandonedRooms(
params: Period & WithDepartment & WithOnlyCount & WithOptions & { inactivityTimeout: number },
): Promise<any>;

findAllAverageOfChatDurationTime(params: Period & WithDepartment & WithOnlyCount & WithOptions): any;

Expand Down

0 comments on commit 444d03f

Please sign in to comment.