Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: dispatchInquiryPosition being called multiple times #32767

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import { Random } from '@rocket.chat/random';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { dispatchInquiryPosition } from '../../../../ee/app/livechat-enterprise/server/lib/Helper';
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
import { callbacks } from '../../../../lib/callbacks';
import {
notifyOnLivechatInquiryChangedById,
notifyOnLivechatInquiryChanged,
notifyOnSettingChanged,
} from '../../../lib/server/lib/notifyListener';
import { settings } from '../../../settings/server';
import { createLivechatRoom, createLivechatInquiry } from './Helper';
import { Livechat as LivechatTyped } from './LivechatTyped';
import { RoutingManager } from './RoutingManager';
import { getInquirySortMechanismSetting } from './settings';

const logger = new Logger('QueueManager');

Expand Down Expand Up @@ -52,6 +55,7 @@ export const queueInquiry = async (inquiry: ILivechatInquiryRecord, defaultAgent
// We'll queue these inquiries so when new license is applied, they just start rolling again
// Minimizing disruption
await saveQueueInquiry(inquiry);
await QueueManager.dispatchInquiryPosition(inquiry);
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const dbInquiry = await LivechatInquiry.findOneById(inquiry._id);
Expand All @@ -62,33 +66,48 @@ export const queueInquiry = async (inquiry: ILivechatInquiryRecord, defaultAgent

if (dbInquiry.status === 'ready') {
logger.debug(`Inquiry with id ${inquiry._id} is ready. Delegating to agent ${inquiryAgent?.username}`);
/**
* There is no need to call QueueManager.dispatchInquiryPosition
* because the method is called on afterTakeInquiry anyway
*
*/
return RoutingManager.delegateInquiry(dbInquiry, inquiryAgent, undefined, room);
}

await QueueManager.dispatchInquiryPosition(dbInquiry);
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
};

interface IQueueManager {
requestRoom: <
E extends Record<string, unknown> & {
sla?: string;
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
},
>(params: {
guest: ILivechatVisitor;
rid?: string;
message?: string;
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
};
agent?: SelectedAgent;
extraData?: E;
}) => Promise<IOmnichannelRoom>;
unarchiveRoom: (archivedRoom?: IOmnichannelRoom) => Promise<IOmnichannelRoom>;
}
export const QueueManager = class {
/**
* It should be used when a inquiry is moved to queue(created|enabled|etc)
* mainly used to inform visitors when they value is set for the first time
*
* @param inquiry
*
*/
static async dispatchInquiryPosition(inquiry: ILivechatInquiryRecord) {
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
const { _id, status, department } = inquiry;

if (status !== 'ready') {
throw new Error('inquiry-not-ready');
}

if (settings.get('Omnichannel_calculate_dispatch_service_queue_statistics')) {
const [inq] = await LivechatInquiry.getCurrentSortedQueueAsync({
inquiryId: _id,
department,
queueSortBy: getInquirySortMechanismSetting(),
});

export const QueueManager = new (class implements IQueueManager {
private async checkServiceStatus({ guest, agent }: { guest: Pick<ILivechatVisitor, 'department'>; agent?: SelectedAgent }) {
if (!inq) {
throw new Error('inquiry-not-in-queue');
}

await dispatchInquiryPosition(inq);
}
}

private static async checkServiceStatus({ guest, agent }: { guest: Pick<ILivechatVisitor, 'department'>; agent?: SelectedAgent }) {
if (!agent) {
return LivechatTyped.online(guest.department);
}
Expand All @@ -98,7 +117,7 @@ export const QueueManager = new (class implements IQueueManager {
return users > 0;
}

async requestRoom<
static async requestRoom<
E extends Record<string, unknown> & {
sla?: string;
customFields?: Record<string, unknown>;
Expand Down Expand Up @@ -186,7 +205,7 @@ export const QueueManager = new (class implements IQueueManager {
return newRoom;
}

async unarchiveRoom(archivedRoom?: IOmnichannelRoom) {
static async unarchiveRoom(archivedRoom: IOmnichannelRoom) {
if (!archivedRoom) {
throw new Error('no-room-to-unarchive');
}
Expand Down Expand Up @@ -239,4 +258,4 @@ export const QueueManager = new (class implements IQueueManager {

return room;
}
})();
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { online } from '../../../../../app/livechat/server/api/lib/livechat';
import { allowAgentSkipQueue } from '../../../../../app/livechat/server/lib/Helper';
import { Livechat } from '../../../../../app/livechat/server/lib/LivechatTyped';
import { saveQueueInquiry } from '../../../../../app/livechat/server/lib/QueueManager';
import { getInquirySortMechanismSetting } from '../../../../../app/livechat/server/lib/settings';
import { settings } from '../../../../../app/settings/server';
import { callbacks } from '../../../../../lib/callbacks';
import { dispatchInquiryPosition } from '../lib/Helper';
import { cbLogger } from '../lib/logger';

callbacks.add(
'livechat.beforeRouteChat',
async (inquiry, agent) => {
if (!inquiry) {
return inquiry;
}

// check here if department has fallback before queueing
if (inquiry?.department && !(await online(inquiry.department, true, true))) {
const department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id' | 'fallbackForwardDepartment'>>(
Expand Down Expand Up @@ -56,34 +58,11 @@ callbacks.add(
return inquiry;
}

if (!inquiry) {
return inquiry;
}

const { _id, status, department } = inquiry;

if (status !== 'ready') {
return inquiry;
}

if (agent && (await allowAgentSkipQueue(agent))) {
return inquiry;
}

await saveQueueInquiry(inquiry);

if (settings.get('Omnichannel_calculate_dispatch_service_queue_statistics')) {
const [inq] = await LivechatInquiry.getCurrentSortedQueueAsync({
inquiryId: _id,
department,
queueSortBy: getInquirySortMechanismSetting(),
});
if (inq) {
await dispatchInquiryPosition(inq);
}
}

return LivechatInquiry.findOneById(_id);
},
callbacks.priority.HIGH,
'livechat-before-routing-chat',
Expand Down
Loading