Merge pull request #739 from fmedeiros95/v2.0.0

Improve reconnection logic for ban and block cases and add disconnection information to BaileysStartupService and Instance model
This commit is contained in:
Davidson Gomes 2024-08-06 15:03:11 -03:00 committed by GitHub
commit d0a723b8ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 67 deletions

View File

@ -57,6 +57,9 @@ model Instance {
number String? @db.VarChar(100) number String? @db.VarChar(100)
token String? @unique @db.VarChar(255) token String? @unique @db.VarChar(255)
clientName String? @db.VarChar(100) clientName String? @db.VarChar(100)
disconnectionReasonCode Int? @db.Int
disconnectionObject Json? @db.Json
disconnectionAt DateTime? @db.Timestamp
createdAt DateTime? @default(now()) @db.Timestamp createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp
Chat Chat[] Chat Chat[]

View File

@ -71,6 +71,9 @@ model Instance {
businessId String? @db.VarChar(100) businessId String? @db.VarChar(100)
token String? @db.VarChar(255) token String? @db.VarChar(255)
clientName String? @db.VarChar(100) clientName String? @db.VarChar(100)
disconnectionReasonCode Int? @db.Integer
disconnectionObject Json? @db.JsonB
disconnectionAt DateTime? @db.Timestamp
createdAt DateTime? @default(now()) @db.Timestamp createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp
Chat Chat[] Chat Chat[]

View File

@ -388,13 +388,18 @@ export class BaileysStartupService extends ChannelStartupService {
} }
if (connection === 'close') { if (connection === 'close') {
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut; const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
if (shouldReconnect) { if (shouldReconnect) {
await this.connectToWhatsapp(this.phoneNumber); await this.connectToWhatsapp(this.phoneNumber);
} else { } else {
this.sendDataWebhook(Events.STATUS_INSTANCE, { this.sendDataWebhook(Events.STATUS_INSTANCE, {
instance: this.instance.name, instance: this.instance.name,
status: 'closed', status: 'closed',
disconnectionAt: new Date(),
disconnectionReasonCode: statusCode,
disconnectionObject: JSON.stringify(lastDisconnect),
}); });
if (this.configService.get<Database>('DATABASE').ENABLED) { if (this.configService.get<Database>('DATABASE').ENABLED) {
@ -402,6 +407,9 @@ export class BaileysStartupService extends ChannelStartupService {
where: { id: this.instanceId }, where: { id: this.instanceId },
data: { data: {
connectionStatus: 'close', connectionStatus: 'close',
disconnectionAt: new Date(),
disconnectionReasonCode: statusCode,
disconnectionObject: JSON.stringify(lastDisconnect),
}, },
}); });
} }