From 666c0b514d6222f789f0b1bc70b8058171ea2077 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 22 Jan 2025 10:22:09 -0300 Subject: [PATCH] Chats filtering to support timestamp range queries - Introduced a new `timestampFilter` to allow filtering messages based on `messageTimestamp` within specified `gte` and `lte` ranges. - Updated SQL query logic in `ChannelStartupService` to incorporate the timestamp filtering, improving message retrieval accuracy. --- CHANGELOG.md | 3 ++- src/api/services/channel.service.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33a9cbae..9714cf32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ### Features * Retry system for send webhooks -* Enhance message filtering to support timestamp range queries +* Message filtering to support timestamp range queries +* Chats filtering to support timestamp range queries ### Fixed diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index d2a4d8b1..9f1a520a 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -682,6 +682,13 @@ export class ChannelStartupService { where['remoteJid'] = remoteJid; } + const timestampFilter = + query?.where?.messageTimestamp?.gte && query?.where?.messageTimestamp?.lte + ? Prisma.sql` + AND "Message"."messageTimestamp" >= ${Math.floor(new Date(query.where.messageTimestamp.gte).getTime() / 1000)} + AND "Message"."messageTimestamp" <= ${Math.floor(new Date(query.where.messageTimestamp.lte).getTime() / 1000)}` + : Prisma.sql``; + const results = await this.prismaRepository.$queryRaw` WITH rankedMessages AS ( SELECT DISTINCT ON ("Contact"."remoteJid") @@ -719,6 +726,7 @@ export class ChannelStartupService { "Contact"."instanceId" = ${this.instanceId} AND "Message"."instanceId" = ${this.instanceId} ${remoteJid ? Prisma.sql`AND "Contact"."remoteJid" = ${remoteJid}` : Prisma.sql``} + ${timestampFilter} ORDER BY "Contact"."remoteJid", "Message"."messageTimestamp" DESC