Skip to content

Commit 09911c4

Browse files
committed
refactor: optimize ChatwootService method for updating contact information
1 parent 7a0149e commit 09911c4

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

src/api/integrations/chatwoot/services/chatwoot.service.ts

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -561,53 +561,40 @@ export class ChatwootService {
561561

562562
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
563563

564-
const findContact = await this.findContact(instance, chatId);
565-
566-
let contact: any;
567-
if (body.key.fromMe) {
568-
if (findContact) {
569-
contact = await this.updateContact(instance, findContact.id, {
570-
avatar_url: picture_url.profilePictureUrl || null,
564+
let contact = await this.findContact(instance, chatId);
565+
566+
if (contact) {
567+
const waProfilePictureFile = picture_url.profilePictureUrl.split('#')[0].split('?')[0].split('/').pop();
568+
const chatwootProfilePictureFile = contact?.thumbnail?.split('#')[0].split('?')[0].split('/').pop();
569+
const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile;
570+
const nameNeedsUpdate =
571+
!contact.name ||
572+
contact.name === chatId ||
573+
(`+${chatId}`.startsWith('+55')
574+
? this.getNumbers(`+${chatId}`).some(
575+
(v) => contact.name === v || contact.name === v.substring(3) || contact.name === v.substring(1),
576+
)
577+
: false);
578+
579+
const contactNeedsUpdate = pictureNeedsUpdate || nameNeedsUpdate;
580+
if (contactNeedsUpdate) {
581+
this.logger.verbose('update contact in chatwoot');
582+
contact = await this.updateContact(instance, contact.id, {
583+
...(nameNeedsUpdate && { name: nameContact }),
584+
...(pictureNeedsUpdate && { avatar_url: picture_url.profilePictureUrl || null }),
571585
});
572-
} else {
573-
const jid = isGroup ? null : body.key.remoteJid;
574-
contact = await this.createContact(
575-
instance,
576-
chatId,
577-
filterInbox.id,
578-
isGroup,
579-
nameContact,
580-
picture_url.profilePictureUrl || null,
581-
jid,
582-
);
583586
}
584587
} else {
585-
if (findContact) {
586-
if (!findContact.name || findContact.name === chatId) {
587-
contact = await this.updateContact(instance, findContact.id, {
588-
name: nameContact,
589-
avatar_url: picture_url.profilePictureUrl || null,
590-
});
591-
} else {
592-
contact = await this.updateContact(instance, findContact.id, {
593-
avatar_url: picture_url.profilePictureUrl || null,
594-
});
595-
}
596-
if (!contact) {
597-
contact = await this.findContact(instance, chatId);
598-
}
599-
} else {
600-
const jid = isGroup ? null : body.key.remoteJid;
601-
contact = await this.createContact(
602-
instance,
603-
chatId,
604-
filterInbox.id,
605-
isGroup,
606-
nameContact,
607-
picture_url.profilePictureUrl || null,
608-
jid,
609-
);
610-
}
588+
const jid = isGroup ? null : body.key.remoteJid;
589+
contact = await this.createContact(
590+
instance,
591+
chatId,
592+
filterInbox.id,
593+
isGroup,
594+
nameContact,
595+
picture_url.profilePictureUrl || null,
596+
jid,
597+
);
611598
}
612599

613600
if (!contact) {
@@ -617,13 +604,6 @@ export class ChatwootService {
617604

618605
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
619606

620-
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
621-
this.logger.verbose('update contact name in chatwoot');
622-
await this.updateContact(instance, contactId, {
623-
name: nameContact,
624-
});
625-
}
626-
627607
this.logger.verbose('get contact conversations in chatwoot');
628608
const contactConversations = (await client.contacts.listConversations({
629609
accountId: this.provider.account_id,

0 commit comments

Comments
 (0)