@@ -86,13 +86,14 @@ export class ChatwootService {
8686 return client ;
8787 }
8888
89- public getClientCwConfig ( ) : ChatwootAPIConfig & { name_inbox : string } {
89+ public getClientCwConfig ( ) : ChatwootAPIConfig & { name_inbox : string ; merge_brazil_contacts : boolean } {
9090 return {
9191 basePath : this . provider . url ,
9292 with_credentials : true ,
9393 credentials : 'include' ,
9494 token : this . provider . token ,
9595 name_inbox : this . provider . name_inbox ,
96+ merge_brazil_contacts : this . provider . merge_brazil_contacts ,
9697 } ;
9798 }
9899
@@ -418,10 +419,49 @@ export class ChatwootService {
418419 }
419420 }
420421
422+ private async mergeBrazilianContacts ( contacts : any [ ] ) {
423+ try {
424+ //sdk chatwoot não tem função merge
425+ this . logger . verbose ( 'merging contacts' ) ;
426+ const contact = await chatwootRequest ( this . getClientCwConfig ( ) , {
427+ method : 'POST' ,
428+ url : `/api/v1/accounts/${ this . provider . account_id } /actions/contact_merge` ,
429+ body : {
430+ base_contact_id : contacts . find ( ( contact ) => contact . phone_number . length === 14 ) ?. id ,
431+ mergee_contact_id : contacts . find ( ( contact ) => contact . phone_number . length === 13 ) ?. id ,
432+ } ,
433+ } ) ;
434+
435+ return contact ;
436+ } catch {
437+ this . logger . error ( 'Error merging contacts' ) ;
438+ return null ;
439+ }
440+ }
441+
421442 private findContactInContactList ( contacts : any [ ] , query : string ) {
422443 const phoneNumbers = this . getNumbers ( query ) ;
423444 const searchableFields = this . getSearchableFields ( ) ;
424445
446+ // eslint-disable-next-line prettier/prettier
447+ if ( contacts . length === 2 && this . getClientCwConfig ( ) . merge_brazil_contacts && query . startsWith ( '+55' ) ) {
448+
449+ const contact = this . mergeBrazilianContacts ( contacts ) ;
450+ if ( contact ) {
451+ return contact ;
452+ }
453+ }
454+
455+ const phone = phoneNumbers . reduce (
456+ ( savedNumber , number ) => ( number . length > savedNumber . length ? number : savedNumber ) ,
457+ '' ,
458+ ) ;
459+
460+ const contact_with9 = contacts . find ( ( contact ) => contact . phone_number === phone ) ;
461+ if ( contact_with9 ) {
462+ return contact_with9 ;
463+ }
464+
425465 for ( const contact of contacts ) {
426466 for ( const field of searchableFields ) {
427467 if ( contact [ field ] && phoneNumbers . includes ( contact [ field ] ) ) {
@@ -449,7 +489,7 @@ export class ChatwootService {
449489 }
450490
451491 private getSearchableFields ( ) {
452- return [ 'phone_number' , 'identifier' ] ;
492+ return [ 'phone_number' ] ;
453493 }
454494
455495 private getFilterPayload ( query : string ) {
@@ -463,7 +503,7 @@ export class ChatwootService {
463503 const queryOperator = fieldsToSearch . length - 1 === index1 && numbers . length - 1 === index2 ? null : 'OR' ;
464504 filterPayload . push ( {
465505 attribute_key : field ,
466- filter_operator : [ 'phone_number' , 'identifier' ] . includes ( field ) ? ' equal_to' : 'contains ',
506+ filter_operator : ' equal_to',
467507 values : [ number . replace ( '+' , '' ) ] ,
468508 query_operator : queryOperator ,
469509 } ) ;
0 commit comments