Skip to content

Commit 58eeaf6

Browse files
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.
1 parent 30ee62c commit 58eeaf6

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ export class BaileysStartupService extends ChannelStartupService {
252252
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
253253
private eventProcessingQueue: Promise<void> = Promise.resolve();
254254

255+
// Cumulative history sync counters (reset on sync completion)
256+
private historySyncMessageCount = 0;
257+
private historySyncChatCount = 0;
258+
private historySyncContactCount = 0;
259+
255260
// Cache TTL constants (in seconds)
256261
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
257262
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates
@@ -997,6 +1002,8 @@ export class BaileysStartupService extends ChannelStartupService {
9971002
await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true });
9981003
}
9991004

1005+
this.historySyncChatCount += chatsRaw.length;
1006+
10001007
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
10011008

10021009
const messagesRaw: any[] = [];
@@ -1050,6 +1057,8 @@ export class BaileysStartupService extends ChannelStartupService {
10501057
messagesRaw.push(this.prepareMessage(m));
10511058
}
10521059

1060+
this.historySyncMessageCount += messagesRaw.length;
1061+
10531062
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
10541063
await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true });
10551064
}
@@ -1071,16 +1080,23 @@ export class BaileysStartupService extends ChannelStartupService {
10711080
);
10721081
}
10731082

1083+
const filteredContacts = contacts.filter((c) => !!c.notify || !!c.name);
1084+
this.historySyncContactCount += filteredContacts.length;
1085+
10741086
await this.contactHandle['contacts.upsert'](
1075-
contacts.filter((c) => !!c.notify || !!c.name).map((c) => ({ id: c.id, name: c.name ?? c.notify })),
1087+
filteredContacts.map((c) => ({ id: c.id, name: c.name ?? c.notify })),
10761088
);
10771089

10781090
if (progress === 100) {
10791091
this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, {
1080-
messageCount: messagesRaw.length,
1081-
chatCount: chatsRaw.length,
1082-
contactCount: contacts?.length ?? 0,
1092+
messageCount: this.historySyncMessageCount,
1093+
chatCount: this.historySyncChatCount,
1094+
contactCount: this.historySyncContactCount,
10831095
});
1096+
1097+
this.historySyncMessageCount = 0;
1098+
this.historySyncChatCount = 0;
1099+
this.historySyncContactCount = 0;
10841100
}
10851101

10861102
contacts = undefined;

0 commit comments

Comments
 (0)