Skip to content

Commit

Permalink
refactor: dispatchInquiryPosition being called multiple times (#32767)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jul 11, 2024
1 parent 1251442 commit 4aa6d58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
36 changes: 10 additions & 26 deletions apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,17 @@ 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);
}
};

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

0 comments on commit 4aa6d58

Please sign in to comment.