mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
Merge pull request #747 from judsonjuniorr/v2-update-contacts-database
V2 update contacts database
This commit is contained in:
commit
404edaa4e1
@ -131,6 +131,8 @@ model Contact {
|
||||
updatedAt DateTime? @updatedAt @db.Timestamp
|
||||
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||
instanceId String
|
||||
|
||||
@@unique([remoteJid, instanceId])
|
||||
}
|
||||
|
||||
model Message {
|
||||
|
@ -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");
|
@ -131,6 +131,8 @@ model Contact {
|
||||
updatedAt DateTime? @updatedAt @db.Timestamp
|
||||
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||
instanceId String
|
||||
|
||||
@@unique([remoteJid, instanceId])
|
||||
}
|
||||
|
||||
model Message {
|
||||
|
@ -730,6 +730,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS)
|
||||
await this.prismaRepository.chat.createMany({
|
||||
data: chatsToInsert,
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
},
|
||||
@ -849,7 +850,12 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
},
|
||||
|
||||
'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) {
|
||||
contactsRaw.push({
|
||||
remoteJid: contact.id,
|
||||
@ -861,10 +867,14 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactsRaw);
|
||||
|
||||
this.prismaRepository.contact.updateMany({
|
||||
where: { instanceId: this.instanceId },
|
||||
data: contactsRaw,
|
||||
});
|
||||
const updateTransactions = contactsRaw.map((contact) =>
|
||||
this.prismaRepository.contact.upsert({
|
||||
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 },
|
||||
});
|
||||
|
||||
const contactRaw: any = {
|
||||
const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
|
||||
remoteJid: received.key.remoteJid,
|
||||
pushName: received.pushName,
|
||||
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
|
||||
instanceId: this.instanceId,
|
||||
};
|
||||
|
||||
if (contactRaw.id === 'status@broadcast') {
|
||||
if (contactRaw.remoteJid === 'status@broadcast') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (contact) {
|
||||
const contactRaw: any = {
|
||||
const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
|
||||
remoteJid: received.key.remoteJid,
|
||||
pushName: contact.pushName,
|
||||
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
|
||||
@ -1292,8 +1302,15 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
|
||||
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS)
|
||||
await this.prismaRepository.contact.create({
|
||||
data: contactRaw,
|
||||
await this.prismaRepository.contact.upsert({
|
||||
where: {
|
||||
remoteJid_instanceId: {
|
||||
remoteJid: contactRaw.remoteJid,
|
||||
instanceId: contactRaw.instanceId,
|
||||
},
|
||||
},
|
||||
update: contactRaw,
|
||||
create: contactRaw,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
|
Loading…
Reference in New Issue
Block a user