Skip to content

Commit cefe3ef

Browse files
committed
fix: chats must be unique
1 parent 1665654 commit cefe3ef

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

prisma/mysql-schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ model Chat {
127127
unreadMessages Int @default(0)
128128
@@index([instanceId])
129129
@@index([remoteJid])
130+
@@unique([remoteJid, instanceId])
130131
}
131132

132133
model Contact {

prisma/postgresql-schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ model Chat {
127127
unreadMessages Int @default(0)
128128
@@index([instanceId])
129129
@@index([remoteJid])
130+
@@unique([remoteJid, instanceId])
130131
}
131132

132133
model Contact {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,10 @@ export class BaileysStartupService extends ChannelStartupService {
11471147

11481148
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
11491149
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
1150-
await this.prismaRepository.chat.create({
1150+
await this.prismaRepository.chat.update({
1151+
where: {
1152+
id: existingChat.id,
1153+
},
11511154
data: chatToInsert,
11521155
});
11531156
}
@@ -1483,7 +1486,10 @@ export class BaileysStartupService extends ChannelStartupService {
14831486

14841487
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
14851488
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
1486-
await this.prismaRepository.chat.create({
1489+
await this.prismaRepository.chat.update({
1490+
where: {
1491+
id: existingChat.id,
1492+
},
14871493
data: chatToInsert,
14881494
});
14891495
}

0 commit comments

Comments
 (0)