Merge pull request #747 from judsonjuniorr/v2-update-contacts-database

V2 update contacts database
This commit is contained in:
Davidson Gomes 2024-08-12 09:41:18 -03:00 committed by GitHub
commit 404edaa4e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 10 deletions

View File

@ -131,6 +131,8 @@ model Contact {
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
@@unique([remoteJid, instanceId])
} }
model Message { model Message {

View File

@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[remoteJid,instanceId]` on the table `Contact` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "Contact_remoteJid_instanceId_key" ON "Contact"("remoteJid", "instanceId");

View File

@ -131,6 +131,8 @@ model Contact {
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
@@unique([remoteJid, instanceId])
} }
model Message { model Message {

View File

@ -730,6 +730,7 @@ export class BaileysStartupService extends ChannelStartupService {
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS)
await this.prismaRepository.chat.createMany({ await this.prismaRepository.chat.createMany({
data: chatsToInsert, data: chatsToInsert,
skipDuplicates: true,
}); });
} }
}, },
@ -849,7 +850,12 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
'contacts.update': async (contacts: Partial<Contact>[]) => { 'contacts.update': async (contacts: Partial<Contact>[]) => {
const contactsRaw: any = []; const contactsRaw: {
remoteJid: string;
pushName?: string;
profilePicUrl?: string;
instanceId: string;
}[] = [];
for await (const contact of contacts) { for await (const contact of contacts) {
contactsRaw.push({ contactsRaw.push({
remoteJid: contact.id, remoteJid: contact.id,
@ -861,10 +867,14 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactsRaw); this.sendDataWebhook(Events.CONTACTS_UPDATE, contactsRaw);
this.prismaRepository.contact.updateMany({ const updateTransactions = contactsRaw.map((contact) =>
where: { instanceId: this.instanceId }, this.prismaRepository.contact.upsert({
data: contactsRaw, where: { remoteJid_instanceId: { remoteJid: contact.remoteJid, instanceId: contact.instanceId } },
}); create: contact,
update: contact,
}),
);
await this.prismaRepository.$transaction(updateTransactions);
}, },
}; };
@ -1253,19 +1263,19 @@ export class BaileysStartupService extends ChannelStartupService {
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId }, where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
}); });
const contactRaw: any = { const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
remoteJid: received.key.remoteJid, remoteJid: received.key.remoteJid,
pushName: received.pushName, pushName: received.pushName,
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl, profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
instanceId: this.instanceId, instanceId: this.instanceId,
}; };
if (contactRaw.id === 'status@broadcast') { if (contactRaw.remoteJid === 'status@broadcast') {
return; return;
} }
if (contact) { if (contact) {
const contactRaw: any = { const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
remoteJid: received.key.remoteJid, remoteJid: received.key.remoteJid,
pushName: contact.pushName, pushName: contact.pushName,
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl, profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
@ -1292,8 +1302,15 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw); this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS) if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS)
await this.prismaRepository.contact.create({ await this.prismaRepository.contact.upsert({
data: contactRaw, where: {
remoteJid_instanceId: {
remoteJid: contactRaw.remoteJid,
instanceId: contactRaw.instanceId,
},
},
update: contactRaw,
create: contactRaw,
}); });
} }
} catch (error) { } catch (error) {