diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 1c9bd232..4b5a115b 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -252,10 +252,11 @@ export class BaileysStartupService extends ChannelStartupService { private logBaileys = this.configService.get('LOG').BAILEYS; private eventProcessingQueue: Promise = Promise.resolve(); - // Cumulative history sync counters (reset on sync completion) + // Cumulative history sync counters (reset on new sync or completion) private historySyncMessageCount = 0; private historySyncChatCount = 0; private historySyncContactCount = 0; + private historySyncLastProgress = -1; // Cache TTL constants (in seconds) private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing @@ -945,6 +946,14 @@ export class BaileysStartupService extends ChannelStartupService { syncType?: proto.HistorySync.HistorySyncType; }) => { try { + // Reset counters when a new sync starts (progress resets or decreases) + if (progress <= this.historySyncLastProgress) { + this.historySyncMessageCount = 0; + this.historySyncChatCount = 0; + this.historySyncContactCount = 0; + } + this.historySyncLastProgress = progress ?? -1; + if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) { console.log('received on-demand history sync, messages=', messages); } @@ -1093,6 +1102,7 @@ export class BaileysStartupService extends ChannelStartupService { this.historySyncMessageCount = 0; this.historySyncChatCount = 0; this.historySyncContactCount = 0; + this.historySyncLastProgress = -1; } contacts = undefined;