diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index ff968882..8ce2f79b 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 011de9a0..eaff59e5 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 a01559e3..b84ce254 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1147,9 +1147,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}`); + } } } @@ -1483,9 +1488,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}`); + } } } }