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 all commits
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
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
Loading