Skip to content

Commit d5a7e03

Browse files
Merge pull request #1012 from yousseefspires/fix/chats-messages
Add unique in Chat by instance/remotejid
2 parents 52b6b61 + f6ccd58 commit d5a7e03

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
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([instanceId, remoteJid])
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: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,14 @@ 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({
1151-
data: chatToInsert,
1152-
});
1150+
try {
1151+
await this.prismaRepository.chat.create({
1152+
data: chatToInsert,
1153+
});
1154+
}
1155+
catch(error){
1156+
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
1157+
}
11531158
}
11541159
}
11551160

@@ -1483,9 +1488,14 @@ export class BaileysStartupService extends ChannelStartupService {
14831488

14841489
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
14851490
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
1486-
await this.prismaRepository.chat.create({
1487-
data: chatToInsert,
1488-
});
1491+
try {
1492+
await this.prismaRepository.chat.create({
1493+
data: chatToInsert,
1494+
});
1495+
}
1496+
catch(error){
1497+
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
1498+
}
14891499
}
14901500
}
14911501
}

0 commit comments

Comments
 (0)