Skip to content

Commit 10aeeeb

Browse files
author
Jesus
committed
Fix and improve fetch chats
1 parent 96e1802 commit 10aeeeb

File tree

1 file changed

+3
-34
lines changed

1 file changed

+3
-34
lines changed

src/api/services/channel.service.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -614,25 +614,8 @@ export class ChannelStartupService {
614614
: this.createJid(query.where?.remoteJid)
615615
: null;
616616

617-
let result;
618-
if (remoteJid) {
619-
result = await this.prismaRepository.$queryRaw`
617+
const result = await this.prismaRepository.$queryRaw`
620618
SELECT
621-
"Chat"."id",
622-
"Chat"."remoteJid",
623-
"Chat"."name",
624-
"Chat"."labels",
625-
"Chat"."createdAt",
626-
"Chat"."updatedAt",
627-
"Contact"."pushName",
628-
"Contact"."profilePicUrl"
629-
"Contact"."unreadMessages"
630-
FROM "Chat"
631-
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
632-
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
633-
WHERE "Chat"."instanceId" = ${this.instanceId}
634-
AND "Chat"."remoteJid" = ${remoteJid}
635-
GROUP BY
636619
"Chat"."id",
637620
"Chat"."remoteJid",
638621
"Chat"."name",
@@ -642,24 +625,11 @@ export class ChannelStartupService {
642625
"Contact"."pushName",
643626
"Contact"."profilePicUrl",
644627
"Contact"."unreadMessages"
645-
ORDER BY "Chat"."updatedAt" DESC;
646-
`;
647-
} else {
648-
result = await this.prismaRepository.$queryRaw`
649-
SELECT
650-
"Chat"."id",
651-
"Chat"."remoteJid",
652-
"Chat"."name",
653-
"Chat"."labels",
654-
"Chat"."createdAt",
655-
"Chat"."updatedAt",
656-
"Contact"."pushName",
657-
"Contact"."profilePicUrl"
658-
"Contact"."unreadMessages"
659628
FROM "Chat"
660629
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
661630
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
662631
WHERE "Chat"."instanceId" = ${this.instanceId}
632+
${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
663633
GROUP BY
664634
"Chat"."id",
665635
"Chat"."remoteJid",
@@ -668,11 +638,10 @@ export class ChannelStartupService {
668638
"Chat"."createdAt",
669639
"Chat"."updatedAt",
670640
"Contact"."pushName",
671-
"Contact"."profilePicUrl"
641+
"Contact"."profilePicUrl",
672642
"Contact"."unreadMessages"
673643
ORDER BY "Chat"."updatedAt" DESC;
674644
`;
675-
}
676645

677646
return result;
678647
}

0 commit comments

Comments
 (0)