diff --git a/src/apps/chatwoot/client/ContactAPI.ts b/src/apps/chatwoot/client/ContactAPI.ts index 537073e8..7f802bd1 100644 --- a/src/apps/chatwoot/client/ContactAPI.ts +++ b/src/apps/chatwoot/client/ContactAPI.ts @@ -96,18 +96,22 @@ export class ContactAPI { }; } - public updateCustomAttributes( + public async upsertCustomAttributes( contact: generic_id & contact, attributes: any, - ) { + ): Promise { + if (lodash.isEqual(attributes, contact.custom_attributes)) { + return false; + } const update: contact_update = { custom_attributes: { ...contact.custom_attributes, ...attributes }, }; - return this.accountAPI.contacts.update({ + await this.accountAPI.contacts.update({ id: contact.id, accountId: this.config.accountId, data: update, }); + return true; } public async create( diff --git a/src/apps/chatwoot/client/ContactConversationService.ts b/src/apps/chatwoot/client/ContactConversationService.ts index cfa8666b..ca0e1613 100644 --- a/src/apps/chatwoot/client/ContactConversationService.ts +++ b/src/apps/chatwoot/client/ContactConversationService.ts @@ -62,9 +62,9 @@ export class ContactConversationService { // Update custom attributes - always const attributes = await contactInfo.Attributes(); this.logger.info( - `Updating contact custom attributes for chat.id: ${chatId}, contact.id: ${contact.data.id}`, + `Updating if required contact custom attributes for chat.id: ${chatId}, contact.id: ${contact.data.id}`, ); - await this.contactAPI.updateCustomAttributes(contact.data, attributes); + await this.contactAPI.upsertCustomAttributes(contact.data, attributes); // Update Avatar if nothing, but keep the original one if any if (!contact.data.thumbnail) {