mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
Added last message to fetch chats
This commit is contained in:
parent
c6ee463dae
commit
73e732bf24
@ -310,9 +310,7 @@ export class ChannelStartupService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ignoreJidsArray = Array.isArray(data.ignoreJids)
|
const ignoreJidsArray = Array.isArray(data.ignoreJids) ? data.ignoreJids.map((event) => String(event)) : [];
|
||||||
? data.ignoreJids.map((event) => String(event))
|
|
||||||
: [];
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enabled: data?.enabled,
|
enabled: data?.enabled,
|
||||||
@ -540,9 +538,7 @@ 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
|
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
||||||
? { key: { path: ['participants'], equals: keyFilters?.participants } }
|
|
||||||
: {},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -565,9 +561,7 @@ 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
|
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
||||||
? { key: { path: ['participants'], equals: keyFilters?.participants } }
|
|
||||||
: {},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
@ -621,7 +615,10 @@ export class ChannelStartupService {
|
|||||||
: this.createJid(query.where?.remoteJid)
|
: this.createJid(query.where?.remoteJid)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const result = await this.prismaRepository.$queryRaw`
|
let results = [];
|
||||||
|
|
||||||
|
if (!remoteJid) {
|
||||||
|
results = await this.prismaRepository.$queryRaw`
|
||||||
SELECT
|
SELECT
|
||||||
"Chat"."id",
|
"Chat"."id",
|
||||||
"Chat"."remoteJid",
|
"Chat"."remoteJid",
|
||||||
@ -631,6 +628,7 @@ export class ChannelStartupService {
|
|||||||
"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,
|
(ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
|
||||||
(ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
|
(ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
|
||||||
(ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
|
(ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
|
||||||
@ -647,18 +645,64 @@ export class ChannelStartupService {
|
|||||||
LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
|
LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
|
||||||
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
|
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
|
||||||
WHERE
|
WHERE
|
||||||
"Chat"."instanceId" like ${this.instanceId}
|
"Chat"."instanceId" = ${this.instanceId}
|
||||||
${remoteJid ? 'AND "Chat"."remoteJid" like ${remoteJid}' : ''}
|
|
||||||
GROUP BY
|
GROUP BY
|
||||||
"Chat"."id",
|
"Chat"."id",
|
||||||
"Chat"."remoteJid",
|
"Chat"."remoteJid",
|
||||||
"Contact"."id"
|
"Contact"."id",
|
||||||
ORDER BY "Chat"."updatedAt" DESC;
|
"Message"."messageTimestamp"
|
||||||
`.then((chats) => {
|
ORDER BY "Message"."messageTimestamp" DESC NULLS LAST, "Chat"."updatedAt" DESC;
|
||||||
if (chats && isArray(chats) && chats.length > 0) {
|
`;
|
||||||
return chats.map((chat) => {
|
} else {
|
||||||
|
results = await this.prismaRepository.$queryRaw`
|
||||||
|
SELECT
|
||||||
|
"Chat"."id",
|
||||||
|
"Chat"."remoteJid",
|
||||||
|
"Chat"."name",
|
||||||
|
"Chat"."labels",
|
||||||
|
"Chat"."createdAt",
|
||||||
|
"Chat"."updatedAt",
|
||||||
|
"Contact"."pushName",
|
||||||
|
"Contact"."profilePicUrl",
|
||||||
|
"Chat"."unreadMessages",
|
||||||
|
(ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
|
||||||
|
(ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
|
||||||
|
(ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
|
||||||
|
(ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
|
||||||
|
(ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
|
||||||
|
(ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
|
||||||
|
(ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
|
||||||
|
(ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
|
||||||
|
(ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
|
||||||
|
(ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
|
||||||
|
(ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
|
||||||
|
(ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
|
||||||
|
FROM "Chat"
|
||||||
|
LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
|
||||||
|
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
|
||||||
|
WHERE
|
||||||
|
"Chat"."instanceId" = ${this.instanceId} AND "Chat"."remoteJid" = ${remoteJid}
|
||||||
|
GROUP BY
|
||||||
|
"Chat"."id",
|
||||||
|
"Chat"."remoteJid",
|
||||||
|
"Contact"."id",
|
||||||
|
"Message"."messageTimestamp"
|
||||||
|
ORDER BY "Message"."messageTimestamp" DESC NULLS LAST, "Chat"."updatedAt" DESC;
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (results && isArray(results) && results.length > 0) {
|
||||||
|
return results.map((chat) => {
|
||||||
return {
|
return {
|
||||||
...chat,
|
id: chat.id,
|
||||||
|
remoteJid: chat.remoteJid,
|
||||||
|
name: chat.name,
|
||||||
|
labels: chat.labels,
|
||||||
|
createdAt: chat.createdAt,
|
||||||
|
updatedAt: chat.updatedAt,
|
||||||
|
pushName: chat.pushName,
|
||||||
|
profilePicUrl: chat.profilePicUrl,
|
||||||
|
unreadMessages: chat.unreadMessages,
|
||||||
lastMessage: chat.last_message_id
|
lastMessage: chat.last_message_id
|
||||||
? {
|
? {
|
||||||
id: chat.last_message_id,
|
id: chat.last_message_id,
|
||||||
@ -680,8 +724,5 @@ export class ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user