From 8162473eb6c2d4895d75bfd65e92b46587e61b48 Mon Sep 17 00:00:00 2001 From: Honza Sterba Date: Mon, 22 Sep 2025 15:29:30 +0200 Subject: [PATCH] fix: Contact search by phone number (#10386) # Pull Request Template ## Description when filtering contacts by phone number a + is always added to the begining of the query, this means that the filtering breaks if the complete phone number with international code and + is entered ## Type of change Please delete options that are not relevant. - [X] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Updated automated tests Tested manually with contact filtering UI ## Checklist: - [X] My code follows the style guidelines of this project - [X] I have performed a self-review of my code - [X] I have commented on my code, particularly in hard-to-understand areas - [X] I have made corresponding changes to the documentation - [X] My changes generate no new warnings - [X] I have added tests that prove my fix is effective or that my feature works - [X] New and existing unit tests pass locally with my changes - [X] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth --- app/services/contacts/filter_service.rb | 2 +- spec/services/contacts/filter_service_spec.rb | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/services/contacts/filter_service.rb b/app/services/contacts/filter_service.rb index 7f2d6a0b8..9d017ea75 100644 --- a/app/services/contacts/filter_service.rb +++ b/app/services/contacts/filter_service.rb @@ -21,7 +21,7 @@ class Contacts::FilterService < FilterService def filter_values(query_hash) current_val = query_hash['values'][0] if query_hash['attribute_key'] == 'phone_number' - "+#{current_val}" + "+#{current_val&.delete('+')}" elsif query_hash['attribute_key'] == 'country_code' current_val.downcase else diff --git a/spec/services/contacts/filter_service_spec.rb b/spec/services/contacts/filter_service_spec.rb index 22882ae81..77d3a49f3 100644 --- a/spec/services/contacts/filter_service_spec.rb +++ b/spec/services/contacts/filter_service_spec.rb @@ -9,7 +9,7 @@ describe Contacts::FilterService do let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) } let!(:en_contact) { create(:contact, account: account, additional_attributes: { 'country_code': 'uk' }) } let!(:el_contact) { create(:contact, account: account, additional_attributes: { 'country_code': 'gr' }) } - let!(:cs_contact) { create(:contact, account: account, additional_attributes: { 'country_code': 'cz' }) } + let!(:cs_contact) { create(:contact, :with_phone_number, account: account, additional_attributes: { 'country_code': 'cz' }) } before do create(:inbox_member, user: first_user, inbox: inbox) @@ -65,6 +65,42 @@ describe Contacts::FilterService do end end + context 'with standard attributes - phone' do + it 'filter contacts by name' do + params[:payload] = [ + { + attribute_key: 'phone_number', + filter_operator: 'equal_to', + values: [cs_contact.phone_number], + query_operator: nil + }.with_indifferent_access + ] + + result = filter_service.new(account, first_user, params).perform + expect(result[:count]).to be 1 + expect(result[:contacts].length).to be 1 + expect(result[:contacts].first.name).to eq(cs_contact.name) + end + end + + context 'with standard attributes - phone (without +)' do + it 'filter contacts by name' do + params[:payload] = [ + { + attribute_key: 'phone_number', + filter_operator: 'equal_to', + values: [cs_contact.phone_number[1..]], + query_operator: nil + }.with_indifferent_access + ] + + result = filter_service.new(account, first_user, params).perform + expect(result[:count]).to be 1 + expect(result[:contacts].length).to be 1 + expect(result[:contacts].first.name).to eq(cs_contact.name) + end + end + context 'with standard attributes - blocked' do it 'filter contacts by blocked' do blocked_contact = create(:contact, account: account, blocked: true)