From c5c354ffe79b069a34d86dfb952126eabc8f78f7 Mon Sep 17 00:00:00 2001 From: pedro-php Date: Tue, 25 Mar 2025 10:52:19 -0300 Subject: [PATCH 1/2] fix_and_add_name_to_find_chats_and_paginate_get_contacts_and_get_chats --- .../whatsapp/whatsapp.baileys.service.ts | 3 ++- src/api/services/channel.service.ts | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 10feb7ce1..9c11527af 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1186,7 +1186,8 @@ export class BaileysStartupService extends ChannelStartupService { existingChat && received.pushName && existingChat.name !== received.pushName && - received.pushName.trim().length > 0 + received.pushName.trim().length > 0 && + !received.key.remoteJid.includes('@g.us') ) { this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 0f30f0c99..60126a377 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -503,9 +503,14 @@ export class ChannelStartupService { where['remoteJid'] = remoteJid; } - return await this.prismaRepository.contact.findMany({ + const contactFindManyArgs: Prisma.ContactFindManyArgs = { where, - }); + }; + + if (query.offset) contactFindManyArgs.take = query.offset; + if (query.page) contactFindManyArgs.skip = query.offset * ((query.page as number) - 1); + + return await this.prismaRepository.contact.findMany(contactFindManyArgs); } public cleanMessageData(message: any) { @@ -674,6 +679,13 @@ export class ChannelStartupService { : createJid(query.where?.remoteJid) : null; + const limit = + query.offset && !query.page + ? Prisma.sql` LIMIT ${query.offset}` + : query.offset && query.page + ? Prisma.sql` LIMIT ${query.offset} OFFSET ${((query.page as number) - 1) * query.offset}` + : Prisma.sql``; + const where = { instanceId: this.instanceId, }; @@ -700,6 +712,7 @@ export class ChannelStartupService { to_timestamp("Message"."messageTimestamp"::double precision), "Contact"."updatedAt" ) as "updatedAt", + "Chat"."name" as "chatName", "Chat"."createdAt" as "windowStart", "Chat"."createdAt" + INTERVAL '24 hours' as "windowExpires", CASE @@ -730,6 +743,7 @@ export class ChannelStartupService { ORDER BY "Contact"."remoteJid", "Message"."messageTimestamp" DESC + ${limit} ) SELECT * FROM rankedMessages ORDER BY "updatedAt" DESC NULLS LAST; @@ -758,6 +772,7 @@ export class ChannelStartupService { id: contact.id, remoteJid: contact.remoteJid, pushName: contact.pushName, + chatName: contact.chatName, profilePicUrl: contact.profilePicUrl, updatedAt: contact.updatedAt, windowStart: contact.windowStart, From d196590862228ce56c08ee3d17449c1ac41148ad Mon Sep 17 00:00:00 2001 From: Pedro Afonso <168743214+pedro-php@users.noreply.github.com> Date: Tue, 25 Mar 2025 11:17:46 -0300 Subject: [PATCH 2/2] Update src/api/services/channel.service.ts Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- src/api/services/channel.service.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 60126a377..6d03856ed 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -508,7 +508,10 @@ export class ChannelStartupService { }; if (query.offset) contactFindManyArgs.take = query.offset; - if (query.page) contactFindManyArgs.skip = query.offset * ((query.page as number) - 1); + if (query.page) { + const validPage = Math.max(query.page as number, 1); + contactFindManyArgs.skip = query.offset * (validPage - 1); + } return await this.prismaRepository.contact.findMany(contactFindManyArgs); }