From 2b87883e7c1b523eb5a828e9042182159595a519 Mon Sep 17 00:00:00 2001 From: Alan Mosko Date: Fri, 21 Jul 2023 10:15:46 -0300 Subject: [PATCH] =?UTF-8?q?Melhoria=20da=20Fun=C3=A7=C3=A3o=20connectToWha?= =?UTF-8?q?tsapp=20e=20connectionState?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/instance.controller.ts | 45 ++++++++++++------- src/whatsapp/whatsapp.module.ts | 13 +----- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index f0adb3a3..9a76f05f 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -337,23 +337,29 @@ export class InstanceController { this.logger.verbose( 'requested connectToWhatsapp from ' + instanceName + ' instance', ); - + const instance = this.waMonitor.waInstances[instanceName]; const state = instance?.connectionStatus?.state; - + this.logger.verbose('state: ' + state); - - switch (state) { - case 'close': - this.logger.verbose('connecting'); + + if (state == 'open') { + return await this.connectionState({ instanceName }); + } + + if (state == 'close') { + this.logger.verbose('connecting'); await instance.connectToWhatsapp(); await delay(2000); - return instance.qrCode; - case 'connecting': - return instance.qrCode; - default: - return await this.connectionState({ instanceName }); } + + return { + instance: { + instanceName: instanceName, + status: state, + }, + qrcode: instance?.qrCode + }; } catch (error) { this.logger.error(error); } @@ -374,7 +380,12 @@ export class InstanceController { public async connectionState({ instanceName }: InstanceDto) { this.logger.verbose('requested connectionState from ' + instanceName + ' instance'); - return this.waMonitor.waInstances[instanceName]?.connectionStatus; + return { + instance: { + instanceName: instanceName, + state: this.waMonitor.waInstances[instanceName]?.connectionStatus?.state, + } + }; } public async fetchInstances({ instanceName }: InstanceDto) { @@ -389,9 +400,9 @@ export class InstanceController { public async logout({ instanceName }: InstanceDto) { this.logger.verbose('requested logout from ' + instanceName + ' instance'); - const stateConn = await this.connectionState({ instanceName }); + const { instance } = await this.connectionState({ instanceName }); - if (stateConn.state === 'close') { + if (instance.state === 'close') { throw new BadRequestException( 'The "' + instanceName + '" instance is not connected', ); @@ -414,15 +425,15 @@ export class InstanceController { public async deleteInstance({ instanceName }: InstanceDto) { this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance'); - const stateConn = await this.connectionState({ instanceName }); + const { instance } = await this.connectionState({ instanceName }); - if (stateConn.state === 'open') { + if (instance.state === 'open') { throw new BadRequestException( 'The "' + instanceName + '" instance needs to be disconnected', ); } try { - if (stateConn.state === 'connecting') { + if (instance.state === 'connecting') { this.logger.verbose('logging out instance: ' + instanceName); await this.logout({ instanceName }); diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index dbc80427..0a3561fb 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -165,18 +165,7 @@ export async function initInstance() { // } try { - const state = instance.connectionStatus?.state; - - switch (state) { - case 'close': - await instance.connectToWhatsapp(); - await delay(2000); - return instance.qrCode; - case 'connecting': - return instance.qrCode; - default: - return await this.connectionState({ instanceName }); - } + return await instance.connectToWhatsapp(); } catch (error) { logger.log(error); }