mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-23 17:08:44 -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 { RabbitMQInstanceMixin } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
|
||||||
import { SQSInstanceMixin } from '@api/integrations/event/sqs/dto/sqs.dto';
|
import { SQSInstanceMixin } from '@api/integrations/event/sqs/dto/sqs.dto';
|
||||||
import { WebhookInstanceMixin } from '@api/integrations/event/webhook/dto/webhook.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;
|
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(
|
export class IntegrationDto extends WebhookInstanceMixin(
|
||||||
WebsocketInstanceMixin(RabbitMQInstanceMixin(SQSInstanceMixin(ChatwootInstanceMixin(class {})))),
|
WebsocketInstanceMixin(RabbitMQInstanceMixin(SQSInstanceMixin(ChatwootInstanceMixin(class {})))),
|
||||||
) {}
|
) {}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { Constructor } from '@api/dto/integration.dto';
|
||||||
|
|
||||||
export class ChatwootDto {
|
export class ChatwootDto {
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
@ -18,3 +20,21 @@ export class ChatwootDto {
|
|||||||
logo?: string;
|
logo?: string;
|
||||||
ignoreJids?: 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);
|
super(prismaRepository, waMonitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(): void {
|
public async init(): Promise<void> {
|
||||||
if (!configService.get<Rabbitmq>('RABBITMQ')?.ENABLED) {
|
if (!configService.get<Rabbitmq>('RABBITMQ')?.ENABLED) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new Promise<void>((resolve, reject) => {
|
await new Promise<void>((resolve, reject) => {
|
||||||
const uri = configService.get<Rabbitmq>('RABBITMQ').URI;
|
const uri = configService.get<Rabbitmq>('RABBITMQ').URI;
|
||||||
amqp.connect(uri, (error, connection) => {
|
amqp.connect(uri, (error, connection) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -101,7 +101,7 @@ export class RabbitmqController extends EventController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new NotFoundException('Rabbitmq not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -131,7 +131,7 @@ export class RabbitmqController extends EventController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const instanceRabbitmq = await this.get(instanceName);
|
const instanceRabbitmq = await this.get(instanceName);
|
||||||
const rabbitmqLocal = instanceRabbitmq.events;
|
const rabbitmqLocal = instanceRabbitmq?.events;
|
||||||
const rabbitmqGlobal = configService.get<Rabbitmq>('RABBITMQ').GLOBAL_ENABLED;
|
const rabbitmqGlobal = configService.get<Rabbitmq>('RABBITMQ').GLOBAL_ENABLED;
|
||||||
const rabbitmqEvents = configService.get<Rabbitmq>('RABBITMQ').EVENTS;
|
const rabbitmqEvents = configService.get<Rabbitmq>('RABBITMQ').EVENTS;
|
||||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||||
@ -147,7 +147,7 @@ export class RabbitmqController extends EventController {
|
|||||||
apikey: apiKey,
|
apikey: apiKey,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (instanceRabbitmq.enabled && this.amqpChannel) {
|
if (instanceRabbitmq?.enabled && this.amqpChannel) {
|
||||||
if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) {
|
if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) {
|
||||||
const exchangeName = instanceName ?? 'evolution_exchange';
|
const exchangeName = instanceName ?? 'evolution_exchange';
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ export class SqsController extends EventController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new NotFoundException('Sqs not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -117,10 +117,10 @@ export class SqsController extends EventController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const instanceSqs = await this.get(instanceName);
|
const instanceSqs = await this.get(instanceName);
|
||||||
const sqsLocal = instanceSqs.events;
|
const sqsLocal = instanceSqs?.events;
|
||||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||||
|
|
||||||
if (instanceSqs.enabled) {
|
if (instanceSqs?.enabled) {
|
||||||
if (this.sqs) {
|
if (this.sqs) {
|
||||||
if (Array.isArray(sqsLocal) && sqsLocal.includes(we)) {
|
if (Array.isArray(sqsLocal) && sqsLocal.includes(we)) {
|
||||||
const eventFormatted = `${event.replace('.', '_').toLowerCase()}`;
|
const eventFormatted = `${event.replace('.', '_').toLowerCase()}`;
|
||||||
|
@ -65,7 +65,7 @@ export class WebhookController extends EventController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new NotFoundException('Webhook not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -98,7 +98,7 @@ export class WebhookController extends EventController {
|
|||||||
|
|
||||||
const instanceWebhook = await this.get(instanceName);
|
const instanceWebhook = await this.get(instanceName);
|
||||||
const webhookGlobal = configService.get<Webhook>('WEBHOOK');
|
const webhookGlobal = configService.get<Webhook>('WEBHOOK');
|
||||||
const webhookLocal = instanceWebhook.events;
|
const webhookLocal = instanceWebhook?.events;
|
||||||
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||||
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
||||||
const enabledLog = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
|
const enabledLog = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
|
||||||
@ -107,7 +107,7 @@ export class WebhookController extends EventController {
|
|||||||
event,
|
event,
|
||||||
instance: instanceName,
|
instance: instanceName,
|
||||||
data,
|
data,
|
||||||
destination: instanceWebhook.url,
|
destination: instanceWebhook?.url,
|
||||||
date_time: dateTime,
|
date_time: dateTime,
|
||||||
sender,
|
sender,
|
||||||
server_url: serverUrl,
|
server_url: serverUrl,
|
||||||
@ -117,10 +117,10 @@ export class WebhookController extends EventController {
|
|||||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||||
let baseURL: string;
|
let baseURL: string;
|
||||||
|
|
||||||
if (instanceWebhook.webhookByEvents) {
|
if (instanceWebhook?.webhookByEvents) {
|
||||||
baseURL = `${instanceWebhook.url}/${transformedWe}`;
|
baseURL = `${instanceWebhook?.url}/${transformedWe}`;
|
||||||
} else {
|
} else {
|
||||||
baseURL = instanceWebhook.url;
|
baseURL = instanceWebhook?.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enabledLog) {
|
if (enabledLog) {
|
||||||
@ -134,7 +134,7 @@ export class WebhookController extends EventController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (instanceWebhook.enabled && isURL(instanceWebhook.url, { require_tld: false })) {
|
if (instanceWebhook?.enabled && isURL(instanceWebhook.url, { require_tld: false })) {
|
||||||
const httpService = axios.create({ baseURL });
|
const httpService = axios.create({ baseURL });
|
||||||
|
|
||||||
await httpService.post('', webhookData);
|
await httpService.post('', webhookData);
|
||||||
|
@ -98,7 +98,7 @@ export class WebsocketController extends EventController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new NotFoundException('Websocket not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -153,11 +153,11 @@ export class WebsocketController extends EventController {
|
|||||||
try {
|
try {
|
||||||
const instanceSocket = await this.get(instanceName);
|
const instanceSocket = await this.get(instanceName);
|
||||||
|
|
||||||
if (!instanceSocket.enabled) {
|
if (!instanceSocket?.enabled) {
|
||||||
return;
|
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);
|
this.socket.of(`/${instanceName}`).emit(event, message);
|
||||||
|
|
||||||
if (logEnabled) {
|
if (logEnabled) {
|
||||||
|
Loading…
Reference in New Issue
Block a user