From 668f4772185c23fbed540e367d0a2fbe2d441206 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sat, 10 Aug 2024 23:15:02 -0300 Subject: [PATCH 1/5] Validate if session exists before deleting it --- .../services/channels/whatsapp.baileys.service.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 392afa17..21b528be 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -227,11 +227,16 @@ export class BaileysStartupService extends ChannelStartupService { this.client?.ws?.close(); - await this.prismaRepository.session.delete({ - where: { - sessionId: this.instanceId, - }, + const sessionExists = await this.prismaRepository.session.findFirst({ + where: { sessionId: this.instanceId }, }); + if (sessionExists) { + await this.prismaRepository.session.delete({ + where: { + sessionId: this.instanceId, + }, + }); + } } public async getProfileName() { From 926197b1baf3c5041083e7ca742c3dc8046e4300 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sat, 10 Aug 2024 23:15:20 -0300 Subject: [PATCH 2/5] Save chat names for groups identification --- prisma/mysql-schema.prisma | 1 + prisma/postgresql-schema.prisma | 1 + src/api/services/channels/whatsapp.baileys.service.ts | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index ed82c7e7..d08bf897 100644 --- a/prisma/mysql-schema.prisma +++ b/prisma/mysql-schema.prisma @@ -114,6 +114,7 @@ model Session { model Chat { id String @id @default(cuid()) remoteJid String @db.VarChar(100) + name String? @db.VarChar(100) labels Json? @db.Json createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index c245ed9d..fcd54b50 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -114,6 +114,7 @@ model Session { model Chat { id String @id @default(cuid()) remoteJid String @db.VarChar(100) + name String? @db.VarChar(100) labels Json? @db.JsonB createdAt DateTime? @default(now()) @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 21b528be..b37b8275 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -722,7 +722,7 @@ export class BaileysStartupService extends ChannelStartupService { const chatsToInsert = chats .filter((chat) => !existingChatIdSet.has(chat.id)) - .map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId })); + .map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name })); this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert); @@ -754,10 +754,9 @@ export class BaileysStartupService extends ChannelStartupService { where: { instanceId: this.instanceId, remoteJid: chat.id, + name: chat.name, }, - data: { - remoteJid: chat.id, - }, + data: { remoteJid: chat.id }, }); } }, @@ -900,7 +899,7 @@ export class BaileysStartupService extends ChannelStartupService { } } - const chatsRaw: any[] = []; + const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = []; const chatsRepository = new Set( ( await this.prismaRepository.chat.findMany({ @@ -917,6 +916,7 @@ export class BaileysStartupService extends ChannelStartupService { chatsRaw.push({ remoteJid: chat.id, instanceId: this.instanceId, + name: chat.name, }); } From d31b2c7f97bcc3d4e44988a61acd713c13a40ef9 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sat, 10 Aug 2024 23:16:14 -0300 Subject: [PATCH 3/5] Remove unnecessary logs --- src/api/services/channels/whatsapp.baileys.service.ts | 8 ++------ src/api/services/channels/whatsapp.business.service.ts | 2 -- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index b37b8275..3cde5fc4 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -923,12 +923,10 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_SET, chatsRaw); if (this.configService.get('DATABASE').SAVE_DATA.HISTORIC) { - const chatsSaved = await this.prismaRepository.chat.createMany({ + await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true, }); - - console.log('chatsSaved', chatsSaved); } const messagesRaw: any[] = []; @@ -987,12 +985,10 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]); if (this.configService.get('DATABASE').SAVE_DATA.HISTORIC) { - const messagesSaved = await this.prismaRepository.message.createMany({ + await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true, }); - - console.log('messagesSaved', messagesSaved); } if ( diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index cd7ab4a6..d4e24765 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -300,7 +300,6 @@ export class BusinessStartupService extends ChannelStartupService { protected async messageHandle(received: any, database: Database, settings: any) { try { - console.log(received); let messageRaw: any; let pushName: any; @@ -983,7 +982,6 @@ export class BusinessStartupService extends ChannelStartupService { return messageRaw; } catch (error) { - console.log(error.response.data); this.logger.error(error); throw new BadRequestException(error.toString()); } From 77cf4125dd05b9df2ce60bf135d10ca2fcbd9bf3 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 11 Aug 2024 15:29:39 -0300 Subject: [PATCH 4/5] Added migration for name column in chats --- .../20240811021156_add_chat_name_column/migration.sql | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 prisma/postgresql-migrations/20240811021156_add_chat_name_column/migration.sql diff --git a/prisma/postgresql-migrations/20240811021156_add_chat_name_column/migration.sql b/prisma/postgresql-migrations/20240811021156_add_chat_name_column/migration.sql new file mode 100644 index 00000000..79d7fc1f --- /dev/null +++ b/prisma/postgresql-migrations/20240811021156_add_chat_name_column/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Chat" ADD COLUMN "name" VARCHAR(100); From 7b1e4e9e3d704aabee90452ef14fc2300d7cedaf Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 11 Aug 2024 15:49:43 -0300 Subject: [PATCH 5/5] Add chat name to fetch chats request --- src/api/services/channel.service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index d00ccd54..8d4cc7cf 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -1267,6 +1267,7 @@ export class ChannelStartupService { SELECT "Chat"."id", "Chat"."remoteJid", + "Chat"."name", "Chat"."labels", "Chat"."createdAt", "Chat"."updatedAt", @@ -1283,6 +1284,7 @@ export class ChannelStartupService { SELECT "Chat"."id", "Chat"."remoteJid", + "Chat"."name", "Chat"."labels", "Chat"."createdAt", "Chat"."updatedAt",