Skip to content

Commit cfe6bd9

Browse files
committed
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.
1 parent ac58f58 commit cfe6bd9

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,11 @@ export class InstanceController {
410410

411411
public async deleteInstance({ instanceName }: InstanceDto) {
412412
const { instance } = await this.connectionState({ instanceName });
413-
414-
if (instance.state === 'open') {
415-
throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected');
416-
}
417413
try {
418414
const waInstances = this.waMonitor.waInstances[instanceName];
419415
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
420416

421-
if (instance.state === 'connecting') {
417+
if (instance.state === 'connecting' || instance.state === 'open') {
422418
await this.logout({ instanceName });
423419
}
424420

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ export class BaileysStartupService extends ChannelStartupService {
312312
instance: this.instance.name,
313313
state: 'refused',
314314
statusReason: DisconnectReason.connectionClosed,
315+
wuid: this.instance.wuid,
316+
profileName: await this.getProfileName(),
317+
profilePictureUrl: this.instance.profilePictureUrl,
315318
});
316319

317320
this.endSession = true;
@@ -391,11 +394,6 @@ export class BaileysStartupService extends ChannelStartupService {
391394
state: connection,
392395
statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200,
393396
};
394-
395-
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
396-
instance: this.instance.name,
397-
...this.stateConnection,
398-
});
399397
}
400398

401399
if (connection === 'close') {
@@ -437,6 +435,11 @@ export class BaileysStartupService extends ChannelStartupService {
437435
this.eventEmitter.emit('logout.instance', this.instance.name, 'inner');
438436
this.client?.ws?.close();
439437
this.client.end(new Error('Close connection'));
438+
439+
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
440+
instance: this.instance.name,
441+
...this.stateConnection,
442+
});
440443
}
441444
}
442445

@@ -484,6 +487,21 @@ export class BaileysStartupService extends ChannelStartupService {
484487
);
485488
this.syncChatwootLostMessages();
486489
}
490+
491+
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
492+
instance: this.instance.name,
493+
wuid: this.instance.wuid,
494+
profileName: await this.getProfileName(),
495+
profilePictureUrl: this.instance.profilePictureUrl,
496+
...this.stateConnection,
497+
});
498+
}
499+
500+
if (connection === 'connecting') {
501+
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
502+
instance: this.instance.name,
503+
...this.stateConnection,
504+
});
487505
}
488506
}
489507

src/validate/settings.schema.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ export const settingsSchema: JSONSchema7 = {
3333
syncFullHistory: { type: 'boolean' },
3434
wavoipToken: { type: 'string' },
3535
},
36-
required: [
37-
'rejectCall',
38-
'groupsIgnore',
39-
'alwaysOnline',
40-
'readMessages',
41-
'readStatus',
42-
'syncFullHistory',
43-
'wavoipToken',
44-
],
45-
...isNotEmpty(
46-
'rejectCall',
47-
'groupsIgnore',
48-
'alwaysOnline',
49-
'readMessages',
50-
'readStatus',
51-
'syncFullHistory',
52-
'wavoipToken',
53-
),
36+
required: ['rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'],
37+
...isNotEmpty('rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'),
5438
};

0 commit comments

Comments
 (0)