Skip to content

Commit 1242baa

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 ec7999b commit 1242baa

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
@@ -993,6 +998,8 @@ export class BaileysStartupService extends ChannelStartupService {
993998
await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true });
994999
}
9951000

1001+
this.historySyncChatCount += chatsRaw.length;
1002+
9961003
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
9971004

9981005
const messagesRaw: any[] = [];
@@ -1046,6 +1053,8 @@ export class BaileysStartupService extends ChannelStartupService {
10461053
messagesRaw.push(this.prepareMessage(m));
10471054
}
10481055

1056+
this.historySyncMessageCount += messagesRaw.length;
1057+
10491058
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
10501059
await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true });
10511060
}
@@ -1067,16 +1076,23 @@ export class BaileysStartupService extends ChannelStartupService {
10671076
);
10681077
}
10691078

1079+
const filteredContacts = contacts.filter((c) => !!c.notify || !!c.name);
1080+
this.historySyncContactCount += filteredContacts.length;
1081+
10701082
await this.contactHandle['contacts.upsert'](
1071-
contacts.filter((c) => !!c.notify || !!c.name).map((c) => ({ id: c.id, name: c.name ?? c.notify })),
1083+
filteredContacts.map((c) => ({ id: c.id, name: c.name ?? c.notify })),
10721084
);
10731085

10741086
if (progress === 100) {
10751087
this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, {
1076-
messageCount: messagesRaw.length,
1077-
chatCount: chatsRaw.length,
1078-
contactCount: contacts?.length ?? 0,
1088+
messageCount: this.historySyncMessageCount,
1089+
chatCount: this.historySyncChatCount,
1090+
contactCount: this.historySyncContactCount,
10791091
});
1092+
1093+
this.historySyncMessageCount = 0;
1094+
this.historySyncChatCount = 0;
1095+
this.historySyncContactCount = 0;
10801096
}
10811097

10821098
contacts = undefined;

0 commit comments

Comments
 (0)