chore: Ingore case while searching contact name, email (#1505)

This commit is contained in:
Pranav Raj S 2020-12-11 23:25:00 +05:30 committed by GitHub
parent b6ca77158c
commit 1ade4f75ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View file

@ -14,7 +14,10 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
def search def search
render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return 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_count = contacts.count
@contacts = fetch_contact_last_seen_at(contacts) @contacts = fetch_contact_last_seen_at(contacts)
end end

View file

@ -90,7 +90,7 @@ RSpec.describe 'Contacts API', type: :request do
context 'when it is an authenticated user' do context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) } let(:admin) { create(:user, account: account, role: :administrator) }
let!(:contact1) { create(:contact, account: account) } 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 it 'returns all contacts with contact inboxes' do
get "/api/v1/accounts/#{account.id}/contacts/search", 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).to include(contact2.email)
expect(response.body).not_to include(contact1.email) expect(response.body).not_to include(contact1.email)
end 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
end end

View file

@ -2,8 +2,8 @@
FactoryBot.define do FactoryBot.define do
factory :contact do factory :contact do
sequence(:name) { |n| "Widget #{n}" } sequence(:name) { |n| "Contact #{n}" }
sequence(:email) { |n| "widget-#{n}@example.com" } sequence(:email) { |n| "contact-#{n}@example.com" }
phone_number { '+123456789011' } phone_number { '+123456789011' }
avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') } avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') }
account account