Skip to content

Commit 8fa61e4

Browse files
authored
Merge branch 'EvolutionAPI:develop' into develop
2 parents 7439d24 + 7dacd75 commit 8fa61e4

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

src/whatsapp/services/chatwoot.service.ts

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ export class ChatwootService {
369369
}
370370

371371
let query: any;
372+
const isGroup = phoneNumber.includes('@g.us');
372373

373-
if (!phoneNumber.includes('@g.us')) {
374+
if (!isGroup) {
374375
this.logger.verbose('format phone number');
375376
query = `+${phoneNumber}`;
376377
} else {
@@ -379,25 +380,86 @@ export class ChatwootService {
379380
}
380381

381382
this.logger.verbose('find contact in chatwoot');
382-
const contact: any = await client.contacts.search({
383-
accountId: this.provider.account_id,
384-
q: query,
385-
});
383+
let contact: any;
386384

387-
if (!contact) {
385+
if(isGroup) {
386+
contact = await client.contacts.search({
387+
accountId: this.provider.account_id,
388+
q: query,
389+
});
390+
} else {
391+
contact = await client.contacts.filter({
392+
accountId: this.provider.account_id,
393+
payload: this.getFilterPayload(query),
394+
});
395+
}
396+
397+
if(!contact) {
388398
this.logger.warn('contact not found');
389399
return null;
390400
}
391401

392-
if (!phoneNumber.includes('@g.us')) {
402+
if (!isGroup) {
393403
this.logger.verbose('return contact');
394-
return contact.payload.find((contact) => contact.phone_number === query);
404+
return this.findContactInContactList(contact.payload, query);
395405
} else {
396406
this.logger.verbose('return group');
397407
return contact.payload.find((contact) => contact.identifier === query);
398408
}
399409
}
400410

411+
private findContactInContactList(contacts: any[], query: string) {
412+
const phoneNumbers = this.getNumbers(query);
413+
const searchableFields = this.getSearchableFields();
414+
415+
for (const contact of contacts) {
416+
for (const field of searchableFields) {
417+
if (contact[field] && phoneNumbers.includes(contact[field])) {
418+
return contact;
419+
}
420+
}
421+
}
422+
423+
return null;
424+
}
425+
426+
private getNumbers(query: string) {
427+
const numbers = [];
428+
numbers.push(query);
429+
430+
if (query.startsWith('+55') && query.length === 14) {
431+
const withoutNine = query.slice(0, 5) + query.slice(6);
432+
numbers.push(withoutNine);
433+
} else if (query.startsWith('+55') && query.length === 13) {
434+
const withNine = query.slice(0, 5) + '9' + query.slice(5);
435+
numbers.push(withNine);
436+
}
437+
438+
return numbers;
439+
}
440+
441+
private getSearchableFields() {
442+
return ['identifier', 'phone_number', 'name', 'email'];
443+
}
444+
445+
private getFilterPayload(query: string) {
446+
const payload = [];
447+
const values = this.getNumbers(query)
448+
449+
const fields = this.getSearchableFields();
450+
fields.forEach((key, index) => {
451+
const queryOperator = fields.length - 1 === index ? null : 'OR';
452+
payload.push({
453+
"attribute_key": key,
454+
"filter_operator": "contains",
455+
"values": values,
456+
"query_operator": queryOperator
457+
});
458+
});
459+
460+
return payload;
461+
}
462+
401463
public async createConversation(instance: InstanceDto, body: any) {
402464
this.logger.verbose('create conversation to instance: ' + instance.instanceName);
403465
try {

0 commit comments

Comments
 (0)