mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51: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
|
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 {
|
||||||
|
@ -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
|
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 {
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user