mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-23 04:22:02 -06:00
refactor: event folder
This commit is contained in:
parent
7b79591e42
commit
df77aadd04
@ -1,3 +1,4 @@
|
||||
import { ChatwootInstanceMixin } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
|
||||
import { RabbitMQInstanceMixin } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
|
||||
import { SQSInstanceMixin } from '@api/integrations/event/sqs/dto/sqs.dto';
|
||||
import { WebhookInstanceMixin } from '@api/integrations/event/webhook/dto/webhook.dto';
|
||||
@ -5,24 +6,6 @@ import { WebsocketInstanceMixin } from '@api/integrations/event/websocket/dto/we
|
||||
|
||||
export type Constructor<T = {}> = new (...args: any[]) => T;
|
||||
|
||||
function ChatwootInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
||||
return class extends Base {
|
||||
chatwootAccountId?: string;
|
||||
chatwootToken?: string;
|
||||
chatwootUrl?: string;
|
||||
chatwootSignMsg?: boolean;
|
||||
chatwootReopenConversation?: boolean;
|
||||
chatwootConversationPending?: boolean;
|
||||
chatwootMergeBrazilContacts?: boolean;
|
||||
chatwootImportContacts?: boolean;
|
||||
chatwootImportMessages?: boolean;
|
||||
chatwootDaysLimitImportMessages?: number;
|
||||
chatwootNameInbox?: string;
|
||||
chatwootOrganization?: string;
|
||||
chatwootLogo?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class IntegrationDto extends WebhookInstanceMixin(
|
||||
WebsocketInstanceMixin(RabbitMQInstanceMixin(SQSInstanceMixin(ChatwootInstanceMixin(class {})))),
|
||||
) {}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
|
||||
export class ChatwootDto {
|
||||
enabled?: boolean;
|
||||
accountId?: string;
|
||||
@ -18,3 +20,21 @@ export class ChatwootDto {
|
||||
logo?: string;
|
||||
ignoreJids?: string[];
|
||||
}
|
||||
|
||||
export function ChatwootInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
||||
return class extends Base {
|
||||
chatwootAccountId?: string;
|
||||
chatwootToken?: string;
|
||||
chatwootUrl?: string;
|
||||
chatwootSignMsg?: boolean;
|
||||
chatwootReopenConversation?: boolean;
|
||||
chatwootConversationPending?: boolean;
|
||||
chatwootMergeBrazilContacts?: boolean;
|
||||
chatwootImportContacts?: boolean;
|
||||
chatwootImportMessages?: boolean;
|
||||
chatwootDaysLimitImportMessages?: number;
|
||||
chatwootNameInbox?: string;
|
||||
chatwootOrganization?: string;
|
||||
chatwootLogo?: string;
|
||||
};
|
||||
}
|
||||
|
@ -15,12 +15,12 @@ export class RabbitmqController extends EventController {
|
||||
super(prismaRepository, waMonitor);
|
||||
}
|
||||
|
||||
public init(): void {
|
||||
public async init(): Promise<void> {
|
||||
if (!configService.get<Rabbitmq>('RABBITMQ')?.ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
new Promise<void>((resolve, reject) => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const uri = configService.get<Rabbitmq>('RABBITMQ').URI;
|
||||
amqp.connect(uri, (error, connection) => {
|
||||
if (error) {
|
||||
@ -101,7 +101,7 @@ export class RabbitmqController extends EventController {
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new NotFoundException('Rabbitmq not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -131,7 +131,7 @@ export class RabbitmqController extends EventController {
|
||||
}
|
||||
|
||||
const instanceRabbitmq = await this.get(instanceName);
|
||||
const rabbitmqLocal = instanceRabbitmq.events;
|
||||
const rabbitmqLocal = instanceRabbitmq?.events;
|
||||
const rabbitmqGlobal = configService.get<Rabbitmq>('RABBITMQ').GLOBAL_ENABLED;
|
||||
const rabbitmqEvents = configService.get<Rabbitmq>('RABBITMQ').EVENTS;
|
||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||
@ -147,7 +147,7 @@ export class RabbitmqController extends EventController {
|
||||
apikey: apiKey,
|
||||
};
|
||||
|
||||
if (instanceRabbitmq.enabled && this.amqpChannel) {
|
||||
if (instanceRabbitmq?.enabled && this.amqpChannel) {
|
||||
if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) {
|
||||
const exchangeName = instanceName ?? 'evolution_exchange';
|
||||
|
||||
|
@ -87,7 +87,7 @@ export class SqsController extends EventController {
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new NotFoundException('Sqs not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -117,10 +117,10 @@ export class SqsController extends EventController {
|
||||
}
|
||||
|
||||
const instanceSqs = await this.get(instanceName);
|
||||
const sqsLocal = instanceSqs.events;
|
||||
const sqsLocal = instanceSqs?.events;
|
||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||
|
||||
if (instanceSqs.enabled) {
|
||||
if (instanceSqs?.enabled) {
|
||||
if (this.sqs) {
|
||||
if (Array.isArray(sqsLocal) && sqsLocal.includes(we)) {
|
||||
const eventFormatted = `${event.replace('.', '_').toLowerCase()}`;
|
||||
|
@ -65,7 +65,7 @@ export class WebhookController extends EventController {
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new NotFoundException('Webhook not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -98,7 +98,7 @@ export class WebhookController extends EventController {
|
||||
|
||||
const instanceWebhook = await this.get(instanceName);
|
||||
const webhookGlobal = configService.get<Webhook>('WEBHOOK');
|
||||
const webhookLocal = instanceWebhook.events;
|
||||
const webhookLocal = instanceWebhook?.events;
|
||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
||||
const enabledLog = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
|
||||
@ -107,7 +107,7 @@ export class WebhookController extends EventController {
|
||||
event,
|
||||
instance: instanceName,
|
||||
data,
|
||||
destination: instanceWebhook.url,
|
||||
destination: instanceWebhook?.url,
|
||||
date_time: dateTime,
|
||||
sender,
|
||||
server_url: serverUrl,
|
||||
@ -117,10 +117,10 @@ export class WebhookController extends EventController {
|
||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||
let baseURL: string;
|
||||
|
||||
if (instanceWebhook.webhookByEvents) {
|
||||
baseURL = `${instanceWebhook.url}/${transformedWe}`;
|
||||
if (instanceWebhook?.webhookByEvents) {
|
||||
baseURL = `${instanceWebhook?.url}/${transformedWe}`;
|
||||
} else {
|
||||
baseURL = instanceWebhook.url;
|
||||
baseURL = instanceWebhook?.url;
|
||||
}
|
||||
|
||||
if (enabledLog) {
|
||||
@ -134,7 +134,7 @@ export class WebhookController extends EventController {
|
||||
}
|
||||
|
||||
try {
|
||||
if (instanceWebhook.enabled && isURL(instanceWebhook.url, { require_tld: false })) {
|
||||
if (instanceWebhook?.enabled && isURL(instanceWebhook.url, { require_tld: false })) {
|
||||
const httpService = axios.create({ baseURL });
|
||||
|
||||
await httpService.post('', webhookData);
|
||||
|
@ -98,7 +98,7 @@ export class WebsocketController extends EventController {
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new NotFoundException('Websocket not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -153,11 +153,11 @@ export class WebsocketController extends EventController {
|
||||
try {
|
||||
const instanceSocket = await this.get(instanceName);
|
||||
|
||||
if (!instanceSocket.enabled) {
|
||||
if (!instanceSocket?.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(instanceSocket.events) && instanceSocket.events.includes(configEv)) {
|
||||
if (Array.isArray(instanceSocket?.events) && instanceSocket?.events.includes(configEv)) {
|
||||
this.socket.of(`/${instanceName}`).emit(event, message);
|
||||
|
||||
if (logEnabled) {
|
||||
|
Loading…
Reference in New Issue
Block a user