refactor: integrations folder

This commit is contained in:
Davidson Gomes
2024-08-20 16:22:02 -03:00
parent 884362e70c
commit 2ec0b842c1
38 changed files with 212 additions and 259 deletions

View File

@@ -0,0 +1,154 @@
import {
rabbitmqController,
sqsController,
webhookController,
websocketController,
} from '@api/integrations/integration.module';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
import { Server } from 'http';
export class EventController {
public prismaRepository: PrismaRepository;
public waMonitor: WAMonitoringService;
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
this.prisma = prismaRepository;
this.monitor = waMonitor;
}
public set prisma(prisma: PrismaRepository) {
this.prismaRepository = prisma;
}
public get prisma() {
return this.prismaRepository;
}
public set monitor(waMonitor: WAMonitoringService) {
this.waMonitor = waMonitor;
}
public get monitor() {
return this.waMonitor;
}
public readonly events = [
'APPLICATION_STARTUP',
'QRCODE_UPDATED',
'MESSAGES_SET',
'MESSAGES_UPSERT',
'MESSAGES_EDITED',
'MESSAGES_UPDATE',
'MESSAGES_DELETE',
'SEND_MESSAGE',
'CONTACTS_SET',
'CONTACTS_UPSERT',
'CONTACTS_UPDATE',
'PRESENCE_UPDATE',
'CHATS_SET',
'CHATS_UPSERT',
'CHATS_UPDATE',
'CHATS_DELETE',
'GROUPS_UPSERT',
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'LABELS_EDIT',
'LABELS_ASSOCIATION',
'CALL',
'TYPEBOT_START',
'TYPEBOT_CHANGE_STATUS',
'REMOVE_INSTANCE',
'LOGOUT_INSTANCE',
];
public init(httpServer: Server): void {
// websocket
websocketController.init(httpServer);
// rabbitmq
rabbitmqController.init();
// sqs
sqsController.init();
}
public async emit({
instanceName,
origin,
event,
data,
serverUrl,
dateTime,
sender,
apiKey,
local,
}: {
instanceName: string;
origin: string;
event: string;
data: Object;
serverUrl: string;
dateTime: string;
sender: string;
apiKey?: string;
local?: boolean;
}): Promise<void> {
const emitData = {
instanceName,
origin,
event,
data,
serverUrl,
dateTime,
sender,
apiKey,
local,
};
// websocket
await websocketController.emit(emitData);
// rabbitmq
await rabbitmqController.emit(emitData);
// sqs
await sqsController.emit(emitData);
// webhook
await webhookController.emit(emitData);
}
public async setInstance(instanceName: string, data: any): Promise<any> {
// websocket
if (data.websocketEnabled)
await websocketController.set(instanceName, {
enabled: true,
events: data.websocketEvents,
});
// rabbitmq
if (data.rabbitmqEnabled)
await rabbitmqController.set(instanceName, {
enabled: true,
events: data.rabbitmqEvents,
});
// sqs
if (data.sqsEnabled)
await sqsController.set(instanceName, {
enabled: true,
events: data.sqsEvents,
});
// webhook
if (data.webhookEnabled)
await webhookController.set(instanceName, {
enabled: true,
events: data.webhookEvents,
url: data.webhookUrl,
webhookBase64: data.webhookBase64,
webhookByEvents: data.webhookByEvents,
});
}
}

View File

@@ -0,0 +1,18 @@
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router';
import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router';
import { WebhookRouter } from '@api/integrations/event/webhook/routes/webhook.router';
import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router';
import { Router } from 'express';
export class EventRouter {
public readonly router: Router;
constructor(configService: any, ...guards: any[]) {
this.router = Router();
this.router.use('/webhook', new WebhookRouter(configService, ...guards).router);
this.router.use('/websocket', new WebsocketRouter(...guards).router);
this.router.use('/rabbitmq', new RabbitmqRouter(...guards).router);
this.router.use('/sqs', new SqsRouter(...guards).router);
}
}

View File

@@ -0,0 +1,4 @@
export * from '@api/integrations/event/rabbitmq/validate/rabbitmq.schema';
export * from '@api/integrations/event/sqs/validate/sqs.schema';
export * from '@api/integrations/event/webhook/validate/webhook.schema';
export * from '@api/integrations/event/websocket/validate/websocket.schema';

View File

@@ -1,4 +1,3 @@
import { EventController } from '@api/controllers/event.controller';
import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
@@ -8,6 +7,8 @@ import { Logger } from '@config/logger.config';
import { NotFoundException } from '@exceptions';
import * as amqp from 'amqplib/callback_api';
import { EventController } from '../../event.controller';
export class RabbitmqController extends EventController {
public amqpChannel: amqp.Channel | null = null;
private readonly logger = new Logger(RabbitmqController.name);

View File

@@ -1,4 +1,4 @@
import { Constructor } from '@api/dto/integration.dto';
import { Constructor } from '@api/integrations/integration.dto';
export class RabbitmqDto {
enabled: boolean;

View File

@@ -1,8 +1,8 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
import { rabbitmqController } from '@api/integrations/integration.module';
import { HttpStatus } from '@api/routes/index.router';
import { rabbitmqController } from '@api/server.module';
import { instanceSchema, rabbitmqSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';

View File

@@ -1,4 +1,3 @@
import { EventController } from '@api/controllers/event.controller';
import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
@@ -8,6 +7,8 @@ import { configService, Log, Sqs } from '@config/env.config';
import { Logger } from '@config/logger.config';
import { NotFoundException } from '@exceptions';
import { EventController } from '../../event.controller';
export class SqsController extends EventController {
private sqs: SQS;
private readonly logger = new Logger(SqsController.name);

View File

@@ -1,4 +1,4 @@
import { Constructor } from '@api/dto/integration.dto';
import { Constructor } from '@api/integrations/integration.dto';
export class SqsDto {
enabled: boolean;

View File

@@ -1,8 +1,8 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto';
import { sqsController } from '@api/integrations/integration.module';
import { HttpStatus } from '@api/routes/index.router';
import { sqsController } from '@api/server.module';
import { instanceSchema, sqsSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';

View File

@@ -1,4 +1,3 @@
import { EventController } from '@api/controllers/event.controller';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
import { wa } from '@api/types/wa.types';
@@ -8,6 +7,7 @@ import { BadRequestException, NotFoundException } from '@exceptions';
import axios from 'axios';
import { isURL } from 'class-validator';
import { EventController } from '../../event.controller';
import { WebhookDto } from '../dto/webhook.dto';
export class WebhookController extends EventController {

View File

@@ -1,4 +1,4 @@
import { Constructor } from '@api/dto/integration.dto';
import { Constructor } from '@api/integrations/integration.dto';
export class WebhookDto {
enabled?: boolean;

View File

@@ -1,8 +1,8 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { webhookController } from '@api/integrations/integration.module';
import { HttpStatus } from '@api/routes/index.router';
import { webhookController } from '@api/server.module';
import { ConfigService } from '@config/env.config';
import { ConfigService, WaBusiness } from '@config/env.config';
import { instanceSchema, webhookSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';
@@ -31,6 +31,17 @@ export class WebhookRouter extends RouterBroker {
});
res.status(HttpStatus.OK).json(response);
})
.get('meta', async (req, res) => {
if (req.query['hub.verify_token'] === configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
res.send(req.query['hub.challenge']);
else res.send('Error, wrong validation token');
})
.post('meta', async (req, res) => {
const { body } = req;
const response = await webhookController.receiveWebhook(body);
return res.status(200).json(response);
});
}

View File

@@ -20,11 +20,14 @@ const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
};
};
export const websocketSchema: JSONSchema7 = {
export const webhookSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
enabled: { type: 'boolean', enum: [true, false] },
enabled: { type: 'boolean' },
url: { type: 'string' },
webhookByEvents: { type: 'boolean' },
webhookBase64: { type: 'boolean' },
events: {
type: 'array',
minItems: 0,
@@ -60,6 +63,6 @@ export const websocketSchema: JSONSchema7 = {
},
},
},
required: ['enabled'],
...isNotEmpty('enabled'),
required: ['enabled', 'url'],
...isNotEmpty('enabled', 'url'),
};

View File

@@ -1,4 +1,3 @@
import { EventController } from '@api/controllers/event.controller';
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
@@ -9,6 +8,8 @@ import { NotFoundException } from '@exceptions';
import { Server } from 'http';
import { Server as SocketIO } from 'socket.io';
import { EventController } from '../../event.controller';
export class WebsocketController extends EventController {
private io: SocketIO;
private corsConfig: Array<any>;

View File

@@ -1,4 +1,4 @@
import { Constructor } from '@api/dto/integration.dto';
import { Constructor } from '@api/integrations/integration.dto';
export class WebsocketDto {
enabled: boolean;

View File

@@ -1,8 +1,8 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
import { websocketController } from '@api/integrations/integration.module';
import { HttpStatus } from '@api/routes/index.router';
import { websocketController } from '@api/server.module';
import { instanceSchema, websocketSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';