mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 09:28:39 -06:00
refactor: optimize ChatwootService method for updating contact information
This commit is contained in:
parent
7a0149ee23
commit
09911c472d
@ -561,53 +561,40 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
|
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
|
||||||
|
|
||||||
const findContact = await this.findContact(instance, chatId);
|
let contact = await this.findContact(instance, chatId);
|
||||||
|
|
||||||
let contact: any;
|
if (contact) {
|
||||||
if (body.key.fromMe) {
|
const waProfilePictureFile = picture_url.profilePictureUrl.split('#')[0].split('?')[0].split('/').pop();
|
||||||
if (findContact) {
|
const chatwootProfilePictureFile = contact?.thumbnail?.split('#')[0].split('?')[0].split('/').pop();
|
||||||
contact = await this.updateContact(instance, findContact.id, {
|
const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile;
|
||||||
avatar_url: picture_url.profilePictureUrl || null,
|
const nameNeedsUpdate =
|
||||||
|
!contact.name ||
|
||||||
|
contact.name === chatId ||
|
||||||
|
(`+${chatId}`.startsWith('+55')
|
||||||
|
? this.getNumbers(`+${chatId}`).some(
|
||||||
|
(v) => contact.name === v || contact.name === v.substring(3) || contact.name === v.substring(1),
|
||||||
|
)
|
||||||
|
: false);
|
||||||
|
|
||||||
|
const contactNeedsUpdate = pictureNeedsUpdate || nameNeedsUpdate;
|
||||||
|
if (contactNeedsUpdate) {
|
||||||
|
this.logger.verbose('update contact in chatwoot');
|
||||||
|
contact = await this.updateContact(instance, contact.id, {
|
||||||
|
...(nameNeedsUpdate && { name: nameContact }),
|
||||||
|
...(pictureNeedsUpdate && { avatar_url: picture_url.profilePictureUrl || null }),
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
const jid = isGroup ? null : body.key.remoteJid;
|
|
||||||
contact = await this.createContact(
|
|
||||||
instance,
|
|
||||||
chatId,
|
|
||||||
filterInbox.id,
|
|
||||||
isGroup,
|
|
||||||
nameContact,
|
|
||||||
picture_url.profilePictureUrl || null,
|
|
||||||
jid,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (findContact) {
|
const jid = isGroup ? null : body.key.remoteJid;
|
||||||
if (!findContact.name || findContact.name === chatId) {
|
contact = await this.createContact(
|
||||||
contact = await this.updateContact(instance, findContact.id, {
|
instance,
|
||||||
name: nameContact,
|
chatId,
|
||||||
avatar_url: picture_url.profilePictureUrl || null,
|
filterInbox.id,
|
||||||
});
|
isGroup,
|
||||||
} else {
|
nameContact,
|
||||||
contact = await this.updateContact(instance, findContact.id, {
|
picture_url.profilePictureUrl || null,
|
||||||
avatar_url: picture_url.profilePictureUrl || null,
|
jid,
|
||||||
});
|
);
|
||||||
}
|
|
||||||
if (!contact) {
|
|
||||||
contact = await this.findContact(instance, chatId);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const jid = isGroup ? null : body.key.remoteJid;
|
|
||||||
contact = await this.createContact(
|
|
||||||
instance,
|
|
||||||
chatId,
|
|
||||||
filterInbox.id,
|
|
||||||
isGroup,
|
|
||||||
nameContact,
|
|
||||||
picture_url.profilePictureUrl || null,
|
|
||||||
jid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!contact) {
|
if (!contact) {
|
||||||
@ -617,13 +604,6 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
|
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
|
||||||
|
|
||||||
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
|
|
||||||
this.logger.verbose('update contact name in chatwoot');
|
|
||||||
await this.updateContact(instance, contactId, {
|
|
||||||
name: nameContact,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.verbose('get contact conversations in chatwoot');
|
this.logger.verbose('get contact conversations in chatwoot');
|
||||||
const contactConversations = (await client.contacts.listConversations({
|
const contactConversations = (await client.contacts.listConversations({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.account_id,
|
||||||
|
Loading…
Reference in New Issue
Block a user