11import { InstanceDto , SetPresenceDto } from '@api/dto/instance.dto' ;
2- import { ChatwootService } from '@api/integrations/chatbot/chatwoot/services/chatwoot.service' ;
32import { ProviderFiles } from '@api/provider/sessions' ;
43import { PrismaRepository } from '@api/repository/repository.service' ;
54import { channelController , eventManager } from '@api/server.module' ;
65import { CacheService } from '@api/services/cache.service' ;
76import { WAMonitoringService } from '@api/services/monitor.service' ;
87import { SettingsService } from '@api/services/settings.service' ;
98import { Events , Integration , wa } from '@api/types/wa.types' ;
10- import { Auth , Chatwoot , ConfigService , HttpServer , WaBusiness } from '@config/env.config' ;
9+ import { Auth , ConfigService , HttpServer , WaBusiness } from '@config/env.config' ;
1110import { Logger } from '@config/logger.config' ;
1211import { BadRequestException , InternalServerErrorException , UnauthorizedException } from '@exceptions' ;
1312import { delay } from 'baileys' ;
14- import { isArray , isURL } from 'class-validator' ;
13+ import { isArray } from 'class-validator' ;
1514import EventEmitter2 from 'eventemitter2' ;
1615import { v4 } from 'uuid' ;
1716
@@ -23,11 +22,9 @@ export class InstanceController {
2322 private readonly configService : ConfigService ,
2423 private readonly prismaRepository : PrismaRepository ,
2524 private readonly eventEmitter : EventEmitter2 ,
26- private readonly chatwootService : ChatwootService ,
2725 private readonly settingsService : SettingsService ,
2826 private readonly proxyService : ProxyController ,
2927 private readonly cache : CacheService ,
30- private readonly chatwootCache : CacheService ,
3128 private readonly baileysCache : CacheService ,
3229 private readonly providerFiles : ProviderFiles ,
3330 ) { }
@@ -41,7 +38,6 @@ export class InstanceController {
4138 eventEmitter : this . eventEmitter ,
4239 prismaRepository : this . prismaRepository ,
4340 cache : this . cache ,
44- chatwootCache : this . chatwootCache ,
4541 baileysCache : this . baileysCache ,
4642 providerFiles : this . providerFiles ,
4743 } ) ;
@@ -135,104 +131,15 @@ export class InstanceController {
135131 accessTokenWaBusiness = this . configService . get < WaBusiness > ( 'WA_BUSINESS' ) . TOKEN_WEBHOOK ;
136132 }
137133
138- if ( ! instanceData . chatwootAccountId || ! instanceData . chatwootToken || ! instanceData . chatwootUrl ) {
139- let getQrcode : wa . QrCode ;
134+ let getQrcode : wa . QrCode ;
140135
141- if ( instanceData . qrcode && instanceData . integration === Integration . WHATSAPP_BAILEYS ) {
142- await instance . connectToWhatsapp ( instanceData . number ) ;
143- await delay ( 5000 ) ;
144- getQrcode = instance . qrCode ;
145- }
146-
147- const result = {
148- instance : {
149- instanceName : instance . instanceName ,
150- instanceId : instanceId ,
151- integration : instanceData . integration ,
152- webhookWaBusiness,
153- accessTokenWaBusiness,
154- status : instance . connectionStatus . state ,
155- } ,
156- hash,
157- webhook : {
158- webhookUrl : instanceData ?. webhook ?. url ,
159- webhookHeaders : instanceData ?. webhook ?. headers ,
160- webhookByEvents : instanceData ?. webhook ?. byEvents ,
161- webhookBase64 : instanceData ?. webhook ?. base64 ,
162- } ,
163- websocket : {
164- enabled : instanceData ?. websocket ?. enabled ,
165- } ,
166- rabbitmq : {
167- enabled : instanceData ?. rabbitmq ?. enabled ,
168- } ,
169- sqs : {
170- enabled : instanceData ?. sqs ?. enabled ,
171- } ,
172- settings,
173- qrcode : getQrcode ,
174- } ;
175-
176- return result ;
177- }
178-
179- if ( ! this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED )
180- throw new BadRequestException ( 'Chatwoot is not enabled' ) ;
181-
182- if ( ! instanceData . chatwootAccountId ) {
183- throw new BadRequestException ( 'accountId is required' ) ;
184- }
185-
186- if ( ! instanceData . chatwootToken ) {
187- throw new BadRequestException ( 'token is required' ) ;
188- }
189-
190- if ( ! instanceData . chatwootUrl ) {
191- throw new BadRequestException ( 'url is required' ) ;
192- }
193-
194- if ( ! isURL ( instanceData . chatwootUrl , { require_tld : false } ) ) {
195- throw new BadRequestException ( 'Invalid "url" property in chatwoot' ) ;
196- }
197-
198- if ( instanceData . chatwootSignMsg !== true && instanceData . chatwootSignMsg !== false ) {
199- throw new BadRequestException ( 'signMsg is required' ) ;
200- }
201-
202- if ( instanceData . chatwootReopenConversation !== true && instanceData . chatwootReopenConversation !== false ) {
203- throw new BadRequestException ( 'reopenConversation is required' ) ;
136+ if ( instanceData . qrcode && instanceData . integration === Integration . WHATSAPP_BAILEYS ) {
137+ await instance . connectToWhatsapp ( instanceData . number ) ;
138+ await delay ( 5000 ) ;
139+ getQrcode = instance . qrCode ;
204140 }
205141
206- if ( instanceData . chatwootConversationPending !== true && instanceData . chatwootConversationPending !== false ) {
207- throw new BadRequestException ( 'conversationPending is required' ) ;
208- }
209-
210- const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
211-
212- try {
213- this . chatwootService . create ( instance , {
214- enabled : true ,
215- accountId : instanceData . chatwootAccountId ,
216- token : instanceData . chatwootToken ,
217- url : instanceData . chatwootUrl ,
218- signMsg : instanceData . chatwootSignMsg || false ,
219- nameInbox : instanceData . chatwootNameInbox ?? instance . instanceName . split ( '-cwId-' ) [ 0 ] ,
220- number : instanceData . number ,
221- reopenConversation : instanceData . chatwootReopenConversation || false ,
222- conversationPending : instanceData . chatwootConversationPending || false ,
223- importContacts : instanceData . chatwootImportContacts ?? true ,
224- mergeBrazilContacts : instanceData . chatwootMergeBrazilContacts ?? false ,
225- importMessages : instanceData . chatwootImportMessages ?? true ,
226- daysLimitImportMessages : instanceData . chatwootDaysLimitImportMessages ?? 60 ,
227- organization : instanceData . chatwootOrganization ,
228- logo : instanceData . chatwootLogo ,
229- autoCreate : instanceData . chatwootAutoCreate !== false ,
230- } ) ;
231- } catch ( error ) {
232- this . logger . log ( error ) ;
233- }
234-
235- return {
142+ const result = {
236143 instance : {
237144 instanceName : instance . instanceName ,
238145 instanceId : instanceId ,
@@ -258,23 +165,10 @@ export class InstanceController {
258165 enabled : instanceData ?. sqs ?. enabled ,
259166 } ,
260167 settings,
261- chatwoot : {
262- enabled : true ,
263- accountId : instanceData . chatwootAccountId ,
264- token : instanceData . chatwootToken ,
265- url : instanceData . chatwootUrl ,
266- signMsg : instanceData . chatwootSignMsg || false ,
267- reopenConversation : instanceData . chatwootReopenConversation || false ,
268- conversationPending : instanceData . chatwootConversationPending || false ,
269- mergeBrazilContacts : instanceData . chatwootMergeBrazilContacts ?? false ,
270- importContacts : instanceData . chatwootImportContacts ?? true ,
271- importMessages : instanceData . chatwootImportMessages ?? true ,
272- daysLimitImportMessages : instanceData . chatwootDaysLimitImportMessages || 60 ,
273- number : instanceData . number ,
274- nameInbox : instanceData . chatwootNameInbox ?? instance . instanceName ,
275- webhookUrl : `${ urlServer } /chatwoot/webhook/${ encodeURIComponent ( instance . instanceName ) } ` ,
276- } ,
168+ qrcode : getQrcode ,
277169 } ;
170+
171+ return result ;
278172 } catch ( error ) {
279173 this . waMonitor . deleteInstance ( instanceData . instanceName ) ;
280174 this . logger . error ( isArray ( error . message ) ? error . message [ 0 ] : error . message ) ;
@@ -331,7 +225,6 @@ export class InstanceController {
331225 if ( state == 'close' ) {
332226 throw new BadRequestException ( 'The "' + instanceName + '" instance is not connected' ) ;
333227 } else if ( state == 'open' ) {
334- if ( this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED ) instance . clearCacheChatwoot ( ) ;
335228 this . logger . info ( 'restarting instance' + instanceName ) ;
336229
337230 instance . client ?. ws ?. close ( ) ;
@@ -415,8 +308,6 @@ export class InstanceController {
415308 }
416309 try {
417310 const waInstances = this . waMonitor . waInstances [ instanceName ] ;
418- if ( this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED ) waInstances ?. clearCacheChatwoot ( ) ;
419-
420311 if ( instance . state === 'connecting' ) {
421312 await this . logout ( { instanceName } ) ;
422313 }
0 commit comments