Fix and improve fetch chats

This commit is contained in:
Jesus 2024-10-07 18:03:27 +02:00
parent 96e1802148
commit 10aeeebb53

View File

@ -614,9 +614,7 @@ export class ChannelStartupService {
: this.createJid(query.where?.remoteJid) : this.createJid(query.where?.remoteJid)
: null; : null;
let result; const result = await this.prismaRepository.$queryRaw`
if (remoteJid) {
result = await this.prismaRepository.$queryRaw`
SELECT SELECT
"Chat"."id", "Chat"."id",
"Chat"."remoteJid", "Chat"."remoteJid",
@ -625,13 +623,13 @@ export class ChannelStartupService {
"Chat"."createdAt", "Chat"."createdAt",
"Chat"."updatedAt", "Chat"."updatedAt",
"Contact"."pushName", "Contact"."pushName",
"Contact"."profilePicUrl" "Contact"."profilePicUrl",
"Contact"."unreadMessages" "Contact"."unreadMessages"
FROM "Chat" FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
WHERE "Chat"."instanceId" = ${this.instanceId} WHERE "Chat"."instanceId" = ${this.instanceId}
AND "Chat"."remoteJid" = ${remoteJid} ${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
GROUP BY GROUP BY
"Chat"."id", "Chat"."id",
"Chat"."remoteJid", "Chat"."remoteJid",
@ -644,35 +642,6 @@ export class ChannelStartupService {
"Contact"."unreadMessages" "Contact"."unreadMessages"
ORDER BY "Chat"."updatedAt" DESC; ORDER BY "Chat"."updatedAt" DESC;
`; `;
} else {
result = await this.prismaRepository.$queryRaw`
SELECT
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."unreadMessages"
FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
WHERE "Chat"."instanceId" = ${this.instanceId}
GROUP BY
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."unreadMessages"
ORDER BY "Chat"."updatedAt" DESC;
`;
}
return result; return result;
} }