From 70f7c8ce89ce55d0c90c44bc3530429587738221 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 14 Jul 2023 17:04:15 -0300 Subject: [PATCH] feat: Save picture url groups in chatwoot --- src/whatsapp/services/chatwoot.service.ts | 54 ++++++++++++++++++----- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 4500fde8..4f0e22cd 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -258,6 +258,7 @@ export class ChatwootService { inboxId: number, isGroup: boolean, name?: string, + avatar_url?: string, ) { this.logger.verbose('create contact to instance: ' + instance.instanceName); @@ -275,6 +276,7 @@ export class ChatwootService { inbox_id: inboxId, name: name || phoneNumber, phone_number: `+${phoneNumber}`, + avatar_url: avatar_url, }; } else { this.logger.verbose('create contact group in chatwoot'); @@ -282,6 +284,7 @@ export class ChatwootService { inbox_id: inboxId, name: name || phoneNumber, identifier: phoneNumber, + avatar_url: avatar_url, }; } @@ -404,27 +407,58 @@ export class ChatwootService { nameContact = `${group.subject} (GROUP)`; this.logger.verbose('find or create participant in chatwoot'); - const participant = - (await this.findContact(instance, body.key.participant.split('@')[0])) || - ((await this.createContact( + + const picture_url = await this.waMonitor.waInstances[ + instance.instanceName + ].profilePicture(body.key.participant.split('@')[0]); + + const findParticipant = await this.findContact( + instance, + body.key.participant.split('@')[0], + ); + + if (findParticipant) { + await this.updateContact(instance, findParticipant.id, { + name: nameContact, + avatar_url: picture_url.profilePictureUrl || null, + }); + } else { + await this.createContact( instance, body.key.participant.split('@')[0], filterInbox.id, - false, - body.pushName || body.key.participant.split('@')[0], - )) as any); + isGroup, + nameContact, + picture_url.profilePictureUrl || null, + ); + } } this.logger.verbose('find or create contact in chatwoot'); - const contact = - (await this.findContact(instance, chatId)) || - ((await this.createContact( + + const picture_url = await this.waMonitor.waInstances[ + instance.instanceName + ].profilePicture(chatId); + + const findContact = await this.findContact(instance, chatId); + + let contact: any; + + if (findContact) { + contact = await this.updateContact(instance, findContact.id, { + name: nameContact, + avatar_url: picture_url.profilePictureUrl || null, + }); + } else { + contact = await this.createContact( instance, chatId, filterInbox.id, isGroup, nameContact, - )) as any); + picture_url.profilePictureUrl || null, + ); + } if (!contact) { this.logger.warn('contact not found');