@@ -14,6 +14,7 @@ import { Logger } from '@config/logger.config';
1414import { NotFoundException } from '@exceptions' ;
1515import { Contact , Message } from '@prisma/client' ;
1616import { WASocket } from 'baileys' ;
17+ import { isArray } from 'class-validator' ;
1718import EventEmitter2 from 'eventemitter2' ;
1819import { v4 } from 'uuid' ;
1920
@@ -309,7 +310,9 @@ export class ChannelStartupService {
309310 return null ;
310311 }
311312
312- const ignoreJidsArray = Array . isArray ( data . ignoreJids ) ? data . ignoreJids . map ( ( event ) => String ( event ) ) : [ ] ;
313+ const ignoreJidsArray = Array . isArray ( data . ignoreJids )
314+ ? data . ignoreJids . map ( ( event ) => String ( event ) )
315+ : [ ] ;
313316
314317 return {
315318 enabled : data ?. enabled ,
@@ -537,7 +540,9 @@ export class ChannelStartupService {
537540 keyFilters ?. id ? { key : { path : [ 'id' ] , equals : keyFilters ?. id } } : { } ,
538541 keyFilters ?. fromMe ? { key : { path : [ 'fromMe' ] , equals : keyFilters ?. fromMe } } : { } ,
539542 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
540- keyFilters ?. participants ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } } : { } ,
543+ keyFilters ?. participants
544+ ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } }
545+ : { } ,
541546 ] ,
542547 } ,
543548 } ) ;
@@ -560,7 +565,9 @@ export class ChannelStartupService {
560565 keyFilters ?. id ? { key : { path : [ 'id' ] , equals : keyFilters ?. id } } : { } ,
561566 keyFilters ?. fromMe ? { key : { path : [ 'fromMe' ] , equals : keyFilters ?. fromMe } } : { } ,
562567 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
563- keyFilters ?. participants ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } } : { } ,
568+ keyFilters ?. participants
569+ ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } }
570+ : { } ,
564571 ] ,
565572 } ,
566573 orderBy : {
@@ -615,33 +622,65 @@ export class ChannelStartupService {
615622 : null ;
616623
617624 const result = await this . prismaRepository . $queryRaw `
618- SELECT
619- "Chat"."id",
620- "Chat"."remoteJid",
621- "Chat"."name",
622- "Chat"."labels",
623- "Chat"."createdAt",
624- "Chat"."updatedAt",
625- "Contact"."pushName",
626- "Contact"."profilePicUrl",
627- "Chat"."unreadMessages"
628- FROM "Chat"
629- INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
630- LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
631- WHERE "Chat"."instanceId" = ${ this . instanceId }
632- ${ remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : '' }
633- GROUP BY
634- "Chat"."id",
635- "Chat"."remoteJid",
636- "Chat"."name",
637- "Chat"."labels",
638- "Chat"."createdAt",
639- "Chat"."updatedAt",
640- "Contact"."pushName",
641- "Contact"."profilePicUrl",
642- "Chat"."unreadMessages"
643- ORDER BY "Chat"."updatedAt" DESC;
644- ` ;
625+ SELECT
626+ "Chat"."id",
627+ "Chat"."remoteJid",
628+ "Chat"."name",
629+ "Chat"."labels",
630+ "Chat"."createdAt",
631+ "Chat"."updatedAt",
632+ "Contact"."pushName",
633+ "Contact"."profilePicUrl",
634+ (ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
635+ (ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
636+ (ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
637+ (ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
638+ (ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
639+ (ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
640+ (ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
641+ (ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
642+ (ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
643+ (ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
644+ (ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
645+ (ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
646+ FROM "Chat"
647+ LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
648+ LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
649+ WHERE
650+ "Chat"."instanceId" like ${ this . instanceId }
651+ ${ remoteJid ? 'AND "Chat"."remoteJid" like ${remoteJid}' : '' }
652+ GROUP BY
653+ "Chat"."id",
654+ "Chat"."remoteJid",
655+ "Contact"."id"
656+ ORDER BY "Chat"."updatedAt" DESC;
657+ ` . then ( ( chats ) => {
658+ if ( chats && isArray ( chats ) && chats . length > 0 ) {
659+ return chats . map ( ( chat ) => {
660+ return {
661+ ...chat ,
662+ lastMessage : chat . last_message_id
663+ ? {
664+ id : chat . last_message_id ,
665+ key : chat . last_message_key ,
666+ pushName : chat . last_message_pushName ,
667+ participant : chat . last_message_participant ,
668+ messageType : chat . last_message_messageType ,
669+ message : chat . last_message_message ,
670+ contextInfo : chat . last_message_contextInfo ,
671+ source : chat . last_message_source ,
672+ messageTimestamp : chat . last_message_messageTimestamp ,
673+ instanceId : chat . last_message_instanceId ,
674+ sessionId : chat . last_message_sessionId ,
675+ status : chat . last_message_status ,
676+ }
677+ : undefined ,
678+ } ;
679+ } ) ;
680+ }
681+
682+ return [ ] ;
683+ } ) ;
645684
646685 return result ;
647686 }
0 commit comments