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.
This commit is contained in:
Davidson Gomes 2025-01-17 17:54:18 -03:00
parent ac58f58bbc
commit cfe6bd9ae0
3 changed files with 26 additions and 28 deletions

View File

@ -410,15 +410,11 @@ export class InstanceController {
public async deleteInstance({ instanceName }: InstanceDto) { public async deleteInstance({ instanceName }: InstanceDto) {
const { instance } = await this.connectionState({ instanceName }); const { instance } = await this.connectionState({ instanceName });
if (instance.state === 'open') {
throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected');
}
try { try {
const waInstances = this.waMonitor.waInstances[instanceName]; const waInstances = this.waMonitor.waInstances[instanceName];
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot(); if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
if (instance.state === 'connecting') { if (instance.state === 'connecting' || instance.state === 'open') {
await this.logout({ instanceName }); await this.logout({ instanceName });
} }

View File

@ -312,6 +312,9 @@ export class BaileysStartupService extends ChannelStartupService {
instance: this.instance.name, instance: this.instance.name,
state: 'refused', state: 'refused',
statusReason: DisconnectReason.connectionClosed, statusReason: DisconnectReason.connectionClosed,
wuid: this.instance.wuid,
profileName: await this.getProfileName(),
profilePictureUrl: this.instance.profilePictureUrl,
}); });
this.endSession = true; this.endSession = true;
@ -391,11 +394,6 @@ export class BaileysStartupService extends ChannelStartupService {
state: connection, state: connection,
statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200, statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200,
}; };
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
instance: this.instance.name,
...this.stateConnection,
});
} }
if (connection === 'close') { if (connection === 'close') {
@ -437,6 +435,11 @@ export class BaileysStartupService extends ChannelStartupService {
this.eventEmitter.emit('logout.instance', this.instance.name, 'inner'); this.eventEmitter.emit('logout.instance', this.instance.name, 'inner');
this.client?.ws?.close(); this.client?.ws?.close();
this.client.end(new Error('Close connection')); 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.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,
});
} }
} }

View File

@ -33,22 +33,6 @@ export const settingsSchema: JSONSchema7 = {
syncFullHistory: { type: 'boolean' }, syncFullHistory: { type: 'boolean' },
wavoipToken: { type: 'string' }, wavoipToken: { type: 'string' },
}, },
required: [ required: ['rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'],
'rejectCall', ...isNotEmpty('rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'),
'groupsIgnore',
'alwaysOnline',
'readMessages',
'readStatus',
'syncFullHistory',
'wavoipToken',
],
...isNotEmpty(
'rejectCall',
'groupsIgnore',
'alwaysOnline',
'readMessages',
'readStatus',
'syncFullHistory',
'wavoipToken',
),
}; };