Spaces:
Runtime error
Runtime error
File size: 2,994 Bytes
4327358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import { InjectQueue } from '@nestjs/bullmq';
import { Injectable } from '@nestjs/common';
import { EventName } from '@waha/apps/chatwoot/client/types';
import { InboxData } from '@waha/apps/chatwoot/consumers/types';
import { Queue } from 'bullmq';
import { QueueName } from '../consumers/QueueName';
/**
* Service for managing ChatWoot queues for inbox events
* This service is used to centralize queue configuration and job options
*/
@Injectable()
export class ChatWootQueueService {
constructor(
@InjectQueue(QueueName.INBOX_MESSAGE_CREATED)
private readonly messageCreatedQueue: Queue,
@InjectQueue(QueueName.INBOX_MESSAGE_UPDATED)
private readonly messageUpdatedQueue: Queue,
@InjectQueue(QueueName.INBOX_MESSAGE_DELETED)
private readonly messageDeletedQueue: Queue,
@InjectQueue(QueueName.INBOX_COMMANDS)
private readonly commandsQueue: Queue,
) {}
/**
* Generic method to add a job to a queue
* @param queue The queue to add the job to
* @param name The name of the job
* @param data The data for the job
* @returns The result of adding the job to the queue
*/
async add(queue: Queue, name: string, data: any): Promise<any> {
return await queue.add(name, data);
}
/**
* Get the specific queue for an event
* @param event The event to get the queue for
* @returns The queue for the event, or null if there is no specific queue
*/
private getQueueForEvent(event: string): Queue | null {
switch (event) {
case EventName.MESSAGE_CREATED:
return this.messageCreatedQueue;
case EventName.MESSAGE_UPDATED:
return this.messageUpdatedQueue;
case 'message_deleted':
return this.messageDeletedQueue;
case 'commands':
return this.commandsQueue;
default:
return null;
}
}
/**
* Add a job to the message created queue
*/
async addMessageCreatedJob(data: InboxData): Promise<any> {
return await this.add(
this.messageCreatedQueue,
EventName.MESSAGE_CREATED,
data,
);
}
/**
* Add a job to the message updated queue
*/
async addMessageUpdatedJob(data: InboxData): Promise<any> {
return await this.add(
this.messageUpdatedQueue,
EventName.MESSAGE_UPDATED,
data,
);
}
/**
* Add a job to the message deleted queue
*/
async addMessageDeletedJob(data: InboxData): Promise<any> {
return await this.add(this.messageDeletedQueue, 'message_deleted', data);
}
/**
* Add a job to the commands queue
*/
async addCommandsJob(event: string, data: InboxData): Promise<any> {
return await this.add(this.commandsQueue, event, data);
}
/**
* Add a job to the appropriate queue based on the event
*/
async addJobToQueue(event: string, data: InboxData): Promise<any> {
const queue = this.getQueueForEvent(event);
if (queue) {
return await this.add(queue, event, data);
}
return { ignored: true, event };
}
}
|