Skip to content

Commit b89f114

Browse files
Merge pull request #1334 from adaptwebtech/fix_and_add_name_to_find_chats_and_paginate_get_contacts_and_get_chats
Corrigindo um bug no endpoint de findChats e permitindo paginação nos endpoints de findChats e findContacts
2 parents 043df62 + d196590 commit b89f114

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ export class BaileysStartupService extends ChannelStartupService {
12231223
existingChat &&
12241224
received.pushName &&
12251225
existingChat.name !== received.pushName &&
1226-
received.pushName.trim().length > 0
1226+
received.pushName.trim().length > 0 &&
1227+
!received.key.remoteJid.includes('@g.us')
12271228
) {
12281229
this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]);
12291230
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {

src/api/services/channel.service.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,17 @@ export class ChannelStartupService {
503503
where['remoteJid'] = remoteJid;
504504
}
505505

506-
return await this.prismaRepository.contact.findMany({
506+
const contactFindManyArgs: Prisma.ContactFindManyArgs = {
507507
where,
508-
});
508+
};
509+
510+
if (query.offset) contactFindManyArgs.take = query.offset;
511+
if (query.page) {
512+
const validPage = Math.max(query.page as number, 1);
513+
contactFindManyArgs.skip = query.offset * (validPage - 1);
514+
}
515+
516+
return await this.prismaRepository.contact.findMany(contactFindManyArgs);
509517
}
510518

511519
public cleanMessageData(message: any) {
@@ -674,6 +682,13 @@ export class ChannelStartupService {
674682
: createJid(query.where?.remoteJid)
675683
: null;
676684

685+
const limit =
686+
query.offset && !query.page
687+
? Prisma.sql` LIMIT ${query.offset}`
688+
: query.offset && query.page
689+
? Prisma.sql` LIMIT ${query.offset} OFFSET ${((query.page as number) - 1) * query.offset}`
690+
: Prisma.sql``;
691+
677692
const where = {
678693
instanceId: this.instanceId,
679694
};
@@ -700,6 +715,7 @@ export class ChannelStartupService {
700715
to_timestamp("Message"."messageTimestamp"::double precision),
701716
"Contact"."updatedAt"
702717
) as "updatedAt",
718+
"Chat"."name" as "chatName",
703719
"Chat"."createdAt" as "windowStart",
704720
"Chat"."createdAt" + INTERVAL '24 hours' as "windowExpires",
705721
CASE
@@ -730,6 +746,7 @@ export class ChannelStartupService {
730746
ORDER BY
731747
"Contact"."remoteJid",
732748
"Message"."messageTimestamp" DESC
749+
${limit}
733750
)
734751
SELECT * FROM rankedMessages
735752
ORDER BY "updatedAt" DESC NULLS LAST;
@@ -758,6 +775,7 @@ export class ChannelStartupService {
758775
id: contact.id,
759776
remoteJid: contact.remoteJid,
760777
pushName: contact.pushName,
778+
chatName: contact.chatName,
761779
profilePicUrl: contact.profilePicUrl,
762780
updatedAt: contact.updatedAt,
763781
windowStart: contact.windowStart,

0 commit comments

Comments
 (0)