From 535d5ee47f50d1ea743ef5d409fbf58dc05860f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves=20Cl=C3=AAuder=20Lima=20de=20Jesus?= Date: Sun, 28 Jan 2024 10:36:42 -0300 Subject: [PATCH] fix: change method search for filter --- src/whatsapp/services/chatwoot.service.ts | 70 ++++++++++++++++++++--- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index e4754cd8..90e2ad6c 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -391,18 +391,18 @@ export class ChatwootService { } this.logger.verbose('find contact in chatwoot'); - let contact: any = await client.contacts.search({ - accountId: this.provider.account_id, - q: query, - }); + let contact: any; - if (!contact && !isGroup && query.startsWith('+55') && query.length > 13) { - this.logger.verbose('trying without the 9th digit'); - query = query.slice(0, 5) + query.slice(6); + if(isGroup) { contact = await client.contacts.search({ accountId: this.provider.account_id, q: query, }); + } else { + contact = await client.contacts.filter({ + accountId: this.provider.account_id, + payload: this.getFilterPayload(query), + }); } if(!contact) { @@ -410,15 +410,67 @@ export class ChatwootService { return null; } - if (!phoneNumber.includes('@g.us')) { + if (!isGroup) { this.logger.verbose('return contact'); - return contact.payload.find((contact) => contact.phone_number === query); + return this.findContactInContactList(contact.payload, query); } else { this.logger.verbose('return group'); return contact.payload.find((contact) => contact.identifier === query); } } + private findContactInContactList(contacts: any[], query: string) { + const phoneNumbers = this.getNumbers(query); + const searchableFields = this.getSearchableFields(); + + for (const contact of contacts) { + for (const field of searchableFields) { + if (contact[field] && phoneNumbers.includes(contact[field])) { + return contact; + } + } + } + + return null; + } + + private getNumbers(query: string) { + const numbers = []; + numbers.push(query); + + if (query.startsWith('+55') && query.length === 14) { + const withoutNine = query.slice(0, 5) + query.slice(6); + numbers.push(withoutNine); + } else if (query.startsWith('+55') && query.length === 13) { + const withNine = query.slice(0, 5) + '9' + query.slice(5); + numbers.push(withNine); + } + + return numbers; + } + + private getSearchableFields() { + return ['identifier', 'phone_number', 'name', 'email']; + } + + private getFilterPayload(query: string) { + const payload = []; + const values = this.getNumbers(query) + + const fields = this.getSearchableFields(); + fields.forEach((key, index) => { + const queryOperator = fields.length - 1 === index ? null : 'OR'; + payload.push({ + "attribute_key": key, + "filter_operator": "contains", + "values": values, + "query_operator": queryOperator + }); + }); + + return payload; + } + public async createConversation(instance: InstanceDto, body: any) { this.logger.verbose('create conversation to instance: ' + instance.instanceName); try {