diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index ff968882..55ab4d70 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([remoteJid, instanceId]) } 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 0afd5318..6261c17d 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1147,7 +1147,10 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { - await this.prismaRepository.chat.create({ + await this.prismaRepository.chat.update({ + where: { + id: existingChat.id, + }, data: chatToInsert, }); } @@ -1483,7 +1486,10 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { - await this.prismaRepository.chat.create({ + await this.prismaRepository.chat.update({ + where: { + id: existingChat.id, + }, data: chatToInsert, }); }