From cfe6bd9ae0bea93efc61a425415a9396b52b73d6 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 17 Jan 2025 17:54:18 -0300 Subject: [PATCH] Refactor instance deletion logic and enhance WhatsApp connection updates - Updated the `deleteInstance` method to allow logout for instances in 'connecting' or 'open' states, improving instance management. - Enhanced the `BaileysStartupService` to include additional profile information (wuid, profileName, profilePictureUrl) in connection update webhooks. - Removed redundant webhook data sending logic, streamlining connection state updates for better performance. - Adjusted settings schema to ensure required fields are properly validated. --- src/api/controllers/instance.controller.ts | 6 +--- .../whatsapp/whatsapp.baileys.service.ts | 28 +++++++++++++++---- src/validate/settings.schema.ts | 20 ++----------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 99fc13f2b..e00e3ca07 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -410,15 +410,11 @@ export class InstanceController { public async deleteInstance({ instanceName }: InstanceDto) { const { instance } = await this.connectionState({ instanceName }); - - if (instance.state === 'open') { - throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected'); - } try { const waInstances = this.waMonitor.waInstances[instanceName]; if (this.configService.get('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot(); - if (instance.state === 'connecting') { + if (instance.state === 'connecting' || instance.state === 'open') { await this.logout({ instanceName }); } diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 06a73c1d5..d830c632b 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -312,6 +312,9 @@ export class BaileysStartupService extends ChannelStartupService { instance: this.instance.name, state: 'refused', statusReason: DisconnectReason.connectionClosed, + wuid: this.instance.wuid, + profileName: await this.getProfileName(), + profilePictureUrl: this.instance.profilePictureUrl, }); this.endSession = true; @@ -391,11 +394,6 @@ export class BaileysStartupService extends ChannelStartupService { state: connection, statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200, }; - - this.sendDataWebhook(Events.CONNECTION_UPDATE, { - instance: this.instance.name, - ...this.stateConnection, - }); } if (connection === 'close') { @@ -437,6 +435,11 @@ export class BaileysStartupService extends ChannelStartupService { this.eventEmitter.emit('logout.instance', this.instance.name, 'inner'); this.client?.ws?.close(); this.client.end(new Error('Close connection')); + + this.sendDataWebhook(Events.CONNECTION_UPDATE, { + instance: this.instance.name, + ...this.stateConnection, + }); } } @@ -484,6 +487,21 @@ export class BaileysStartupService extends ChannelStartupService { ); this.syncChatwootLostMessages(); } + + this.sendDataWebhook(Events.CONNECTION_UPDATE, { + instance: this.instance.name, + wuid: this.instance.wuid, + profileName: await this.getProfileName(), + profilePictureUrl: this.instance.profilePictureUrl, + ...this.stateConnection, + }); + } + + if (connection === 'connecting') { + this.sendDataWebhook(Events.CONNECTION_UPDATE, { + instance: this.instance.name, + ...this.stateConnection, + }); } } diff --git a/src/validate/settings.schema.ts b/src/validate/settings.schema.ts index 50cbfe6ae..95a0e9c96 100644 --- a/src/validate/settings.schema.ts +++ b/src/validate/settings.schema.ts @@ -33,22 +33,6 @@ export const settingsSchema: JSONSchema7 = { syncFullHistory: { type: 'boolean' }, wavoipToken: { type: 'string' }, }, - required: [ - 'rejectCall', - 'groupsIgnore', - 'alwaysOnline', - 'readMessages', - 'readStatus', - 'syncFullHistory', - 'wavoipToken', - ], - ...isNotEmpty( - 'rejectCall', - 'groupsIgnore', - 'alwaysOnline', - 'readMessages', - 'readStatus', - 'syncFullHistory', - 'wavoipToken', - ), + required: ['rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'], + ...isNotEmpty('rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'), };