Skip to content

Commit

Permalink
refactor: remove getOmniChatSortQuery dependency from livechat inquir…
Browse files Browse the repository at this point in the history
…y model (#34491)

Co-authored-by: Guilherme Gazzo <[email protected]>
  • Loading branch information
sampaiodiego and ggazzo authored Dec 24, 2024
1 parent d37433b commit e5600bb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '../../../lib/server/lib/notifyListener';
import { settings } from '../../../settings/server';
import { i18n } from '../../../utils/lib/i18n';
import { getOmniChatSortQuery } from '../../lib/inquiries';

const logger = new Logger('QueueManager');

Expand Down Expand Up @@ -336,7 +337,7 @@ export class QueueManager {
const [inq] = await LivechatInquiry.getCurrentSortedQueueAsync({
inquiryId: inquiry._id,
department: room.departmentId,
queueSortBy: getInquirySortMechanismSetting(),
queueSortBy: getOmniChatSortQuery(getInquirySortMechanismSetting()),
});

if (inq) {
Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { OmnichannelQueueInactivityMonitor } from './QueueInactivityMonitor';
import { updateInquiryQueueSla } from './SlaHelper';
import { memoizeDebounce } from './debounceByParams';
import { logger } from './logger';
import { getOmniChatSortQuery } from '../../../../../app/livechat/lib/inquiries';
import { getInquirySortMechanismSetting } from '../../../../../app/livechat/server/lib/settings';
import { settings } from '../../../../../app/settings/server';
import { callbacks } from '../../../../../lib/callbacks';
Expand Down Expand Up @@ -123,7 +124,7 @@ const dispatchWaitingQueueStatus = async (department?: string) => {

const queue = await LivechatInquiry.getCurrentSortedQueueAsync({
department,
queueSortBy: getInquirySortMechanismSetting(),
queueSortBy: getOmniChatSortQuery(getInquirySortMechanismSetting()),
});

if (!queue.length) {
Expand Down Expand Up @@ -262,7 +263,7 @@ export const getLivechatQueueInfo = async (room?: IOmnichannelRoom) => {
const [inq] = await LivechatInquiry.getCurrentSortedQueueAsync({
inquiryId: _id,
department,
queueSortBy: getInquirySortMechanismSetting(),
queueSortBy: getOmniChatSortQuery(getInquirySortMechanismSetting()),
});

if (!inq) {
Expand Down
20 changes: 8 additions & 12 deletions apps/meteor/server/models/raw/LivechatInquiry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
ILivechatInquiryRecord,
IMessage,
RocketChatRecordDeleted,
OmnichannelSortingMechanismSettingType,
ILivechatPriority,
} from '@rocket.chat/core-typings';
import type { ILivechatInquiryRecord, IMessage, RocketChatRecordDeleted, ILivechatPriority } from '@rocket.chat/core-typings';
import { LivechatInquiryStatus } from '@rocket.chat/core-typings';
import type { ILivechatInquiryModel } from '@rocket.chat/model-typings';
import type {
Expand All @@ -24,7 +18,6 @@ import type {
} from 'mongodb';

import { BaseRaw } from './BaseRaw';
import { getOmniChatSortQuery } from '../../../app/livechat/lib/inquiries';
import { readSecondaryPreferred } from '../../database/readSecondaryPreferred';

export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implements ILivechatInquiryModel {
Expand Down Expand Up @@ -154,7 +147,10 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
return updated?.value;
}

async findNextAndLock(queueSortBy: OmnichannelSortingMechanismSettingType, department?: string): Promise<ILivechatInquiryRecord | null> {
async findNextAndLock(
queueSortBy: FindOptions<ILivechatInquiryRecord>['sort'],
department?: string,
): Promise<ILivechatInquiryRecord | null> {
const date = new Date();
const result = await this.findOneAndUpdate(
{
Expand All @@ -180,7 +176,7 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
},
},
{
sort: getOmniChatSortQuery(queueSortBy),
sort: queueSortBy,
},
);

Expand Down Expand Up @@ -212,7 +208,7 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
}: {
inquiryId?: string;
department?: string;
queueSortBy: OmnichannelSortingMechanismSettingType;
queueSortBy: FindOptions<ILivechatInquiryRecord>['sort'];
}): Promise<(Pick<ILivechatInquiryRecord, '_id' | 'rid' | 'name' | 'ts' | 'status' | 'department'> & { position: number })[]> {
const filter: Filter<ILivechatInquiryRecord>[] = [
{
Expand All @@ -221,7 +217,7 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
...(department && { department }),
},
},
{ $sort: getOmniChatSortQuery(queueSortBy) },
{ $sort: queueSortBy },
{
$group: {
_id: 1,
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/server/services/omnichannel/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { License } from '@rocket.chat/license';
import { LivechatInquiry, LivechatRooms } from '@rocket.chat/models';

import { queueLogger } from './logger';
import { getOmniChatSortQuery } from '../../../app/livechat/lib/inquiries';
import { dispatchAgentDelegated } from '../../../app/livechat/server/lib/Helper';
import { RoutingManager } from '../../../app/livechat/server/lib/RoutingManager';
import { getInquirySortMechanismSetting } from '../../../app/livechat/server/lib/settings';
Expand Down Expand Up @@ -112,7 +113,7 @@ export class OmnichannelQueue implements IOmnichannelQueue {
private async checkQueue(queue: string | undefined) {
queueLogger.debug(`Processing items for queue ${queue || 'Public'}`);
try {
const nextInquiry = await LivechatInquiry.findNextAndLock(getInquirySortMechanismSetting(), queue);
const nextInquiry = await LivechatInquiry.findNextAndLock(getOmniChatSortQuery(getInquirySortMechanismSetting()), queue);
if (!nextInquiry) {
queueLogger.debug(`No more items for queue ${queue || 'Public'}`);
return;
Expand Down
11 changes: 3 additions & 8 deletions packages/model-typings/src/models/ILivechatInquiryModel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
IMessage,
ILivechatInquiryRecord,
LivechatInquiryStatus,
OmnichannelSortingMechanismSettingType,
} from '@rocket.chat/core-typings';
import type { IMessage, ILivechatInquiryRecord, LivechatInquiryStatus } from '@rocket.chat/core-typings';
import type { FindOptions, DistinctOptions, Document, UpdateResult, DeleteResult, FindCursor, DeleteOptions } from 'mongodb';

import type { IBaseModel } from './IBaseModel';
Expand All @@ -21,14 +16,14 @@ export interface ILivechatInquiryModel extends IBaseModel<ILivechatInquiryRecord
getDistinctQueuedDepartments(options: DistinctOptions): Promise<(string | undefined)[]>;
setDepartmentByInquiryId(inquiryId: string, department: string): Promise<ILivechatInquiryRecord | null>;
setLastMessageByRoomId(rid: ILivechatInquiryRecord['rid'], message: IMessage): Promise<ILivechatInquiryRecord | null>;
findNextAndLock(queueSortBy: OmnichannelSortingMechanismSettingType, department?: string): Promise<ILivechatInquiryRecord | null>;
findNextAndLock(queueSortBy: FindOptions<ILivechatInquiryRecord>['sort'], department?: string): Promise<ILivechatInquiryRecord | null>;
unlock(inquiryId: string): Promise<UpdateResult>;
unlockAndQueue(inquiryId: string): Promise<UpdateResult>;
unlockAll(): Promise<UpdateResult | Document>;
getCurrentSortedQueueAsync(props: {
inquiryId?: string;
department?: string;
queueSortBy: OmnichannelSortingMechanismSettingType;
queueSortBy: FindOptions<ILivechatInquiryRecord>['sort'];
}): Promise<(Pick<ILivechatInquiryRecord, '_id' | 'rid' | 'name' | 'ts' | 'status' | 'department'> & { position: number })[]>;
removeByRoomId(rid: string, options?: DeleteOptions): Promise<DeleteResult>;
getQueuedInquiries(options?: FindOptions<ILivechatInquiryRecord>): FindCursor<ILivechatInquiryRecord>;
Expand Down

0 comments on commit e5600bb

Please sign in to comment.