mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
refactor: channel controller
This commit is contained in:
parent
3b39f13180
commit
583ce33868
@ -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();
|
||||
|
||||
|
45
src/api/integrations/channel/channel.controller.ts
Normal file
45
src/api/integrations/channel/channel.controller.ts
Normal file
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user