@@ -3,7 +3,7 @@ import { isURL } from 'class-validator';
33import EventEmitter2 from 'eventemitter2' ;
44import { v4 } from 'uuid' ;
55
6- import { ConfigService , HttpServer } from '../../config/env.config' ;
6+ import { ConfigService , HttpServer , WaBusiness } from '../../config/env.config' ;
77import { Logger } from '../../config/logger.config' ;
88import { BadRequestException , InternalServerErrorException } from '../../exceptions' ;
99import { RedisCache } from '../../libs/redis.client' ;
@@ -12,15 +12,17 @@ import { RepositoryBroker } from '../repository/repository.manager';
1212import { AuthService , OldToken } from '../services/auth.service' ;
1313import { CacheService } from '../services/cache.service' ;
1414import { ChatwootService } from '../services/chatwoot.service' ;
15+ import { IntegrationService } from '../services/integration.service' ;
1516import { WAMonitoringService } from '../services/monitor.service' ;
1617import { RabbitmqService } from '../services/rabbitmq.service' ;
1718import { SettingsService } from '../services/settings.service' ;
1819import { SqsService } from '../services/sqs.service' ;
1920import { TypebotService } from '../services/typebot.service' ;
2021import { WebhookService } from '../services/webhook.service' ;
2122import { WebsocketService } from '../services/websocket.service' ;
22- import { WAStartupService } from '../services/whatsapp.service' ;
23- import { Events , wa } from '../types/wa.types' ;
23+ import { BaileysStartupService } from '../services/whatsapp.baileys.service' ;
24+ import { BusinessStartupService } from '../services/whatsapp.business.service' ;
25+ import { Events , Integration , wa } from '../types/wa.types' ;
2426
2527export class InstanceController {
2628 constructor (
@@ -36,6 +38,7 @@ export class InstanceController {
3638 private readonly rabbitmqService : RabbitmqService ,
3739 private readonly sqsService : SqsService ,
3840 private readonly typebotService : TypebotService ,
41+ private readonly integrationService : IntegrationService ,
3942 private readonly cache : RedisCache ,
4043 private readonly chatwootCache : CacheService ,
4144 ) { }
@@ -50,6 +53,7 @@ export class InstanceController {
5053 events,
5154 qrcode,
5255 number,
56+ integration,
5357 token,
5458 chatwoot_account_id,
5559 chatwoot_token,
@@ -87,14 +91,31 @@ export class InstanceController {
8791 this . logger . verbose ( 'checking duplicate token' ) ;
8892 await this . authService . checkDuplicateToken ( token ) ;
8993
94+ if ( ! token && integration !== Integration . WHATSAPP_BUSINESS ) {
95+ throw new BadRequestException ( 'token is required' ) ;
96+ }
97+
9098 this . logger . verbose ( 'creating instance' ) ;
91- const instance = new WAStartupService (
92- this . configService ,
93- this . eventEmitter ,
94- this . repository ,
95- this . cache ,
96- this . chatwootCache ,
97- ) ;
99+ let instance : BaileysStartupService | BusinessStartupService ;
100+ if ( integration === Integration . WHATSAPP_BUSINESS ) {
101+ instance = new BusinessStartupService (
102+ this . configService ,
103+ this . eventEmitter ,
104+ this . repository ,
105+ this . cache ,
106+ this . chatwootCache ,
107+ ) ;
108+ await this . waMonitor . saveInstance ( { integration, instanceName, token, number } ) ;
109+ } else {
110+ instance = new BaileysStartupService (
111+ this . configService ,
112+ this . eventEmitter ,
113+ this . repository ,
114+ this . cache ,
115+ this . chatwootCache ,
116+ ) ;
117+ }
118+
98119 instance . instanceName = instanceName ;
99120
100121 const instanceId = v4 ( ) ;
@@ -361,6 +382,23 @@ export class InstanceController {
361382
362383 this . settingsService . create ( instance , settings ) ;
363384
385+ let webhook_wa_business = null ,
386+ access_token_wa_business = '' ;
387+
388+ if ( integration === Integration . WHATSAPP_BUSINESS ) {
389+ if ( ! number ) {
390+ throw new BadRequestException ( 'number is required' ) ;
391+ }
392+ const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
393+ webhook_wa_business = `${ urlServer } /webhook/whatsapp/${ encodeURIComponent ( instance . instanceName ) } ` ;
394+ access_token_wa_business = this . configService . get < WaBusiness > ( 'WA_BUSINESS' ) . TOKEN_WEBHOOK ;
395+ }
396+
397+ this . integrationService . create ( instance , {
398+ integration,
399+ number,
400+ token,
401+ } ) ;
364402 if ( ! chatwoot_account_id || ! chatwoot_token || ! chatwoot_url ) {
365403 let getQrcode : wa . QrCode ;
366404
@@ -375,6 +413,9 @@ export class InstanceController {
375413 instance : {
376414 instanceName : instance . instanceName ,
377415 instanceId : instanceId ,
416+ integration : integration ,
417+ webhook_wa_business,
418+ access_token_wa_business,
378419 status : 'created' ,
379420 } ,
380421 hash,
@@ -470,6 +511,9 @@ export class InstanceController {
470511 instance : {
471512 instanceName : instance . instanceName ,
472513 instanceId : instanceId ,
514+ integration : integration ,
515+ webhook_wa_business,
516+ access_token_wa_business,
473517 status : 'created' ,
474518 } ,
475519 hash,
0 commit comments