From 583ce33868ce8906823e5b9ad25b258bb0e97aad Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 20 Aug 2024 18:47:24 -0300 Subject: [PATCH] refactor: channel controller --- src/api/controllers/instance.controller.ts | 35 ++++------- .../channel/channel.controller.ts | 45 ++++++++++++++ src/api/server.module.ts | 2 + src/api/services/monitor.service.ts | 58 ++++++------------- 4 files changed, 75 insertions(+), 65 deletions(-) create mode 100644 src/api/integrations/channel/channel.controller.ts diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 6df6d854..20846250 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -1,10 +1,8 @@ import { InstanceDto, SetPresenceDto } from '@api/dto/instance.dto'; -import { BaileysStartupService } from '@api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service'; -import { BusinessStartupService } from '@api/integrations/channel/whatsapp/business/whatsapp.business.service'; import { ChatwootService } from '@api/integrations/chatbot/chatwoot/services/chatwoot.service'; import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; -import { eventController } from '@api/server.module'; +import { channelController, eventController } from '@api/server.module'; import { CacheService } from '@api/services/cache.service'; import { WAMonitoringService } from '@api/services/monitor.service'; import { SettingsService } from '@api/services/settings.service'; @@ -42,28 +40,15 @@ export class InstanceController { throw new BadRequestException('token is required'); } - let instance: BaileysStartupService | BusinessStartupService; - if (instanceData.integration === Integration.WHATSAPP_BUSINESS) { - instance = new BusinessStartupService( - this.configService, - this.eventEmitter, - this.prismaRepository, - this.cache, - this.chatwootCache, - this.baileysCache, - this.providerFiles, - ); - } else { - instance = new BaileysStartupService( - this.configService, - this.eventEmitter, - this.prismaRepository, - this.cache, - this.chatwootCache, - this.baileysCache, - this.providerFiles, - ); - } + const instance = channelController.init(instanceData.integration, { + configService: this.configService, + eventEmitter: this.eventEmitter, + prismaRepository: this.prismaRepository, + cache: this.cache, + chatwootCache: this.chatwootCache, + baileysCache: this.baileysCache, + providerFiles: this.providerFiles, + }); const instanceId = v4(); diff --git a/src/api/integrations/channel/channel.controller.ts b/src/api/integrations/channel/channel.controller.ts new file mode 100644 index 00000000..ab3f1764 --- /dev/null +++ b/src/api/integrations/channel/channel.controller.ts @@ -0,0 +1,45 @@ +import { ProviderFiles } from '@api/provider/sessions'; +import { PrismaRepository } from '@api/repository/repository.service'; +import { CacheService } from '@api/services/cache.service'; +import { Integration } from '@api/types/wa.types'; +import { ConfigService } from '@config/env.config'; +import EventEmitter2 from 'eventemitter2'; + +import { BaileysStartupService } from './whatsapp/baileys/whatsapp.baileys.service'; +import { BusinessStartupService } from './whatsapp/business/whatsapp.business.service'; + +type ChannelDataType = { + configService: ConfigService; + eventEmitter: EventEmitter2; + prismaRepository: PrismaRepository; + cache: CacheService; + chatwootCache: CacheService; + baileysCache: CacheService; + providerFiles: ProviderFiles; +}; + +export class ChannelController { + public init(integration: string, data: ChannelDataType) { + if (integration === Integration.WHATSAPP_BUSINESS) { + return new BusinessStartupService( + data.configService, + data.eventEmitter, + data.prismaRepository, + data.cache, + data.chatwootCache, + data.baileysCache, + data.providerFiles, + ); + } + + return new BaileysStartupService( + data.configService, + data.eventEmitter, + data.prismaRepository, + data.cache, + data.chatwootCache, + data.baileysCache, + data.providerFiles, + ); + } +} diff --git a/src/api/server.module.ts b/src/api/server.module.ts index a8a8baba..2f18075c 100644 --- a/src/api/server.module.ts +++ b/src/api/server.module.ts @@ -11,6 +11,7 @@ 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 { ChannelController } from './integrations/channel/channel.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'; @@ -97,6 +98,7 @@ export const labelController = new LabelController(waMonitor); export const eventController = new EventController(prismaRepository, waMonitor); export const chatbotController = new ChatbotController(prismaRepository, waMonitor); +export const channelController = new ChannelController(); // events export const websocketController = new WebsocketController(prismaRepository, waMonitor); diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index 763059b1..9d7a503b 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -3,6 +3,7 @@ import { BaileysStartupService } from '@api/integrations/channel/whatsapp/bailey import { BusinessStartupService } from '@api/integrations/channel/whatsapp/business/whatsapp.business.service'; import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; +import { channelController } from '@api/server.module'; import { Events, Integration } from '@api/types/wa.types'; import { CacheConf, Chatwoot, ConfigService, Database, DelInstance, ProviderSession } from '@config/env.config'; import { Logger } from '@config/logger.config'; @@ -209,47 +210,24 @@ export class WAMonitoringService { } private async setInstance(instanceData: InstanceDto) { - let instance: BaileysStartupService | BusinessStartupService; + const instance = channelController.init(instanceData.integration, { + configService: this.configService, + eventEmitter: this.eventEmitter, + prismaRepository: this.prismaRepository, + cache: this.cache, + chatwootCache: this.chatwootCache, + baileysCache: this.baileysCache, + providerFiles: this.providerFiles, + }); - if (instanceData.integration && instanceData.integration === Integration.WHATSAPP_BUSINESS) { - instance = new BusinessStartupService( - this.configService, - this.eventEmitter, - this.prismaRepository, - this.cache, - this.chatwootCache, - this.baileysCache, - this.providerFiles, - ); - - instance.setInstance({ - instanceId: instanceData.instanceId, - instanceName: instanceData.instanceName, - integration: instanceData.integration, - token: instanceData.token, - number: instanceData.number, - businessId: instanceData.businessId, - }); - } else { - instance = new BaileysStartupService( - this.configService, - this.eventEmitter, - this.prismaRepository, - this.cache, - this.chatwootCache, - this.baileysCache, - this.providerFiles, - ); - - instance.setInstance({ - instanceId: instanceData.instanceId, - instanceName: instanceData.instanceName, - integration: instanceData.integration, - token: instanceData.token, - number: instanceData.number, - businessId: instanceData.businessId, - }); - } + instance.setInstance({ + instanceId: instanceData.instanceId, + instanceName: instanceData.instanceName, + integration: instanceData.integration, + token: instanceData.token, + number: instanceData.number, + businessId: instanceData.businessId, + }); await instance.connectToWhatsapp();