diff --git a/src/api/controllers/event.controller.ts b/src/api/controllers/event.controller.ts new file mode 100644 index 00000000..45c389c5 --- /dev/null +++ b/src/api/controllers/event.controller.ts @@ -0,0 +1,95 @@ +import { PrismaRepository } from '@api/repository/repository.service'; +import { websocketController } from '@api/server.module'; +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); + } + + public async emit({ + instanceName, + origin, + event, + data, + }: { + instanceName: string; + origin: string; + event: string; + data: Object; + }): Promise { + // websocket + await websocketController.emit({ + instanceName, + origin, + event, + data, + }); + } + + public async set(instanceName: string, data: any): Promise { + // websocket + await websocketController.set(instanceName, data); + } + + public async get(instanceName: string): Promise { + // websocket + await websocketController.get(instanceName); + } +} diff --git a/src/api/integrations/event/websocket/controllers/websocket.controller.ts b/src/api/integrations/event/websocket/controllers/websocket.controller.ts index 037b0bc2..563a7302 100644 --- a/src/api/integrations/event/websocket/controllers/websocket.controller.ts +++ b/src/api/integrations/event/websocket/controllers/websocket.controller.ts @@ -1,3 +1,4 @@ +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'; @@ -8,45 +9,13 @@ import { NotFoundException } from '@exceptions'; import { Server } from 'http'; import { Server as SocketIO } from 'socket.io'; -export class WebsocketController { +export class WebsocketController extends EventController { private io: SocketIO; - private prismaRepository: PrismaRepository; - private waMonitor: WAMonitoringService; private corsConfig: Array; - private readonly logger = new Logger('SocketStartupService'); - 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', - ]; + private readonly logger = new Logger(WebsocketController.name); constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) { - this.prisma = prismaRepository; - this.monitor = waMonitor; + super(prismaRepository, waMonitor); this.cors = configService.get('CORS').ORIGIN; } @@ -72,22 +41,6 @@ export class WebsocketController { this.logger.info('Socket.io initialized'); } - private set prisma(prisma: PrismaRepository) { - this.prismaRepository = prisma; - } - - private get prisma() { - return this.prismaRepository; - } - - private set monitor(waMonitor: WAMonitoringService) { - this.waMonitor = waMonitor; - } - - private get monitor() { - return this.waMonitor; - } - private set cors(cors: Array) { this.corsConfig = cors; } diff --git a/src/api/routes/chatbot.router.ts b/src/api/routes/chatbot.router.ts new file mode 100644 index 00000000..2bada257 --- /dev/null +++ b/src/api/routes/chatbot.router.ts @@ -0,0 +1,18 @@ +import { ChatwootRouter } from '@api/integrations/chatbot/chatwoot/routes/chatwoot.router'; +import { DifyRouter } from '@api/integrations/chatbot/dify/routes/dify.router'; +import { OpenaiRouter } from '@api/integrations/chatbot/openai/routes/openai.router'; +import { TypebotRouter } from '@api/integrations/chatbot/typebot/routes/typebot.router'; +import { Router } from 'express'; + +export class ChatbotRouter { + public readonly router: Router; + + constructor(...guards: any[]) { + this.router = Router(); + + this.router.use('/chatwoot', new ChatwootRouter(...guards).router); + this.router.use('/typebot', new TypebotRouter(...guards).router); + this.router.use('/openai', new OpenaiRouter(...guards).router); + this.router.use('/dify', new DifyRouter(...guards).router); + } +} diff --git a/src/api/routes/event.router.ts b/src/api/routes/event.router.ts new file mode 100644 index 00000000..80da82db --- /dev/null +++ b/src/api/routes/event.router.ts @@ -0,0 +1,19 @@ +import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router'; +import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router'; +import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router'; +import { Router } from 'express'; + +import { WebhookRouter } from './webhook.router'; + +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); + } +} diff --git a/src/api/routes/index.router.ts b/src/api/routes/index.router.ts index b56d4dac..a8df0ac3 100644 --- a/src/api/routes/index.router.ts +++ b/src/api/routes/index.router.ts @@ -1,14 +1,6 @@ import { authGuard } from '@api/guards/auth.guard'; import { instanceExistsGuard, instanceLoggedGuard } from '@api/guards/instance.guard'; import Telemetry from '@api/guards/telemetry.guard'; -import { ChatwootRouter } from '@api/integrations/chatbot/chatwoot/routes/chatwoot.router'; -import { DifyRouter } from '@api/integrations/chatbot/dify/routes/dify.router'; -import { OpenaiRouter } from '@api/integrations/chatbot/openai/routes/openai.router'; -import { TypebotRouter } from '@api/integrations/chatbot/typebot/routes/typebot.router'; -import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router'; -import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router'; -import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router'; -import { S3Router } from '@api/integrations/storage/s3/routes/s3.router'; import { webhookController } from '@api/server.module'; import { configService, WaBusiness } from '@config/env.config'; import { Router } from 'express'; @@ -17,15 +9,17 @@ import mime from 'mime'; import path from 'path'; import { ChatRouter } from './chat.router'; +import { ChatbotRouter } from './chatbot.router'; +import { EventRouter } from './event.router'; import { GroupRouter } from './group.router'; import { InstanceRouter } from './instance.router'; import { LabelRouter } from './label.router'; import { ProxyRouter } from './proxy.router'; import { MessageRouter } from './sendMessage.router'; import { SettingsRouter } from './settings.router'; +import { StorageRouter } from './storage.router'; import { TemplateRouter } from './template.router'; import { ViewsRouter } from './view.router'; -import { WebhookRouter } from './webhook.router'; enum HttpStatus { OK = 200, @@ -87,19 +81,10 @@ router .use('/message', new MessageRouter(...guards).router) .use('/chat', new ChatRouter(...guards).router) .use('/group', new GroupRouter(...guards).router) - .use('/webhook', new WebhookRouter(configService, ...guards).router) .use('/template', new TemplateRouter(configService, ...guards).router) - .use('/chatwoot', new ChatwootRouter(...guards).router) .use('/settings', new SettingsRouter(...guards).router) - .use('/websocket', new WebsocketRouter(...guards).router) - .use('/rabbitmq', new RabbitmqRouter(...guards).router) - .use('/sqs', new SqsRouter(...guards).router) - .use('/typebot', new TypebotRouter(...guards).router) .use('/proxy', new ProxyRouter(...guards).router) .use('/label', new LabelRouter(...guards).router) - .use('/s3', new S3Router(...guards).router) - .use('/openai', new OpenaiRouter(...guards).router) - .use('/dify', new DifyRouter(...guards).router) .get('/webhook/meta', async (req, res) => { if (req.query['hub.verify_token'] === configService.get('WA_BUSINESS').TOKEN_WEBHOOK) res.send(req.query['hub.challenge']); @@ -110,6 +95,9 @@ router const response = await webhookController.receiveWebhook(body); return res.status(200).json(response); - }); + }) + .use('', new EventRouter(configService, ...guards).router) + .use('', new ChatbotRouter(...guards).router) + .use('', new StorageRouter(...guards).router); export { HttpStatus, router }; diff --git a/src/api/routes/storage.router.ts b/src/api/routes/storage.router.ts new file mode 100644 index 00000000..7bbcb837 --- /dev/null +++ b/src/api/routes/storage.router.ts @@ -0,0 +1,12 @@ +import { S3Router } from '@api/integrations/storage/s3/routes/s3.router'; +import { Router } from 'express'; + +export class StorageRouter { + public readonly router: Router; + + constructor(...guards: any[]) { + this.router = Router(); + + this.router.use('/s3', new S3Router(...guards).router); + } +} diff --git a/src/api/server.module.ts b/src/api/server.module.ts index 3cdd5cb3..3e5fff38 100644 --- a/src/api/server.module.ts +++ b/src/api/server.module.ts @@ -4,6 +4,7 @@ import { eventEmitter } from '@config/event.config'; import { Logger } from '@config/logger.config'; import { ChatController } from './controllers/chat.controller'; +import { EventController } from './controllers/event.controller'; import { GroupController } from './controllers/group.controller'; import { InstanceController } from './controllers/instance.controller'; import { LabelController } from './controllers/label.controller'; @@ -18,15 +19,15 @@ import { DifyController } from './integrations/chatbot/dify/controllers/dify.con import { DifyService } from './integrations/chatbot/dify/services/dify.service'; import { OpenaiController } from './integrations/chatbot/openai/controllers/openai.controller'; import { OpenaiService } from './integrations/chatbot/openai/services/openai.service'; -import { RabbitmqController } from './integrations/event/rabbitmq/controllers/rabbitmq.controller'; -import { RabbitmqService } from './integrations/event/rabbitmq/services/rabbitmq.service'; -import { S3Controller } from './integrations/storage/s3/controllers/s3.controller'; -import { S3Service } from './integrations/storage/s3/services/s3.service'; -import { SqsController } from './integrations/event/sqs/controllers/sqs.controller'; -import { SqsService } from './integrations/event/sqs/services/sqs.service'; import { TypebotController } from './integrations/chatbot/typebot/controllers/typebot.controller'; import { TypebotService } from './integrations/chatbot/typebot/services/typebot.service'; +import { RabbitmqController } from './integrations/event/rabbitmq/controllers/rabbitmq.controller'; +import { RabbitmqService } from './integrations/event/rabbitmq/services/rabbitmq.service'; +import { SqsController } from './integrations/event/sqs/controllers/sqs.controller'; +import { SqsService } from './integrations/event/sqs/services/sqs.service'; import { WebsocketController } from './integrations/event/websocket/controllers/websocket.controller'; +import { S3Controller } from './integrations/storage/s3/controllers/s3.controller'; +import { S3Service } from './integrations/storage/s3/services/s3.service'; import { ProviderFiles } from './provider/sessions'; import { PrismaRepository } from './repository/repository.service'; import { AuthService } from './services/auth.service'; @@ -84,6 +85,8 @@ export const webhookController = new WebhookController(webhookService, waMonitor const templateService = new TemplateService(waMonitor, prismaRepository, configService); export const templateController = new TemplateController(templateService); +export const eventController = new EventController(prismaRepository, waMonitor); + export const websocketController = new WebsocketController(prismaRepository, waMonitor); const proxyService = new ProxyService(waMonitor); diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 7de460d1..b3291b92 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -6,13 +6,13 @@ import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto import { ChatwootService } from '@api/integrations/chatbot/chatwoot/services/chatwoot.service'; import { DifyService } from '@api/integrations/chatbot/dify/services/dify.service'; import { OpenaiService } from '@api/integrations/chatbot/openai/services/openai.service'; +import { TypebotService } from '@api/integrations/chatbot/typebot/services/typebot.service'; import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto'; import { getAMQP, removeQueues } from '@api/integrations/event/rabbitmq/libs/amqp.server'; import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto'; import { getSQS, removeQueues as removeQueuesSQS } from '@api/integrations/event/sqs/libs/sqs.server'; -import { TypebotService } from '@api/integrations/chatbot/typebot/services/typebot.service'; import { PrismaRepository, Query } from '@api/repository/repository.service'; -import { waMonitor, websocketController } from '@api/server.module'; +import { eventController, waMonitor } from '@api/server.module'; import { Events, wa } from '@api/types/wa.types'; import { Auth, Chatwoot, ConfigService, HttpServer, Log, Rabbitmq, Sqs, Webhook } from '@config/env.config'; import { Logger } from '@config/logger.config'; @@ -821,7 +821,7 @@ export class ChannelStartupService { } } - await websocketController.emit({ + await eventController.emit({ instanceName: this.instance.name, origin: ChannelStartupService.name, event, diff --git a/src/main.ts b/src/main.ts index 49523101..27da0bbe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import { initSQS } from '@api/integrations/event/sqs/libs/sqs.server'; import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; import { HttpStatus, router } from '@api/routes/index.router'; -import { waMonitor, websocketController } from '@api/server.module'; +import { eventController, waMonitor } from '@api/server.module'; import { Auth, configService, Cors, HttpServer, ProviderSession, Rabbitmq, Sqs, Webhook } from '@config/env.config'; import { onUnexpectedError } from '@config/error.config'; import { Logger } from '@config/logger.config'; @@ -141,7 +141,7 @@ async function bootstrap() { ServerUP.app = app; const server = ServerUP[httpServer.TYPE]; - websocketController.init(server); + eventController.init(server); server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));