@@ -391,34 +391,86 @@ export class ChatwootService {
391391 }
392392
393393 this . logger . verbose ( 'find contact in chatwoot' ) ;
394- let contact : any = await client . contacts . search ( {
395- accountId : this . provider . account_id ,
396- q : query ,
397- } ) ;
394+ let contact : any ;
398395
399- if ( ! contact && ! isGroup && query . startsWith ( '+55' ) && query . length > 13 ) {
400- this . logger . verbose ( 'trying without the 9th digit' ) ;
401- query = query . slice ( 0 , 5 ) + query . slice ( 6 ) ;
396+ if ( isGroup ) {
402397 contact = await client . contacts . search ( {
403398 accountId : this . provider . account_id ,
404399 q : query ,
405400 } ) ;
401+ } else {
402+ contact = await client . contacts . filter ( {
403+ accountId : this . provider . account_id ,
404+ payload : this . getFilterPayload ( query ) ,
405+ } ) ;
406406 }
407407
408408 if ( ! contact ) {
409409 this . logger . warn ( 'contact not found' ) ;
410410 return null ;
411411 }
412412
413- if ( ! phoneNumber . includes ( '@g.us' ) ) {
413+ if ( ! isGroup ) {
414414 this . logger . verbose ( 'return contact' ) ;
415- return contact . payload . find ( ( contact ) => contact . phone_number === query ) ;
415+ return this . findContactInContactList ( contact . payload , query ) ;
416416 } else {
417417 this . logger . verbose ( 'return group' ) ;
418418 return contact . payload . find ( ( contact ) => contact . identifier === query ) ;
419419 }
420420 }
421421
422+ private findContactInContactList ( contacts : any [ ] , query : string ) {
423+ const phoneNumbers = this . getNumbers ( query ) ;
424+ const searchableFields = this . getSearchableFields ( ) ;
425+
426+ for ( const contact of contacts ) {
427+ for ( const field of searchableFields ) {
428+ if ( contact [ field ] && phoneNumbers . includes ( contact [ field ] ) ) {
429+ return contact ;
430+ }
431+ }
432+ }
433+
434+ return null ;
435+ }
436+
437+ private getNumbers ( query : string ) {
438+ const numbers = [ ] ;
439+ numbers . push ( query ) ;
440+
441+ if ( query . startsWith ( '+55' ) && query . length === 14 ) {
442+ const withoutNine = query . slice ( 0 , 5 ) + query . slice ( 6 ) ;
443+ numbers . push ( withoutNine ) ;
444+ } else if ( query . startsWith ( '+55' ) && query . length === 13 ) {
445+ const withNine = query . slice ( 0 , 5 ) + '9' + query . slice ( 5 ) ;
446+ numbers . push ( withNine ) ;
447+ }
448+
449+ return numbers ;
450+ }
451+
452+ private getSearchableFields ( ) {
453+ return [ 'identifier' , 'phone_number' , 'name' , 'email' ] ;
454+ }
455+
456+ private getFilterPayload ( query : string ) {
457+ const payload = [ ] ;
458+ const values = this . getNumbers ( query )
459+
460+ const fields = this . getSearchableFields ( ) ;
461+ fields . forEach ( ( key , index ) => {
462+ const queryOperator = fields . length - 1 === index ? null : 'OR' ;
463+ payload . push ( {
464+ "attribute_key" : key ,
465+ "filter_operator" : "contains" ,
466+ "values" : values ,
467+ "query_operator" : queryOperator
468+ } ) ;
469+ } ) ;
470+
471+ return payload ;
472+ }
473+
422474 public async createConversation ( instance : InstanceDto , body : any ) {
423475 this . logger . verbose ( 'create conversation to instance: ' + instance . instanceName ) ;
424476 try {
0 commit comments