diff --git a/app/models/claim/search.rb b/app/models/claim/search.rb index 715d30f3f..8e1468817 100644 --- a/app/models/claim/search.rb +++ b/app/models/claim/search.rb @@ -30,9 +30,23 @@ def claims Payment.where(id: search_term) ) + claims_matched_on_ey_provider_details = Claim + .joins(:early_years_payment_eligibility) + .joins("JOIN eligible_ey_providers ON eligible_ey_providers.urn = early_years_payment_eligibilities.nursery_urn") + .where( + <<~SQL, search_term: search_term + LOWER(claims.practitioner_email_address) = LOWER(:search_term) + OR LOWER(early_years_payment_eligibilities.provider_email_address) = LOWER(:search_term) + OR LOWER(eligible_ey_providers.nursery_name) = LOWER(:search_term) + OR LOWER(eligible_ey_providers.primary_key_contact_email_address) = LOWER(:search_term) + OR LOWER(eligible_ey_providers.secondary_contact_email_address) = LOWER(:search_term) + SQL + ) + claim_match_query .or(Claim.where(eligibility_id: eligibility_ids)) .or(Claim.where(id: claims_matched_on_payment_ids)) + .or(Claim.where(id: claims_matched_on_ey_provider_details)) end private diff --git a/spec/models/claim/search_spec.rb b/spec/models/claim/search_spec.rb index 30b9ce96a..5baa006bf 100644 --- a/spec/models/claim/search_spec.rb +++ b/spec/models/claim/search_spec.rb @@ -172,4 +172,111 @@ it { is_expected.not_to include(claim_3) } it { is_expected.not_to include(claim_4) } end + + context "search by EY provider details" do + let!(:provider_1) do + create( + :eligible_ey_provider, + primary_key_contact_email_address: "test1-nursery@example.com" + ) + end + + let!(:provider_2) do + create( + :eligible_ey_provider, + secondary_contact_email_address: "test2-nursery@example.com" + ) + end + + let!(:provider_3) do + create( + :eligible_ey_provider, + nursery_name: "Test Nursery" + ) + end + + let!(:claim_1) do + create( + :claim, + policy: Policies::EarlyYearsPayments, + eligibility_attributes: { + nursery_urn: provider_1.urn + } + ) + end + + let!(:claim_2) do + create( + :claim, + policy: Policies::EarlyYearsPayments, + eligibility_attributes: { + nursery_urn: provider_2.urn + } + ) + end + + let!(:claim_3) do + create( + :claim, + policy: Policies::EarlyYearsPayments, + eligibility_attributes: { + nursery_urn: provider_3.urn + } + ) + end + + let!(:claim_4) do + create( + :claim, + policy: Policies::EarlyYearsPayments, + eligibility_attributes: { + provider_email_address: "provider-email-address@example.com", + nursery_urn: create(:eligible_ey_provider).urn + } + ) + end + + let!(:claim_5) do + create( + :claim, + policy: Policies::EarlyYearsPayments, + practitioner_email_address: "pracitioner@example.com", + eligibility_attributes: { + nursery_urn: create(:eligible_ey_provider).urn + } + ) + end + + subject { search.claims } + + context "when searching for a primary key contact email address" do + let(:query) { provider_1.primary_key_contact_email_address } + + it { is_expected.to match_array([claim_1]) } + end + + context "when searching for a secondary key contact email address" do + let(:query) { provider_2.secondary_contact_email_address } + + it { is_expected.to match_array([claim_2]) } + end + + context "when searching for a nursery name" do + let(:query) { provider_3.nursery_name } + + it { is_expected.to match_array([claim_3]) } + end + + context "when searching for a provider email address" do + let(:query) { claim_4.eligibility.provider_email_address } + + it { is_expected.to match_array([claim_4]) } + end + + context "when searching for a practitioner email address" do + let(:query) { claim_5.practitioner_email_address } + + it { is_expected.to match_array([claim_5]) } + end + end end