@@ -310,9 +310,7 @@ export class ChannelStartupService {
310310 return null ;
311311 }
312312
313- const ignoreJidsArray = Array . isArray ( data . ignoreJids )
314- ? data . ignoreJids . map ( ( event ) => String ( event ) )
315- : [ ] ;
313+ const ignoreJidsArray = Array . isArray ( data . ignoreJids ) ? data . ignoreJids . map ( ( event ) => String ( event ) ) : [ ] ;
316314
317315 return {
318316 enabled : data ?. enabled ,
@@ -540,9 +538,7 @@ export class ChannelStartupService {
540538 keyFilters ?. id ? { key : { path : [ 'id' ] , equals : keyFilters ?. id } } : { } ,
541539 keyFilters ?. fromMe ? { key : { path : [ 'fromMe' ] , equals : keyFilters ?. fromMe } } : { } ,
542540 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
543- keyFilters ?. participants
544- ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } }
545- : { } ,
541+ keyFilters ?. participants ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } } : { } ,
546542 ] ,
547543 } ,
548544 } ) ;
@@ -565,9 +561,7 @@ export class ChannelStartupService {
565561 keyFilters ?. id ? { key : { path : [ 'id' ] , equals : keyFilters ?. id } } : { } ,
566562 keyFilters ?. fromMe ? { key : { path : [ 'fromMe' ] , equals : keyFilters ?. fromMe } } : { } ,
567563 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
568- keyFilters ?. participants
569- ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } }
570- : { } ,
564+ keyFilters ?. participants ? { key : { path : [ 'participants' ] , equals : keyFilters ?. participants } } : { } ,
571565 ] ,
572566 } ,
573567 orderBy : {
@@ -621,67 +615,114 @@ export class ChannelStartupService {
621615 : this . createJid ( query . where ?. remoteJid )
622616 : null ;
623617
624- const result = await this . prismaRepository . $queryRaw `
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- }
618+ let results = [ ] ;
619+
620+ if ( ! remoteJid ) {
621+ results = await this . prismaRepository . $queryRaw `
622+ SELECT
623+ "Chat"."id",
624+ "Chat"."remoteJid",
625+ "Chat"."name",
626+ "Chat"."labels",
627+ "Chat"."createdAt",
628+ "Chat"."updatedAt",
629+ "Contact"."pushName",
630+ "Contact"."profilePicUrl",
631+ "Chat"."unreadMessages",
632+ (ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
633+ (ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
634+ (ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
635+ (ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
636+ (ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
637+ (ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
638+ (ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
639+ (ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
640+ (ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
641+ (ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
642+ (ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
643+ (ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
644+ FROM "Chat"
645+ LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
646+ LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
647+ WHERE
648+ "Chat"."instanceId" = ${ this . instanceId }
649+ GROUP BY
650+ "Chat"."id",
651+ "Chat"."remoteJid",
652+ "Contact"."id",
653+ "Message"."messageTimestamp"
654+ ORDER BY "Message"."messageTimestamp" DESC NULLS LAST, "Chat"."updatedAt" DESC;
655+ ` ;
656+ } else {
657+ results = await this . prismaRepository . $queryRaw `
658+ SELECT
659+ "Chat"."id",
660+ "Chat"."remoteJid",
661+ "Chat"."name",
662+ "Chat"."labels",
663+ "Chat"."createdAt",
664+ "Chat"."updatedAt",
665+ "Contact"."pushName",
666+ "Contact"."profilePicUrl",
667+ "Chat"."unreadMessages",
668+ (ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
669+ (ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
670+ (ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
671+ (ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
672+ (ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
673+ (ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
674+ (ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
675+ (ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
676+ (ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
677+ (ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
678+ (ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
679+ (ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
680+ FROM "Chat"
681+ LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
682+ LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
683+ WHERE
684+ "Chat"."instanceId" = ${ this . instanceId } AND "Chat"."remoteJid" = ${ remoteJid }
685+ GROUP BY
686+ "Chat"."id",
687+ "Chat"."remoteJid",
688+ "Contact"."id",
689+ "Message"."messageTimestamp"
690+ ORDER BY "Message"."messageTimestamp" DESC NULLS LAST, "Chat"."updatedAt" DESC;
691+ ` ;
692+ }
681693
682- return [ ] ;
683- } ) ;
694+ if ( results && isArray ( results ) && results . length > 0 ) {
695+ return results . map ( ( chat ) => {
696+ return {
697+ id : chat . id ,
698+ remoteJid : chat . remoteJid ,
699+ name : chat . name ,
700+ labels : chat . labels ,
701+ createdAt : chat . createdAt ,
702+ updatedAt : chat . updatedAt ,
703+ pushName : chat . pushName ,
704+ profilePicUrl : chat . profilePicUrl ,
705+ unreadMessages : chat . unreadMessages ,
706+ lastMessage : chat . last_message_id
707+ ? {
708+ id : chat . last_message_id ,
709+ key : chat . last_message_key ,
710+ pushName : chat . last_message_pushName ,
711+ participant : chat . last_message_participant ,
712+ messageType : chat . last_message_messageType ,
713+ message : chat . last_message_message ,
714+ contextInfo : chat . last_message_contextInfo ,
715+ source : chat . last_message_source ,
716+ messageTimestamp : chat . last_message_messageTimestamp ,
717+ instanceId : chat . last_message_instanceId ,
718+ sessionId : chat . last_message_sessionId ,
719+ status : chat . last_message_status ,
720+ }
721+ : undefined ,
722+ } ;
723+ } ) ;
724+ }
684725
685- return result ;
726+ return [ ] ;
686727 }
687728}
0 commit comments