mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 17:38:40 -06:00
fix: change method search for filter
This commit is contained in:
parent
838905f3dd
commit
535d5ee47f
@ -391,18 +391,18 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('find contact in chatwoot');
|
this.logger.verbose('find contact in chatwoot');
|
||||||
let contact: any = await client.contacts.search({
|
let contact: any;
|
||||||
accountId: this.provider.account_id,
|
|
||||||
q: query,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!contact && !isGroup && query.startsWith('+55') && query.length > 13) {
|
if(isGroup) {
|
||||||
this.logger.verbose('trying without the 9th digit');
|
|
||||||
query = query.slice(0, 5) + query.slice(6);
|
|
||||||
contact = await client.contacts.search({
|
contact = await client.contacts.search({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.account_id,
|
||||||
q: query,
|
q: query,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
contact = await client.contacts.filter({
|
||||||
|
accountId: this.provider.account_id,
|
||||||
|
payload: this.getFilterPayload(query),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!contact) {
|
if(!contact) {
|
||||||
@ -410,15 +410,67 @@ export class ChatwootService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!phoneNumber.includes('@g.us')) {
|
if (!isGroup) {
|
||||||
this.logger.verbose('return contact');
|
this.logger.verbose('return contact');
|
||||||
return contact.payload.find((contact) => contact.phone_number === query);
|
return this.findContactInContactList(contact.payload, query);
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('return group');
|
this.logger.verbose('return group');
|
||||||
return contact.payload.find((contact) => contact.identifier === query);
|
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) {
|
public async createConversation(instance: InstanceDto, body: any) {
|
||||||
this.logger.verbose('create conversation to instance: ' + instance.instanceName);
|
this.logger.verbose('create conversation to instance: ' + instance.instanceName);
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user