From 1ade4f75ea76aa1dab5947a3f6ca72c32514cb4b Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Fri, 11 Dec 2020 23:25:00 +0530 Subject: [PATCH] chore: Ingore case while searching contact name, email (#1505) --- .../api/v1/accounts/contacts_controller.rb | 5 +++- .../v1/accounts/contacts_controller_spec.rb | 24 ++++++++++++++++++- spec/factories/contacts.rb | 4 ++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index fbed92bbd..58e301d5a 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -14,7 +14,10 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController def search render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return - contacts = resolved_contacts.where('name LIKE :search OR email LIKE :search OR phone_number LIKE :search', search: "%#{params[:q]}%") + contacts = resolved_contacts.where( + 'name ILIKE :search OR email ILIKE :search OR phone_number ILIKE :search', + search: "%#{params[:q]}%" + ) @contacts_count = contacts.count @contacts = fetch_contact_last_seen_at(contacts) end diff --git a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb index 486a844b4..413b9cd94 100644 --- a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb @@ -90,7 +90,7 @@ RSpec.describe 'Contacts API', type: :request do context 'when it is an authenticated user' do let(:admin) { create(:user, account: account, role: :administrator) } let!(:contact1) { create(:contact, account: account) } - let!(:contact2) { create(:contact, account: account, email: 'test@test.com') } + let!(:contact2) { create(:contact, name: 'testcontact', account: account, email: 'test@test.com') } it 'returns all contacts with contact inboxes' do get "/api/v1/accounts/#{account.id}/contacts/search", @@ -102,6 +102,28 @@ RSpec.describe 'Contacts API', type: :request do expect(response.body).to include(contact2.email) expect(response.body).not_to include(contact1.email) end + + it 'matches the contact ignoring the case in email' do + get "/api/v1/accounts/#{account.id}/contacts/search", + params: { q: 'Test@Test.com' }, + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(contact2.email) + expect(response.body).not_to include(contact1.email) + end + + it 'matches the contact ignoring the case in name' do + get "/api/v1/accounts/#{account.id}/contacts/search", + params: { q: 'TestContact' }, + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(contact2.email) + expect(response.body).not_to include(contact1.email) + end end end diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb index ec5046fbf..eaecd67e9 100644 --- a/spec/factories/contacts.rb +++ b/spec/factories/contacts.rb @@ -2,8 +2,8 @@ FactoryBot.define do factory :contact do - sequence(:name) { |n| "Widget #{n}" } - sequence(:email) { |n| "widget-#{n}@example.com" } + sequence(:name) { |n| "Contact #{n}" } + sequence(:email) { |n| "contact-#{n}@example.com" } phone_number { '+123456789011' } avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') } account