mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
Add last_message to fetchChats query
This commit is contained in:
parent
eb456f6a88
commit
cada7be1e5
@ -14,6 +14,7 @@ import { Logger } from '@config/logger.config';
|
|||||||
import { NotFoundException } from '@exceptions';
|
import { NotFoundException } from '@exceptions';
|
||||||
import { Contact, Message } from '@prisma/client';
|
import { Contact, Message } from '@prisma/client';
|
||||||
import { WASocket } from 'baileys';
|
import { WASocket } from 'baileys';
|
||||||
|
import { isArray } from 'class-validator';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
@ -309,7 +310,9 @@ export class ChannelStartupService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ignoreJidsArray = Array.isArray(data.ignoreJids) ? data.ignoreJids.map((event) => String(event)) : [];
|
const ignoreJidsArray = Array.isArray(data.ignoreJids)
|
||||||
|
? data.ignoreJids.map((event) => String(event))
|
||||||
|
: [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enabled: data?.enabled,
|
enabled: data?.enabled,
|
||||||
@ -537,7 +540,9 @@ export class ChannelStartupService {
|
|||||||
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
||||||
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
||||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||||
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
keyFilters?.participants
|
||||||
|
? { key: { path: ['participants'], equals: keyFilters?.participants } }
|
||||||
|
: {},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -560,7 +565,9 @@ export class ChannelStartupService {
|
|||||||
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
||||||
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
||||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||||
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
keyFilters?.participants
|
||||||
|
? { key: { path: ['participants'], equals: keyFilters?.participants } }
|
||||||
|
: {},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
@ -615,33 +622,65 @@ export class ChannelStartupService {
|
|||||||
: null;
|
: null;
|
||||||
|
|
||||||
const result = await this.prismaRepository.$queryRaw`
|
const result = await this.prismaRepository.$queryRaw`
|
||||||
SELECT
|
SELECT
|
||||||
"Chat"."id",
|
"Chat"."id",
|
||||||
"Chat"."remoteJid",
|
"Chat"."remoteJid",
|
||||||
"Chat"."name",
|
"Chat"."name",
|
||||||
"Chat"."labels",
|
"Chat"."labels",
|
||||||
"Chat"."createdAt",
|
"Chat"."createdAt",
|
||||||
"Chat"."updatedAt",
|
"Chat"."updatedAt",
|
||||||
"Contact"."pushName",
|
"Contact"."pushName",
|
||||||
"Contact"."profilePicUrl",
|
"Contact"."profilePicUrl",
|
||||||
"Chat"."unreadMessages"
|
(ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
|
||||||
FROM "Chat"
|
(ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
|
||||||
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
|
(ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
|
||||||
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
|
(ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
|
||||||
WHERE "Chat"."instanceId" = ${this.instanceId}
|
(ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
|
||||||
${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
|
(ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
|
||||||
GROUP BY
|
(ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
|
||||||
"Chat"."id",
|
(ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
|
||||||
"Chat"."remoteJid",
|
(ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
|
||||||
"Chat"."name",
|
(ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
|
||||||
"Chat"."labels",
|
(ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
|
||||||
"Chat"."createdAt",
|
(ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
|
||||||
"Chat"."updatedAt",
|
FROM "Chat"
|
||||||
"Contact"."pushName",
|
LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
|
||||||
"Contact"."profilePicUrl",
|
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
|
||||||
"Chat"."unreadMessages"
|
WHERE
|
||||||
ORDER BY "Chat"."updatedAt" DESC;
|
"Chat"."instanceId" like ${this.instanceId}
|
||||||
`;
|
${remoteJid ? 'AND "Chat"."remoteJid" like ${remoteJid}' : ''}
|
||||||
|
GROUP BY
|
||||||
|
"Chat"."id",
|
||||||
|
"Chat"."remoteJid",
|
||||||
|
"Contact"."id"
|
||||||
|
ORDER BY "Chat"."updatedAt" DESC;
|
||||||
|
`.then((chats) => {
|
||||||
|
if (chats && isArray(chats) && chats.length > 0) {
|
||||||
|
return chats.map((chat) => {
|
||||||
|
return {
|
||||||
|
...chat,
|
||||||
|
lastMessage: chat.last_message_id
|
||||||
|
? {
|
||||||
|
id: chat.last_message_id,
|
||||||
|
key: chat.last_message_key,
|
||||||
|
pushName: chat.last_message_pushName,
|
||||||
|
participant: chat.last_message_participant,
|
||||||
|
messageType: chat.last_message_messageType,
|
||||||
|
message: chat.last_message_message,
|
||||||
|
contextInfo: chat.last_message_contextInfo,
|
||||||
|
source: chat.last_message_source,
|
||||||
|
messageTimestamp: chat.last_message_messageTimestamp,
|
||||||
|
instanceId: chat.last_message_instanceId,
|
||||||
|
sessionId: chat.last_message_sessionId,
|
||||||
|
status: chat.last_message_status,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user