mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
Fixed contacts update in the database
This commit is contained in:
parent
dacd408f8d
commit
54e47c09c4
@ -725,6 +725,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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -845,7 +846,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,
|
||||||
@ -857,10 +863,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);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1252,19 +1262,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,
|
||||||
@ -1291,8 +1301,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