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