Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
145 lines
4.4 KiB
TypeScript
145 lines
4.4 KiB
TypeScript
import { Agenda } from '@rocket.chat/agenda';
|
|
import type { IUser } from '@rocket.chat/core-typings';
|
|
import type { MainLogger } from '@rocket.chat/logger';
|
|
import { LivechatRooms, Users } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { MongoInternals } from 'meteor/mongo';
|
|
|
|
import { schedulerLogger } from './logger';
|
|
import { forwardRoomToAgent } from '../../../../server/lib/omnichannel/Helper';
|
|
import { RoutingManager } from '../../../../server/lib/omnichannel/RoutingManager';
|
|
import { returnRoomAsInquiry } from '../../../../server/lib/omnichannel/rooms';
|
|
import { settings } from '../../../../server/settings';
|
|
|
|
const SCHEDULER_NAME = 'omnichannel_scheduler';
|
|
|
|
export class AutoTransferChatSchedulerClass {
|
|
scheduler: Agenda;
|
|
|
|
running: boolean;
|
|
|
|
user: IUser;
|
|
|
|
logger: MainLogger;
|
|
|
|
constructor() {
|
|
this.logger = schedulerLogger.section('AutoTransferChatScheduler');
|
|
}
|
|
|
|
public async init(): Promise<void> {
|
|
if (this.running) {
|
|
return;
|
|
}
|
|
|
|
this.scheduler = new Agenda({
|
|
mongo: (MongoInternals.defaultRemoteCollectionDriver().mongo as any).client.db(),
|
|
db: { collection: SCHEDULER_NAME },
|
|
defaultConcurrency: 1,
|
|
processEvery: process.env.TEST_MODE === 'true' || process.env.TEST_MODE === 'api' ? '3 seconds' : '1 minute',
|
|
});
|
|
|
|
await this.scheduler.start();
|
|
this.running = true;
|
|
this.logger.info('Service started');
|
|
}
|
|
|
|
private async getSchedulerUser(): Promise<IUser & { userType: 'user' }> {
|
|
const user = await Users.findOneById('rocket.cat');
|
|
if (!user) {
|
|
this.logger.error('Error while transferring room: user not found');
|
|
throw new Error('error-no-cat');
|
|
}
|
|
return {
|
|
...user,
|
|
userType: 'user',
|
|
};
|
|
}
|
|
|
|
public async scheduleRoom(roomId: string, timeout: number): Promise<void> {
|
|
this.logger.debug({ msg: 'Scheduling room to be transferred', roomId, timeoutSeconds: timeout });
|
|
await this.unscheduleRoom(roomId);
|
|
|
|
const jobName = `${SCHEDULER_NAME}-${roomId}`;
|
|
const when = new Date();
|
|
when.setSeconds(when.getSeconds() + timeout);
|
|
|
|
this.scheduler.define(jobName, this.executeJob.bind(this));
|
|
await this.scheduler.schedule(when, jobName, { roomId });
|
|
await LivechatRooms.setAutoTransferOngoingById(roomId);
|
|
}
|
|
|
|
public async unscheduleRoom(roomId: string): Promise<void> {
|
|
this.logger.debug({ msg: 'Unscheduling room', roomId });
|
|
const jobName = `${SCHEDULER_NAME}-${roomId}`;
|
|
|
|
await LivechatRooms.unsetAutoTransferOngoingById(roomId);
|
|
await this.scheduler.cancel({ name: jobName });
|
|
}
|
|
|
|
private async transferRoom(roomId: string): Promise<void> {
|
|
this.logger.debug({ msg: 'Transferring room', roomId });
|
|
const room = await LivechatRooms.findOneById(roomId);
|
|
if (!room?.open || !room?.servedBy?._id) {
|
|
throw new Error('Room is not open or is not being served by an agent');
|
|
}
|
|
|
|
const {
|
|
departmentId,
|
|
servedBy: { _id: ignoreAgentId },
|
|
} = room;
|
|
|
|
const timeoutDuration = settings.get<number>('Livechat_auto_transfer_chat_timeout').toString();
|
|
|
|
if (!RoutingManager.getConfig()?.autoAssignAgent) {
|
|
this.logger.debug({ msg: 'Auto-assign agent is disabled, returning room as inquiry', roomId });
|
|
|
|
await returnRoomAsInquiry(room, departmentId, {
|
|
scope: 'autoTransferUnansweredChatsToQueue',
|
|
comment: timeoutDuration,
|
|
transferredBy: await this.getSchedulerUser(),
|
|
});
|
|
return;
|
|
}
|
|
|
|
const agent = await RoutingManager.getNextAgent(departmentId, ignoreAgentId);
|
|
if (!agent) {
|
|
this.logger.error({
|
|
msg: 'No agent found to transfer unanswered room',
|
|
roomId: room._id,
|
|
timeoutSeconds: timeoutDuration,
|
|
});
|
|
return;
|
|
}
|
|
|
|
this.logger.debug({ msg: 'Transferring room to agent', roomId, agentId: agent.agentId });
|
|
|
|
const transferredBy = await this.getSchedulerUser();
|
|
|
|
await forwardRoomToAgent(room, {
|
|
userId: agent.agentId,
|
|
transferredBy,
|
|
transferredTo: agent,
|
|
scope: 'autoTransferUnansweredChatsToAgent',
|
|
comment: timeoutDuration,
|
|
});
|
|
}
|
|
|
|
private async executeJob({ attrs: { data } }: any = {}): Promise<void> {
|
|
const { roomId } = data;
|
|
|
|
try {
|
|
await this.transferRoom(roomId);
|
|
|
|
await Promise.all([LivechatRooms.setAutoTransferredAtById(roomId), this.unscheduleRoom(roomId)]);
|
|
} catch (error) {
|
|
this.logger.error({ msg: 'Error while executing auto-transfer job', schedulerName: SCHEDULER_NAME, roomId, err: error });
|
|
}
|
|
}
|
|
}
|
|
|
|
export const AutoTransferChatScheduler = new AutoTransferChatSchedulerClass();
|
|
|
|
Meteor.startup(() => {
|
|
void AutoTransferChatScheduler.init();
|
|
});
|