From 2ec0b842c19e89b2af75f013cac689bf612d702f Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 20 Aug 2024 16:22:02 -0300 Subject: [PATCH] refactor: integrations folder --- src/api/dto/instance.dto.ts | 3 +- .../baileys/whatsapp.baileys.service.ts | 41 ++--------- .../business/whatsapp.business.service.ts | 36 +++------- .../chatbot/chatbot.controller.ts | 71 +++++++++++++++++++ .../chatbot}/chatbot.router.ts | 0 .../integrations/chatbot}/chatbot.schema.ts | 0 .../chatbot/chatwoot/dto/chatwoot.dto.ts | 2 +- .../dify/controllers/dify.controller.ts | 15 ++++ .../chatbot/dify/routes/dify.router.ts | 2 +- .../openai/controllers/openai.controller.ts | 16 +++++ .../chatbot/openai/routes/openai.router.ts | 2 +- .../typebot/controllers/typebot.controller.ts | 15 ++++ .../chatbot/typebot/routes/typebot.router.ts | 2 +- .../event}/event.controller.ts | 7 +- .../event}/event.router.ts | 0 src/api/integrations/event/event.schema.ts | 4 ++ .../controllers/rabbitmq.controller.ts | 3 +- .../event/rabbitmq/dto/rabbitmq.dto.ts | 2 +- .../event/rabbitmq/routes/rabbitmq.router.ts | 2 +- .../event/sqs/controllers/sqs.controller.ts | 3 +- src/api/integrations/event/sqs/dto/sqs.dto.ts | 2 +- .../event/sqs/routes/sqs.router.ts | 2 +- .../webhook/controllers/webhook.controller.ts | 2 +- .../event/webhook/dto/webhook.dto.ts | 2 +- .../event/webhook/routes/webhook.router.ts | 15 +++- .../event/webhook}/validate/webhook.schema.ts | 0 .../webhook/validate/websocket.schema.ts | 65 ----------------- .../controllers/websocket.controller.ts | 3 +- .../event/websocket/dto/websocket.dto.ts | 2 +- .../websocket/routes/websocket.router.ts | 2 +- .../{dto => integrations}/integration.dto.ts | 0 src/api/integrations/integration.module.ts | 29 ++++++++ .../storage}/storage.router.ts | 0 src/api/routes/index.router.ts | 20 ++---- src/api/server.module.ts | 28 +------- src/validate/event.schema.ts | 4 -- src/validate/validate.schema.ts | 4 +- src/validate/websocket.schema.ts | 65 ----------------- 38 files changed, 212 insertions(+), 259 deletions(-) create mode 100644 src/api/integrations/chatbot/chatbot.controller.ts rename src/api/{routes => integrations/chatbot}/chatbot.router.ts (100%) rename src/{validate => api/integrations/chatbot}/chatbot.schema.ts (100%) rename src/api/{controllers => integrations/event}/event.controller.ts (95%) rename src/api/{routes => integrations/event}/event.router.ts (100%) create mode 100644 src/api/integrations/event/event.schema.ts rename src/{ => api/integrations/event/webhook}/validate/webhook.schema.ts (100%) delete mode 100644 src/api/integrations/event/webhook/validate/websocket.schema.ts rename src/api/{dto => integrations}/integration.dto.ts (100%) create mode 100644 src/api/integrations/integration.module.ts rename src/api/{routes => integrations/storage}/storage.router.ts (100%) delete mode 100644 src/validate/event.schema.ts delete mode 100644 src/validate/websocket.schema.ts diff --git a/src/api/dto/instance.dto.ts b/src/api/dto/instance.dto.ts index cb31f4c5..81fd7132 100644 --- a/src/api/dto/instance.dto.ts +++ b/src/api/dto/instance.dto.ts @@ -1,7 +1,6 @@ +import { IntegrationDto } from '@api/integrations/integration.dto'; import { WAPresence } from 'baileys'; -import { IntegrationDto } from './integration.dto'; - export class InstanceDto extends IntegrationDto { instanceName: string; instanceId?: string; diff --git a/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts index 7be72ae8..533045b6 100644 --- a/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts @@ -48,7 +48,7 @@ import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoo import * as s3Service from '@api/integrations/storage/s3/libs/minio.server'; import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; -import { waMonitor } from '@api/server.module'; +import { chatbotController, waMonitor } from '@api/server.module'; import { CacheService } from '@api/services/cache.service'; import { ChannelStartupService } from '@api/services/channel.service'; import { Events, MessageSubtype, TypeMediaMessage, wa } from '@api/types/wa.types'; @@ -1223,39 +1223,12 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); - if (this.configService.get('TYPEBOT').ENABLED) { - if (type === 'notify') { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - } - - if (this.configService.get('OPENAI').ENABLED) { - if (type === 'notify') { - if (messageRaw.messageType !== 'reactionMessage') - await this.openaiService.sendOpenai( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw.pushName, - messageRaw, - ); - } - } - - if (this.configService.get('DIFY').ENABLED) { - if (type === 'notify') { - if (messageRaw.messageType !== 'reactionMessage') - await this.difyService.sendDify( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - } + await chatbotController.emit({ + instance: { instanceName: this.instance.name, instanceId: this.instanceId }, + remoteJid: messageRaw.key.remoteJid, + msg: messageRaw, + pushName: messageRaw.pushName, + }); const contact = await this.prismaRepository.contact.findFirst({ where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId }, diff --git a/src/api/integrations/channel/whatsapp/business/whatsapp.business.service.ts b/src/api/integrations/channel/whatsapp/business/whatsapp.business.service.ts index 885708d9..2207ff29 100644 --- a/src/api/integrations/channel/whatsapp/business/whatsapp.business.service.ts +++ b/src/api/integrations/channel/whatsapp/business/whatsapp.business.service.ts @@ -16,6 +16,7 @@ import { import * as s3Service from '@api/integrations/storage/s3/libs/minio.server'; import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; +import { chatbotController } from '@api/server.module'; import { CacheService } from '@api/services/cache.service'; import { ChannelStartupService } from '@api/services/channel.service'; import { Events, wa } from '@api/types/wa.types'; @@ -481,6 +482,13 @@ export class BusinessStartupService extends ChannelStartupService { this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); + await chatbotController.emit({ + instance: { instanceName: this.instance.name, instanceId: this.instanceId }, + remoteJid: messageRaw.key.remoteJid, + msg: messageRaw, + pushName: messageRaw.pushName, + }); + if (this.configService.get('CHATWOOT').ENABLED && this.localChatwoot.enabled) { const chatwootSentMessage = await this.chatwootService.eventWhatsapp( Events.MESSAGES_UPSERT, @@ -495,34 +503,6 @@ export class BusinessStartupService extends ChannelStartupService { } } - if (this.configService.get('TYPEBOT').ENABLED) { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - - if (this.configService.get('OPENAI').ENABLED) { - if (messageRaw.messageType !== 'reactionMessage') - await this.openaiService.sendOpenai( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - pushName, - messageRaw, - ); - } - - if (this.configService.get('DIFY').ENABLED) { - if (messageRaw.messageType !== 'reactionMessage') - await this.difyService.sendDify( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - await this.prismaRepository.message.create({ data: messageRaw, }); diff --git a/src/api/integrations/chatbot/chatbot.controller.ts b/src/api/integrations/chatbot/chatbot.controller.ts new file mode 100644 index 00000000..3cf9aef0 --- /dev/null +++ b/src/api/integrations/chatbot/chatbot.controller.ts @@ -0,0 +1,71 @@ +import { InstanceDto } from '@api/dto/instance.dto'; +import { + difyController, + openaiController, + typebotController, + websocketController, +} from '@api/integrations/integration.module'; +import { PrismaRepository } from '@api/repository/repository.service'; +import { WAMonitoringService } from '@api/services/monitor.service'; + +export class ChatbotController { + 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 async emit({ + instance, + remoteJid, + msg, + pushName, + }: { + instance: InstanceDto; + remoteJid: string; + msg: any; + pushName?: string; + }): Promise { + const emitData = { + instance, + remoteJid, + msg, + pushName, + }; + // typebot + await typebotController.emit(emitData); + + // openai + await openaiController.emit(emitData); + + // dify + await difyController.emit(emitData); + } + + public async setInstance(instanceName: string, data: any): Promise { + // chatwoot + if (data.websocketEnabled) + await websocketController.set(instanceName, { + enabled: true, + events: data.websocketEvents, + }); + } +} diff --git a/src/api/routes/chatbot.router.ts b/src/api/integrations/chatbot/chatbot.router.ts similarity index 100% rename from src/api/routes/chatbot.router.ts rename to src/api/integrations/chatbot/chatbot.router.ts diff --git a/src/validate/chatbot.schema.ts b/src/api/integrations/chatbot/chatbot.schema.ts similarity index 100% rename from src/validate/chatbot.schema.ts rename to src/api/integrations/chatbot/chatbot.schema.ts diff --git a/src/api/integrations/chatbot/chatwoot/dto/chatwoot.dto.ts b/src/api/integrations/chatbot/chatwoot/dto/chatwoot.dto.ts index 2ab11215..c4f90ca0 100644 --- a/src/api/integrations/chatbot/chatwoot/dto/chatwoot.dto.ts +++ b/src/api/integrations/chatbot/chatwoot/dto/chatwoot.dto.ts @@ -1,4 +1,4 @@ -import { Constructor } from '@api/dto/integration.dto'; +import { Constructor } from '@api/integrations/integration.dto'; export class ChatwootDto { enabled?: boolean; diff --git a/src/api/integrations/chatbot/dify/controllers/dify.controller.ts b/src/api/integrations/chatbot/dify/controllers/dify.controller.ts index dd19127f..79972473 100644 --- a/src/api/integrations/chatbot/dify/controllers/dify.controller.ts +++ b/src/api/integrations/chatbot/dify/controllers/dify.controller.ts @@ -66,4 +66,19 @@ export class DifyController { return this.difyService.ignoreJid(instance, data); } + + public async emit({ + instance, + remoteJid, + msg, + }: { + instance: InstanceDto; + remoteJid: string; + msg: any; + pushName?: string; + }) { + if (!configService.get('DIFY').ENABLED) return; + + await this.difyService.sendDify(instance, remoteJid, msg); + } } diff --git a/src/api/integrations/chatbot/dify/routes/dify.router.ts b/src/api/integrations/chatbot/dify/routes/dify.router.ts index 017c6171..942a8cc0 100644 --- a/src/api/integrations/chatbot/dify/routes/dify.router.ts +++ b/src/api/integrations/chatbot/dify/routes/dify.router.ts @@ -1,8 +1,8 @@ import { RouterBroker } from '@api/abstract/abstract.router'; import { InstanceDto } from '@api/dto/instance.dto'; import { DifyDto, DifyIgnoreJidDto, DifySettingDto } from '@api/integrations/chatbot/dify/dto/dify.dto'; +import { difyController } from '@api/integrations/integration.module'; import { HttpStatus } from '@api/routes/index.router'; -import { difyController } from '@api/server.module'; import { difyIgnoreJidSchema, difySchema, diff --git a/src/api/integrations/chatbot/openai/controllers/openai.controller.ts b/src/api/integrations/chatbot/openai/controllers/openai.controller.ts index a9e0ffc7..70595884 100644 --- a/src/api/integrations/chatbot/openai/controllers/openai.controller.ts +++ b/src/api/integrations/chatbot/openai/controllers/openai.controller.ts @@ -90,4 +90,20 @@ export class OpenaiController { return this.openaiService.ignoreJid(instance, data); } + + public async emit({ + instance, + remoteJid, + msg, + pushName, + }: { + instance: InstanceDto; + remoteJid: string; + msg: any; + pushName?: string; + }) { + if (!configService.get('OPENAI').ENABLED) return; + + await this.openaiService.sendOpenai(instance, remoteJid, pushName, msg); + } } diff --git a/src/api/integrations/chatbot/openai/routes/openai.router.ts b/src/api/integrations/chatbot/openai/routes/openai.router.ts index 15b468c4..ce316623 100644 --- a/src/api/integrations/chatbot/openai/routes/openai.router.ts +++ b/src/api/integrations/chatbot/openai/routes/openai.router.ts @@ -6,8 +6,8 @@ import { OpenaiIgnoreJidDto, OpenaiSettingDto, } from '@api/integrations/chatbot/openai/dto/openai.dto'; +import { openaiController } from '@api/integrations/integration.module'; import { HttpStatus } from '@api/routes/index.router'; -import { openaiController } from '@api/server.module'; import { instanceSchema, openaiCredsSchema, diff --git a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts index 35d35eb2..79827cfc 100644 --- a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts +++ b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts @@ -72,4 +72,19 @@ export class TypebotController { return this.typebotService.ignoreJid(instance, data); } + + public async emit({ + instance, + remoteJid, + msg, + }: { + instance: InstanceDto; + remoteJid: string; + msg: any; + pushName?: string; + }) { + if (!configService.get('TYPEBOT').ENABLED) return; + + await this.typebotService.sendTypebot(instance, remoteJid, msg); + } } diff --git a/src/api/integrations/chatbot/typebot/routes/typebot.router.ts b/src/api/integrations/chatbot/typebot/routes/typebot.router.ts index c9bcd932..51446d05 100644 --- a/src/api/integrations/chatbot/typebot/routes/typebot.router.ts +++ b/src/api/integrations/chatbot/typebot/routes/typebot.router.ts @@ -1,8 +1,8 @@ import { RouterBroker } from '@api/abstract/abstract.router'; import { InstanceDto } from '@api/dto/instance.dto'; import { TypebotDto, TypebotIgnoreJidDto, TypebotSettingDto } from '@api/integrations/chatbot/typebot/dto/typebot.dto'; +import { typebotController } from '@api/integrations/integration.module'; import { HttpStatus } from '@api/routes/index.router'; -import { typebotController } from '@api/server.module'; import { instanceSchema, typebotIgnoreJidSchema, diff --git a/src/api/controllers/event.controller.ts b/src/api/integrations/event/event.controller.ts similarity index 95% rename from src/api/controllers/event.controller.ts rename to src/api/integrations/event/event.controller.ts index d1281fda..5f528092 100644 --- a/src/api/controllers/event.controller.ts +++ b/src/api/integrations/event/event.controller.ts @@ -1,5 +1,10 @@ +import { + rabbitmqController, + sqsController, + webhookController, + websocketController, +} from '@api/integrations/integration.module'; import { PrismaRepository } from '@api/repository/repository.service'; -import { rabbitmqController, sqsController, webhookController, websocketController } from '@api/server.module'; import { WAMonitoringService } from '@api/services/monitor.service'; import { Server } from 'http'; diff --git a/src/api/routes/event.router.ts b/src/api/integrations/event/event.router.ts similarity index 100% rename from src/api/routes/event.router.ts rename to src/api/integrations/event/event.router.ts diff --git a/src/api/integrations/event/event.schema.ts b/src/api/integrations/event/event.schema.ts new file mode 100644 index 00000000..8bd1598d --- /dev/null +++ b/src/api/integrations/event/event.schema.ts @@ -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'; diff --git a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts index 7519bb9e..af33ae6f 100644 --- a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts +++ b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts @@ -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); diff --git a/src/api/integrations/event/rabbitmq/dto/rabbitmq.dto.ts b/src/api/integrations/event/rabbitmq/dto/rabbitmq.dto.ts index 3766f287..7e7fb6db 100644 --- a/src/api/integrations/event/rabbitmq/dto/rabbitmq.dto.ts +++ b/src/api/integrations/event/rabbitmq/dto/rabbitmq.dto.ts @@ -1,4 +1,4 @@ -import { Constructor } from '@api/dto/integration.dto'; +import { Constructor } from '@api/integrations/integration.dto'; export class RabbitmqDto { enabled: boolean; diff --git a/src/api/integrations/event/rabbitmq/routes/rabbitmq.router.ts b/src/api/integrations/event/rabbitmq/routes/rabbitmq.router.ts index 8a11975e..d8b5e27c 100644 --- a/src/api/integrations/event/rabbitmq/routes/rabbitmq.router.ts +++ b/src/api/integrations/event/rabbitmq/routes/rabbitmq.router.ts @@ -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'; diff --git a/src/api/integrations/event/sqs/controllers/sqs.controller.ts b/src/api/integrations/event/sqs/controllers/sqs.controller.ts index 534f1c98..519e2762 100644 --- a/src/api/integrations/event/sqs/controllers/sqs.controller.ts +++ b/src/api/integrations/event/sqs/controllers/sqs.controller.ts @@ -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); diff --git a/src/api/integrations/event/sqs/dto/sqs.dto.ts b/src/api/integrations/event/sqs/dto/sqs.dto.ts index daca23fd..8a2cfd9e 100644 --- a/src/api/integrations/event/sqs/dto/sqs.dto.ts +++ b/src/api/integrations/event/sqs/dto/sqs.dto.ts @@ -1,4 +1,4 @@ -import { Constructor } from '@api/dto/integration.dto'; +import { Constructor } from '@api/integrations/integration.dto'; export class SqsDto { enabled: boolean; diff --git a/src/api/integrations/event/sqs/routes/sqs.router.ts b/src/api/integrations/event/sqs/routes/sqs.router.ts index 9a811c66..16163448 100644 --- a/src/api/integrations/event/sqs/routes/sqs.router.ts +++ b/src/api/integrations/event/sqs/routes/sqs.router.ts @@ -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'; diff --git a/src/api/integrations/event/webhook/controllers/webhook.controller.ts b/src/api/integrations/event/webhook/controllers/webhook.controller.ts index 83b884f1..d77363b6 100644 --- a/src/api/integrations/event/webhook/controllers/webhook.controller.ts +++ b/src/api/integrations/event/webhook/controllers/webhook.controller.ts @@ -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 { diff --git a/src/api/integrations/event/webhook/dto/webhook.dto.ts b/src/api/integrations/event/webhook/dto/webhook.dto.ts index 6d1d7eea..be95e364 100644 --- a/src/api/integrations/event/webhook/dto/webhook.dto.ts +++ b/src/api/integrations/event/webhook/dto/webhook.dto.ts @@ -1,4 +1,4 @@ -import { Constructor } from '@api/dto/integration.dto'; +import { Constructor } from '@api/integrations/integration.dto'; export class WebhookDto { enabled?: boolean; diff --git a/src/api/integrations/event/webhook/routes/webhook.router.ts b/src/api/integrations/event/webhook/routes/webhook.router.ts index ca9a90bb..e02a2750 100644 --- a/src/api/integrations/event/webhook/routes/webhook.router.ts +++ b/src/api/integrations/event/webhook/routes/webhook.router.ts @@ -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('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); }); } diff --git a/src/validate/webhook.schema.ts b/src/api/integrations/event/webhook/validate/webhook.schema.ts similarity index 100% rename from src/validate/webhook.schema.ts rename to src/api/integrations/event/webhook/validate/webhook.schema.ts diff --git a/src/api/integrations/event/webhook/validate/websocket.schema.ts b/src/api/integrations/event/webhook/validate/websocket.schema.ts deleted file mode 100644 index 8a7678c1..00000000 --- a/src/api/integrations/event/webhook/validate/websocket.schema.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { JSONSchema7 } from 'json-schema'; -import { v4 } from 'uuid'; - -const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { - const properties = {}; - propertyNames.forEach( - (property) => - (properties[property] = { - minLength: 1, - description: `The "${property}" cannot be empty`, - }), - ); - return { - if: { - propertyNames: { - enum: [...propertyNames], - }, - }, - then: { properties }, - }; -}; - -export const websocketSchema: JSONSchema7 = { - $id: v4(), - type: 'object', - properties: { - enabled: { type: 'boolean', enum: [true, false] }, - events: { - type: 'array', - minItems: 0, - items: { - type: 'string', - enum: [ - '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', - ], - }, - }, - }, - required: ['enabled'], - ...isNotEmpty('enabled'), -}; diff --git a/src/api/integrations/event/websocket/controllers/websocket.controller.ts b/src/api/integrations/event/websocket/controllers/websocket.controller.ts index ca127f0c..7bfaa1b4 100644 --- a/src/api/integrations/event/websocket/controllers/websocket.controller.ts +++ b/src/api/integrations/event/websocket/controllers/websocket.controller.ts @@ -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; diff --git a/src/api/integrations/event/websocket/dto/websocket.dto.ts b/src/api/integrations/event/websocket/dto/websocket.dto.ts index cead7b84..947c8fd3 100644 --- a/src/api/integrations/event/websocket/dto/websocket.dto.ts +++ b/src/api/integrations/event/websocket/dto/websocket.dto.ts @@ -1,4 +1,4 @@ -import { Constructor } from '@api/dto/integration.dto'; +import { Constructor } from '@api/integrations/integration.dto'; export class WebsocketDto { enabled: boolean; diff --git a/src/api/integrations/event/websocket/routes/websocket.router.ts b/src/api/integrations/event/websocket/routes/websocket.router.ts index 7d46d50d..d2647524 100644 --- a/src/api/integrations/event/websocket/routes/websocket.router.ts +++ b/src/api/integrations/event/websocket/routes/websocket.router.ts @@ -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'; diff --git a/src/api/dto/integration.dto.ts b/src/api/integrations/integration.dto.ts similarity index 100% rename from src/api/dto/integration.dto.ts rename to src/api/integrations/integration.dto.ts diff --git a/src/api/integrations/integration.module.ts b/src/api/integrations/integration.module.ts new file mode 100644 index 00000000..5f124604 --- /dev/null +++ b/src/api/integrations/integration.module.ts @@ -0,0 +1,29 @@ +import { prismaRepository, waMonitor } from '@api/server.module'; +import { configService } from '@config/env.config'; + +import { DifyController } from './chatbot/dify/controllers/dify.controller'; +import { DifyService } from './chatbot/dify/services/dify.service'; +import { OpenaiController } from './chatbot/openai/controllers/openai.controller'; +import { OpenaiService } from './chatbot/openai/services/openai.service'; +import { TypebotController } from './chatbot/typebot/controllers/typebot.controller'; +import { TypebotService } from './chatbot/typebot/services/typebot.service'; +import { RabbitmqController } from './event/rabbitmq/controllers/rabbitmq.controller'; +import { SqsController } from './event/sqs/controllers/sqs.controller'; +import { WebhookController } from './event/webhook/controllers/webhook.controller'; +import { WebsocketController } from './event/websocket/controllers/websocket.controller'; + +// events +export const websocketController = new WebsocketController(prismaRepository, waMonitor); +export const rabbitmqController = new RabbitmqController(prismaRepository, waMonitor); +export const sqsController = new SqsController(prismaRepository, waMonitor); +export const webhookController = new WebhookController(prismaRepository, waMonitor); + +// chatbots +const typebotService = new TypebotService(waMonitor, configService, prismaRepository); +export const typebotController = new TypebotController(typebotService); + +const openaiService = new OpenaiService(waMonitor, configService, prismaRepository); +export const openaiController = new OpenaiController(openaiService); + +const difyService = new DifyService(waMonitor, configService, prismaRepository); +export const difyController = new DifyController(difyService); diff --git a/src/api/routes/storage.router.ts b/src/api/integrations/storage/storage.router.ts similarity index 100% rename from src/api/routes/storage.router.ts rename to src/api/integrations/storage/storage.router.ts diff --git a/src/api/routes/index.router.ts b/src/api/routes/index.router.ts index a8df0ac3..9ce3f033 100644 --- a/src/api/routes/index.router.ts +++ b/src/api/routes/index.router.ts @@ -1,23 +1,22 @@ import { authGuard } from '@api/guards/auth.guard'; import { instanceExistsGuard, instanceLoggedGuard } from '@api/guards/instance.guard'; import Telemetry from '@api/guards/telemetry.guard'; -import { webhookController } from '@api/server.module'; -import { configService, WaBusiness } from '@config/env.config'; +import { ChatbotRouter } from '@api/integrations/chatbot/chatbot.router'; +import { EventRouter } from '@api/integrations/event/event.router'; +import { StorageRouter } from '@api/integrations/storage/storage.router'; +import { configService } from '@config/env.config'; import { Router } from 'express'; import fs from 'fs'; 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'; @@ -85,17 +84,6 @@ router .use('/settings', new SettingsRouter(...guards).router) .use('/proxy', new ProxyRouter(...guards).router) .use('/label', new LabelRouter(...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']); - else res.send('Error, wrong validation token'); - }) - .post('/webhook/meta', async (req, res) => { - const { body } = req; - 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); diff --git a/src/api/server.module.ts b/src/api/server.module.ts index 8e375b7b..1dbdd602 100644 --- a/src/api/server.module.ts +++ b/src/api/server.module.ts @@ -4,7 +4,6 @@ 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'; @@ -12,18 +11,10 @@ import { ProxyController } from './controllers/proxy.controller'; import { SendMessageController } from './controllers/sendMessage.controller'; import { SettingsController } from './controllers/settings.controller'; import { TemplateController } from './controllers/template.controller'; +import { ChatbotController } from './integrations/chatbot/chatbot.controller'; import { ChatwootController } from './integrations/chatbot/chatwoot/controllers/chatwoot.controller'; import { ChatwootService } from './integrations/chatbot/chatwoot/services/chatwoot.service'; -import { DifyController } from './integrations/chatbot/dify/controllers/dify.controller'; -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 { 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 { SqsController } from './integrations/event/sqs/controllers/sqs.controller'; -import { WebhookController } from './integrations/event/webhook/controllers/webhook.controller'; -import { WebsocketController } from './integrations/event/websocket/controllers/websocket.controller'; +import { EventController } from './integrations/event/event.controller'; import { S3Controller } from './integrations/storage/s3/controllers/s3.controller'; import { S3Service } from './integrations/storage/s3/services/s3.service'; import { ProviderFiles } from './provider/sessions'; @@ -61,15 +52,6 @@ export const waMonitor = new WAMonitoringService( baileysCache, ); -const typebotService = new TypebotService(waMonitor, configService, prismaRepository); -export const typebotController = new TypebotController(typebotService); - -const openaiService = new OpenaiService(waMonitor, configService, prismaRepository); -export const openaiController = new OpenaiController(openaiService); - -const difyService = new DifyService(waMonitor, configService, prismaRepository); -export const difyController = new DifyController(difyService); - const s3Service = new S3Service(prismaRepository); export const s3Controller = new S3Controller(s3Service); @@ -77,11 +59,7 @@ const templateService = new TemplateService(waMonitor, prismaRepository, configS export const templateController = new TemplateController(templateService); export const eventController = new EventController(prismaRepository, waMonitor); - -export const websocketController = new WebsocketController(prismaRepository, waMonitor); -export const rabbitmqController = new RabbitmqController(prismaRepository, waMonitor); -export const sqsController = new SqsController(prismaRepository, waMonitor); -export const webhookController = new WebhookController(prismaRepository, waMonitor); +export const chatbotController = new ChatbotController(prismaRepository, waMonitor); const proxyService = new ProxyService(waMonitor); export const proxyController = new ProxyController(proxyService, waMonitor); diff --git a/src/validate/event.schema.ts b/src/validate/event.schema.ts deleted file mode 100644 index d4f691ce..00000000 --- a/src/validate/event.schema.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './webhook.schema'; -export * from './websocket.schema'; -export * from '@api/integrations/event/rabbitmq/validate/rabbitmq.schema'; -export * from '@api/integrations/event/sqs/validate/sqs.schema'; diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index fb17e3d5..cf3d7828 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -1,7 +1,5 @@ // Integrations Schema export * from './chat.schema'; -export * from './chatbot.schema'; -export * from './event.schema'; export * from './group.schema'; export * from './instance.schema'; export * from './label.schema'; @@ -9,3 +7,5 @@ export * from './message.schema'; export * from './proxy.schema'; export * from './settings.schema'; export * from './template.schema'; +export * from '@api/integrations/chatbot/chatbot.schema'; +export * from '@api/integrations/event/event.schema'; diff --git a/src/validate/websocket.schema.ts b/src/validate/websocket.schema.ts deleted file mode 100644 index 8a7678c1..00000000 --- a/src/validate/websocket.schema.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { JSONSchema7 } from 'json-schema'; -import { v4 } from 'uuid'; - -const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { - const properties = {}; - propertyNames.forEach( - (property) => - (properties[property] = { - minLength: 1, - description: `The "${property}" cannot be empty`, - }), - ); - return { - if: { - propertyNames: { - enum: [...propertyNames], - }, - }, - then: { properties }, - }; -}; - -export const websocketSchema: JSONSchema7 = { - $id: v4(), - type: 'object', - properties: { - enabled: { type: 'boolean', enum: [true, false] }, - events: { - type: 'array', - minItems: 0, - items: { - type: 'string', - enum: [ - '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', - ], - }, - }, - }, - required: ['enabled'], - ...isNotEmpty('enabled'), -};