From f6ccd58deee788f19183500cb51d8fdc8aebce7b Mon Sep 17 00:00:00 2001 From: yousseefs Date: Tue, 29 Oct 2024 18:43:31 +0000 Subject: [PATCH] some fixs --- prisma/mysql-schema.prisma | 1 + prisma/postgresql-schema.prisma | 1 + .../whatsapp/whatsapp.baileys.service.ts | 22 ++++++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index ff968882b..8ce2f79b8 100644 --- a/prisma/mysql-schema.prisma +++ b/prisma/mysql-schema.prisma @@ -127,6 +127,7 @@ model Chat { unreadMessages Int @default(0) @@index([instanceId]) @@index([remoteJid]) + @@unique([instanceId, remoteJid]) } model Contact { diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 011de9a09..eaff59e58 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -127,6 +127,7 @@ model Chat { unreadMessages Int @default(0) @@index([instanceId]) @@index([remoteJid]) + @@unique([remoteJid, instanceId]) } model Contact { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 185910bd3..54d6b26a9 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1085,9 +1085,14 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { - await this.prismaRepository.chat.create({ - data: chatToInsert, - }); + try { + await this.prismaRepository.chat.create({ + data: chatToInsert, + }); + } + catch(error){ + console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`); + } } } @@ -1420,9 +1425,14 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { - await this.prismaRepository.chat.create({ - data: chatToInsert, - }); + try { + await this.prismaRepository.chat.create({ + data: chatToInsert, + }); + } + catch(error){ + console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`); + } } } }