fix: change method search for filter

This commit is contained in:
Yves Clêuder Lima de Jesus 2024-01-28 10:36:42 -03:00
parent 838905f3dd
commit 535d5ee47f

View File

@ -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 {