@@ -9,6 +9,7 @@ import { Logger } from '@config/logger.config';
99import { NotFoundException } from '@exceptions' ;
1010import { Contact , Message } from '@prisma/client' ;
1111import { WASocket } from 'baileys' ;
12+ import { isArray } from 'class-validator' ;
1213import EventEmitter2 from 'eventemitter2' ;
1314import { v4 } from 'uuid' ;
1415
@@ -455,36 +456,112 @@ export class ChannelStartupService {
455456 : this . createJid ( query . where ?. remoteJid )
456457 : null ;
457458
458- const result = await this . prismaRepository . $queryRaw `
459- SELECT
460- "Chat"."id",
461- "Chat"."remoteJid",
462- "Chat"."name",
463- "Chat"."labels",
464- "Chat"."createdAt",
465- "Chat"."updatedAt",
466- "Contact"."pushName",
467- "Contact"."profilePicUrl",
468- "Contact"."unreadMessages"
469- FROM "Chat"
470- INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
471- LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
472- WHERE "Chat"."instanceId" = ${ this . instanceId }
473- ${ remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : '' }
474- AND "Chat"."remoteJid" = ${ remoteJid }
475- GROUP BY
476- "Chat"."id",
477- "Chat"."remoteJid",
478- "Chat"."name",
479- "Chat"."labels",
480- "Chat"."createdAt",
481- "Chat"."updatedAt",
482- "Contact"."pushName",
483- "Contact"."profilePicUrl",
484- "Contact"."unreadMessages"
485- ORDER BY "Chat"."updatedAt" DESC;
486- ` ;
487-
488- return result ;
459+ let results = [ ] ;
460+
461+ if ( ! remoteJid ) {
462+ results = await this . prismaRepository . $queryRaw `
463+ SELECT
464+ "Chat"."id",
465+ "Chat"."remoteJid",
466+ "Chat"."name",
467+ "Chat"."labels",
468+ "Chat"."createdAt",
469+ "Chat"."updatedAt",
470+ "Contact"."pushName",
471+ "Contact"."profilePicUrl",
472+ "Chat"."unreadMessages",
473+ (ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
474+ (ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
475+ (ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
476+ (ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
477+ (ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
478+ (ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
479+ (ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
480+ (ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
481+ (ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
482+ (ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
483+ (ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
484+ (ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
485+ FROM "Chat"
486+ LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
487+ LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
488+ WHERE
489+ "Chat"."instanceId" = ${ this . instanceId }
490+ GROUP BY
491+ "Chat"."id",
492+ "Chat"."remoteJid",
493+ "Contact"."id"
494+ ORDER BY last_message_messageTimestamp DESC NULLS LAST, "Chat"."updatedAt" DESC;
495+ ` ;
496+ } else {
497+ results = await this . prismaRepository . $queryRaw `
498+ SELECT
499+ "Chat"."id",
500+ "Chat"."remoteJid",
501+ "Chat"."name",
502+ "Chat"."labels",
503+ "Chat"."createdAt",
504+ "Chat"."updatedAt",
505+ "Contact"."pushName",
506+ "Contact"."profilePicUrl",
507+ "Chat"."unreadMessages",
508+ (ARRAY_AGG("Message"."id" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_id,
509+ (ARRAY_AGG("Message"."key" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_key,
510+ (ARRAY_AGG("Message"."pushName" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_pushName,
511+ (ARRAY_AGG("Message"."participant" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_participant,
512+ (ARRAY_AGG("Message"."messageType" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageType,
513+ (ARRAY_AGG("Message"."message" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_message,
514+ (ARRAY_AGG("Message"."contextInfo" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_contextInfo,
515+ (ARRAY_AGG("Message"."source" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_source,
516+ (ARRAY_AGG("Message"."messageTimestamp" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_messageTimestamp,
517+ (ARRAY_AGG("Message"."instanceId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_instanceId,
518+ (ARRAY_AGG("Message"."sessionId" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_sessionId,
519+ (ARRAY_AGG("Message"."status" ORDER BY "Message"."messageTimestamp" DESC))[1] AS last_message_status
520+ FROM "Chat"
521+ LEFT JOIN "Message" ON "Message"."key"->>'remoteJid' = "Chat"."remoteJid"
522+ LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
523+ WHERE
524+ "Chat"."instanceId" = ${ this . instanceId } AND "Chat"."remoteJid" = ${ remoteJid }
525+ GROUP BY
526+ "Chat"."id",
527+ "Chat"."remoteJid",
528+ "Contact"."id"
529+ ORDER BY last_message_messageTimestamp DESC NULLS LAST, "Chat"."updatedAt" DESC;
530+ ` ;
531+ }
532+
533+ if ( results && isArray ( results ) && results . length > 0 ) {
534+ return results . map ( ( chat ) => {
535+ return {
536+ id : chat . id ,
537+ remoteJid : chat . remoteJid ,
538+ name : chat . name ,
539+ labels : chat . labels ,
540+ createdAt : chat . createdAt ,
541+ updatedAt : chat . updatedAt ,
542+ pushName : chat . pushName ,
543+ profilePicUrl : chat . profilePicUrl ,
544+ unreadMessages : chat . unreadMessages ,
545+ lastMessage : chat . last_message_id
546+ ? {
547+ id : chat . last_message_id ,
548+ key : chat . last_message_key ,
549+ pushName : chat . last_message_pushName ,
550+ participant : chat . last_message_participant ,
551+ messageType : chat . last_message_messageType ,
552+ message : chat . last_message_message ,
553+ contextInfo : chat . last_message_contextInfo ,
554+ source : chat . last_message_source ,
555+ messageTimestamp : chat . last_message_messageTimestamp ,
556+ instanceId : chat . last_message_instanceId ,
557+ sessionId : chat . last_message_sessionId ,
558+ status : chat . last_message_status ,
559+ }
560+ : undefined ,
561+ } ;
562+ } ) ;
563+ }
564+
565+ return [ ] ;
489566 }
490567}
0 commit comments