Skip to content

Commit

Permalink
Chats filtering to support timestamp range queries
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
DavidsonGomes committed Jan 22, 2025
1 parent ab5eb80 commit 666c0b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/api/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 666c0b5

Please sign in to comment.