Skip to content

Commit

Permalink
Merge pull request #1012 from yousseefspires/fix/chats-messages
Browse files Browse the repository at this point in the history
Add unique in Chat by instance/remotejid
  • Loading branch information
DavidsonGomes authored Nov 21, 2024
2 parents 52b6b61 + f6ccd58 commit d5a7e03
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions prisma/mysql-schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ model Chat {
unreadMessages Int @default(0)
@@index([instanceId])
@@index([remoteJid])
@@unique([instanceId, remoteJid])
}

model Contact {
Expand Down
1 change: 1 addition & 0 deletions prisma/postgresql-schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ model Chat {
unreadMessages Int @default(0)
@@index([instanceId])
@@index([remoteJid])
@@unique([remoteJid, instanceId])
}

model Contact {
Expand Down
22 changes: 16 additions & 6 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,14 @@ export class BaileysStartupService extends ChannelStartupService {

this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
if (this.configService.get<Database>('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}`);
}
}
}

Expand Down Expand Up @@ -1483,9 +1488,14 @@ export class BaileysStartupService extends ChannelStartupService {

this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
if (this.configService.get<Database>('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}`);
}
}
}
}
Expand Down

0 comments on commit d5a7e03

Please sign in to comment.