Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(stats): bound messages by type to the selected period

Yudhi Armyndharis97b909e050bb
diff --git a/src/modules/stats/stats.service.spec.ts b/src/modules/stats/stats.service.spec.ts
index 132a67c50..48c94d9c7 100644
--- a/src/modules/stats/stats.service.spec.ts
+++ b/src/modules/stats/stats.service.spec.ts
@@ -116,6 +116,20 @@ describe('StatsService time-series + hourly activity on SQLite (end-to-end regre
expect(stats.byType).toEqual({ text: 1, image: 1 });
});
+ it('getMessageStats byType counts only rows inside the period, metadata-carrying rows included', async () => {
+ await ds
+ .getRepository(Session)
+ .save(ds.getRepository(Session).create({ id: 's1', name: 'n', status: SessionStatus.READY, config: {} }));
+ const oldImage = await seedMessage({ type: 'image', body: '', metadata: { media: { mimetype: 'image/png' } } });
+ const oldReply = await seedMessage({ body: '', metadata: { quotedMessageId: 'q1' } });
+ const old = new Date(Date.now() - 20 * 24 * 60 * 60 * 1000);
+ await ds.getRepository(Message).update([oldImage.id, oldReply.id], { createdAt: old });
+ await seedMessage({ type: 'image', body: '', metadata: { media: { mimetype: 'image/png' } } });
+
+ expect((await service.getMessageStats('24h')).byType).toEqual({ image: 1 });
+ expect((await service.getMessageStats('7d')).byType).toEqual({ image: 1 });
+ });
+
it('time-series query never groups by the bare reserved word `timestamp` (Postgres-safe)', async () => {
await ds
.getRepository(Session)
diff --git a/src/modules/stats/stats.service.ts b/src/modules/stats/stats.service.ts
index d42df09b5..31e8a4906 100644
--- a/src/modules/stats/stats.service.ts
+++ b/src/modules/stats/stats.service.ts
@@ -206,7 +206,8 @@ export class StatsService {
.select('m.type', 'type')
.addSelect('COUNT(*)', 'count')
.where('m.createdAt >= :since', { since })
- .andWhere("(m.body IS NOT NULL AND m.body != '') OR m.metadata IS NOT NULL")
+ // Parenthesized: TypeORM does not wrap an andWhere, so a bare OR would escape the period bound.
+ .andWhere("((m.body IS NOT NULL AND m.body != '') OR m.metadata IS NOT NULL)")
.groupBy('m.type')
.getRawMany<{ type: string; count: string }>();