Merge pull request #586 from deivisonrpg/chatwoot-update-contact

refactor(chatwoot): optimize ChatwootService method for updating contact inform…
This commit is contained in:
Davidson Gomes 2024-05-09 14:11:07 -03:00 committed by GitHub
commit f78d360c38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -561,40 +561,32 @@ 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) { if (!body.key.fromMe) {
if (findContact) { const waProfilePictureFile =
contact = await this.updateContact(instance, findContact.id, { picture_url?.profilePictureUrl?.split('#')[0].split('?')[0].split('/').pop() || '';
avatar_url: picture_url.profilePictureUrl || null, const chatwootProfilePictureFile = contact?.thumbnail?.split('#')[0].split('?')[0].split('/').pop() || '';
}); const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile;
} else { const nameNeedsUpdate =
const jid = isGroup ? null : body.key.remoteJid; !contact.name ||
contact = await this.createContact( contact.name === chatId ||
instance, (`+${chatId}`.startsWith('+55')
chatId, ? this.getNumbers(`+${chatId}`).some(
filterInbox.id, (v) => contact.name === v || contact.name === v.substring(3) || contact.name === v.substring(1),
isGroup, )
nameContact, : false);
picture_url.profilePictureUrl || null,
jid, const contactNeedsUpdate = pictureNeedsUpdate || nameNeedsUpdate;
); if (contactNeedsUpdate) {
} this.logger.verbose('update contact in chatwoot');
} else { contact = await this.updateContact(instance, contact.id, {
if (findContact) { ...(nameNeedsUpdate && { name: nameContact }),
if (!findContact.name || findContact.name === chatId) { ...(waProfilePictureFile === '' && { avatar: null }),
contact = await this.updateContact(instance, findContact.id, { ...(pictureNeedsUpdate && { avatar_url: picture_url?.profilePictureUrl }),
name: nameContact,
avatar_url: picture_url.profilePictureUrl || null,
});
} else {
contact = await this.updateContact(instance, findContact.id, {
avatar_url: picture_url.profilePictureUrl || null,
}); });
} }
if (!contact) {
contact = await this.findContact(instance, chatId);
} }
} else { } else {
const jid = isGroup ? null : body.key.remoteJid; const jid = isGroup ? null : body.key.remoteJid;
@ -608,7 +600,6 @@ export class ChatwootService {
jid, jid,
); );
} }
}
if (!contact) { if (!contact) {
this.logger.warn('contact not found'); this.logger.warn('contact not found');
@ -617,13 +608,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,