Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add firstname and last name to queries #2553

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Customer < ApplicationRecord
validates :email, email: true, if: :email?

def self.ransackable_attributes(_auth_object = nil)
%w[id name external_id email]
%w[id name firstname lastname external_id email]
end

def display_name
Expand Down
2 changes: 2 additions & 0 deletions app/queries/customers_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def search_params
{
m: 'or',
name_cont: search_term,
firstname_cont: search_term,
lastname_cont: search_term,
external_id_cont: search_term,
email_cont: search_term
}
Expand Down
2 changes: 2 additions & 0 deletions app/queries/invoices_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def search_params

terms.merge(
customer_name_cont: search_term,
customer_firstname_cont: search_term,
customer_lastname_cont: search_term,
customer_external_id_cont: search_term,
customer_email_cont: search_term
)
Expand Down
36 changes: 33 additions & 3 deletions spec/queries/customers_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
let(:organization) { membership.organization }

let(:customer_first) do
create(:customer, organization:, name: 'defgh', external_id: '11', email: '1@example.com')
create(:customer, organization:, name: 'defgh', firstname: 'John', lastname: 'Doe', external_id: '11', email: '1@example.com')
end
let(:customer_second) do
create(:customer, organization:, name: 'abcde', external_id: '22', email: '2@example.com')
create(:customer, organization:, name: 'abcde', firstname: 'Jane', lastname: 'Smith', external_id: '22', email: '2@example.com')
end
let(:customer_third) do
create(:customer, organization:, name: 'presuv', external_id: '33', email: '3@example.com')
create(:customer, organization:, name: 'presuv', firstname: 'Mary', lastname: 'Johnson', external_id: '33', email: '3@example.com')
end

before do
Expand Down Expand Up @@ -71,4 +71,34 @@
end
end
end

context 'when searching for firstname "Jane"' do
let(:search_term) { 'Jane' }

it 'returns only one customer' do
returned_ids = result.customers.pluck(:id)

aggregate_failures do
expect(returned_ids.count).to eq(1)
expect(returned_ids).to include(customer_second.id)
expect(returned_ids).not_to include(customer_first.id)
expect(returned_ids).not_to include(customer_third.id)
end
end
end

context 'when searching for lastname "Johnson"' do
let(:search_term) { 'Johnson' }

it 'returns only one customer' do
returned_ids = result.customers.pluck(:id)

aggregate_failures do
expect(returned_ids.count).to eq(1)
expect(returned_ids).not_to include(customer_first.id)
expect(returned_ids).not_to include(customer_second.id)
expect(returned_ids).to include(customer_third.id)
end
end
end
end
40 changes: 38 additions & 2 deletions spec/queries/invoices_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
let(:filters) { nil }
let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:customer_first) { create(:customer, organization:, name: 'Rick Sanchez', email: 'pickle@hotmail.com') }
let(:customer_second) { create(:customer, organization:, name: 'Morty Smith', email: 'ilovejessica@gmail.com') }
let(:customer_first) { create(:customer, organization:, name: 'Rick Sanchez', firstname: 'RickFirst', lastname: 'SanchezLast', email: 'pickle@hotmail.com') }
let(:customer_second) { create(:customer, organization:, name: 'Morty Smith', firstname: 'MortyFirst', lastname: 'SmithLast', email: 'ilovejessica@gmail.com') }
let(:invoice_first) do
create(
:invoice,
Expand Down Expand Up @@ -491,4 +491,40 @@
end
end
end

context 'when searching for lastname "SanchezLast"' do
let(:search_term) { 'SanchezLast' }

it 'returns the correct invoices for this customer' do
returned_ids = result.invoices.pluck(:id)

aggregate_failures do
expect(returned_ids.count).to eq(4)
expect(returned_ids).to include(invoice_first.id)
expect(returned_ids).not_to include(invoice_second.id)
expect(returned_ids).to include(invoice_third.id)
expect(returned_ids).not_to include(invoice_fourth.id)
expect(returned_ids).to include(invoice_fifth.id)
expect(returned_ids).to include(invoice_sixth.id)
end
end
end

context 'when searching for firstname "MortyFirst"' do
let(:search_term) { 'MortyFirst' }

it 'returns the correct invoices for this customer' do
returned_ids = result.invoices.pluck(:id)

aggregate_failures do
expect(returned_ids.count).to eq(2)
expect(returned_ids).not_to include(invoice_first.id)
expect(returned_ids).to include(invoice_second.id)
expect(returned_ids).not_to include(invoice_third.id)
expect(returned_ids).to include(invoice_fourth.id)
expect(returned_ids).not_to include(invoice_fifth.id)
expect(returned_ids).not_to include(invoice_sixth.id)
end
end
end
end