From c6db03ffa24a91b1a4fae05efcac67a53b42b239 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 3 Aug 2024 12:54:23 -0300 Subject: [PATCH] fix: Adjusts in restart instance --- CHANGELOG.md | 1 + src/api/controllers/instance.controller.ts | 26 ++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0d964be..3e4494ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Fixed * BusinessId added on create instances in manager +* Adjusts in restart instance # 2.0.6-rc (2024-08-02 19:23) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 72e64265..302b598d 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -583,6 +583,7 @@ export class InstanceController { }; } catch (error) { this.logger.error(error); + return { error: true, message: error.toString() }; } } @@ -591,19 +592,26 @@ export class InstanceController { const instance = this.waMonitor.waInstances[instanceName]; const state = instance?.connectionStatus?.state; - switch (state) { - case 'open': - if (this.configService.get('CHATWOOT').ENABLED) instance.clearCacheChatwoot(); + if (!state) { + throw new BadRequestException('The "' + instanceName + '" instance does not exist'); + } - this.logger.info('Connection closed, restarting instance' + instanceName); - instance.client?.ws?.close(); - instance.client?.end(new Error('restart')); - return await this.connectToWhatsapp({ instanceName }); - default: - return await this.connectionState({ instanceName }); + if (state == 'close') { + throw new BadRequestException('The "' + instanceName + '" instance is not connected'); + } else if (state == 'open') { + if (this.configService.get('CHATWOOT').ENABLED) instance.clearCacheChatwoot(); + this.logger.info('restarting instance' + instanceName); + + instance.client?.ws?.close(); + instance.client?.end(new Error('restart')); + return await this.connectToWhatsapp({ instanceName }); + } else if (state == 'connecting') { + instance.client?.end(new Error('restart')); + return await this.connectToWhatsapp({ instanceName }); } } catch (error) { this.logger.error(error); + return { error: true, message: error.toString() }; } }