diff --git a/apps/meteor/app/livechat/server/lib/QueueManager.ts b/apps/meteor/app/livechat/server/lib/QueueManager.ts index fb1469bcde8b..2e846bc24d9e 100644 --- a/apps/meteor/app/livechat/server/lib/QueueManager.ts +++ b/apps/meteor/app/livechat/server/lib/QueueManager.ts @@ -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'); @@ -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) { diff --git a/apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.ts b/apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.ts index da78d351250b..cb8503d82e76 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.ts @@ -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'; @@ -123,7 +124,7 @@ const dispatchWaitingQueueStatus = async (department?: string) => { const queue = await LivechatInquiry.getCurrentSortedQueueAsync({ department, - queueSortBy: getInquirySortMechanismSetting(), + queueSortBy: getOmniChatSortQuery(getInquirySortMechanismSetting()), }); if (!queue.length) { @@ -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) { diff --git a/apps/meteor/server/models/raw/LivechatInquiry.ts b/apps/meteor/server/models/raw/LivechatInquiry.ts index ec5ad430b160..de5ebc069949 100644 --- a/apps/meteor/server/models/raw/LivechatInquiry.ts +++ b/apps/meteor/server/models/raw/LivechatInquiry.ts @@ -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 { @@ -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 implements ILivechatInquiryModel { @@ -154,7 +147,10 @@ export class LivechatInquiryRaw extends BaseRaw implemen return updated?.value; } - async findNextAndLock(queueSortBy: OmnichannelSortingMechanismSettingType, department?: string): Promise { + async findNextAndLock( + queueSortBy: FindOptions['sort'], + department?: string, + ): Promise { const date = new Date(); const result = await this.findOneAndUpdate( { @@ -180,7 +176,7 @@ export class LivechatInquiryRaw extends BaseRaw implemen }, }, { - sort: getOmniChatSortQuery(queueSortBy), + sort: queueSortBy, }, ); @@ -212,7 +208,7 @@ export class LivechatInquiryRaw extends BaseRaw implemen }: { inquiryId?: string; department?: string; - queueSortBy: OmnichannelSortingMechanismSettingType; + queueSortBy: FindOptions['sort']; }): Promise<(Pick & { position: number })[]> { const filter: Filter[] = [ { @@ -221,7 +217,7 @@ export class LivechatInquiryRaw extends BaseRaw implemen ...(department && { department }), }, }, - { $sort: getOmniChatSortQuery(queueSortBy) }, + { $sort: queueSortBy }, { $group: { _id: 1, diff --git a/apps/meteor/server/services/omnichannel/queue.ts b/apps/meteor/server/services/omnichannel/queue.ts index 29d48b9f1f6b..ed7420e78d99 100644 --- a/apps/meteor/server/services/omnichannel/queue.ts +++ b/apps/meteor/server/services/omnichannel/queue.ts @@ -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'; @@ -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; diff --git a/packages/model-typings/src/models/ILivechatInquiryModel.ts b/packages/model-typings/src/models/ILivechatInquiryModel.ts index e44ff37b6ed4..b81c37afb95a 100644 --- a/packages/model-typings/src/models/ILivechatInquiryModel.ts +++ b/packages/model-typings/src/models/ILivechatInquiryModel.ts @@ -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'; @@ -21,14 +16,14 @@ export interface ILivechatInquiryModel extends IBaseModel; setDepartmentByInquiryId(inquiryId: string, department: string): Promise; setLastMessageByRoomId(rid: ILivechatInquiryRecord['rid'], message: IMessage): Promise; - findNextAndLock(queueSortBy: OmnichannelSortingMechanismSettingType, department?: string): Promise; + findNextAndLock(queueSortBy: FindOptions['sort'], department?: string): Promise; unlock(inquiryId: string): Promise; unlockAndQueue(inquiryId: string): Promise; unlockAll(): Promise; getCurrentSortedQueueAsync(props: { inquiryId?: string; department?: string; - queueSortBy: OmnichannelSortingMechanismSettingType; + queueSortBy: FindOptions['sort']; }): Promise<(Pick & { position: number })[]>; removeByRoomId(rid: string, options?: DeleteOptions): Promise; getQueuedInquiries(options?: FindOptions): FindCursor;