fix(history-sync): use cumulative counts in MESSAGING_HISTORY_SET event

Track message, chat and contact counts across all history sync batches
using instance-level counters, so the final event reports accurate
totals instead of only the last batch counts.

Addresses Sourcery review feedback on PR #2440.
This commit is contained in:
Alexandre Reyes Martins
2026-02-23 21:48:30 +00:00
parent ec7999b04f
commit 1242baa5a4
@@ -252,6 +252,11 @@ export class BaileysStartupService extends ChannelStartupService {
private logBaileys = this.configService.get<Log>('LOG').BAILEYS; private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
private eventProcessingQueue: Promise<void> = Promise.resolve(); private eventProcessingQueue: Promise<void> = Promise.resolve();
// Cumulative history sync counters (reset on sync completion)
private historySyncMessageCount = 0;
private historySyncChatCount = 0;
private historySyncContactCount = 0;
// Cache TTL constants (in seconds) // Cache TTL constants (in seconds)
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates
@@ -993,6 +998,8 @@ export class BaileysStartupService extends ChannelStartupService {
await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true }); await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true });
} }
this.historySyncChatCount += chatsRaw.length;
this.sendDataWebhook(Events.CHATS_SET, chatsRaw); this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
const messagesRaw: any[] = []; const messagesRaw: any[] = [];
@@ -1046,6 +1053,8 @@ export class BaileysStartupService extends ChannelStartupService {
messagesRaw.push(this.prepareMessage(m)); messagesRaw.push(this.prepareMessage(m));
} }
this.historySyncMessageCount += messagesRaw.length;
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) { if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true }); await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true });
} }
@@ -1067,16 +1076,23 @@ export class BaileysStartupService extends ChannelStartupService {
); );
} }
const filteredContacts = contacts.filter((c) => !!c.notify || !!c.name);
this.historySyncContactCount += filteredContacts.length;
await this.contactHandle['contacts.upsert']( await this.contactHandle['contacts.upsert'](
contacts.filter((c) => !!c.notify || !!c.name).map((c) => ({ id: c.id, name: c.name ?? c.notify })), filteredContacts.map((c) => ({ id: c.id, name: c.name ?? c.notify })),
); );
if (progress === 100) { if (progress === 100) {
this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, { this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, {
messageCount: messagesRaw.length, messageCount: this.historySyncMessageCount,
chatCount: chatsRaw.length, chatCount: this.historySyncChatCount,
contactCount: contacts?.length ?? 0, contactCount: this.historySyncContactCount,
}); });
this.historySyncMessageCount = 0;
this.historySyncChatCount = 0;
this.historySyncContactCount = 0;
} }
contacts = undefined; contacts = undefined;