diff --git a/app/forms/journeys/further_education_payments/claim_submission_form.rb b/app/forms/journeys/further_education_payments/claim_submission_form.rb index 6feeb5b2bf..757f410c3c 100644 --- a/app/forms/journeys/further_education_payments/claim_submission_form.rb +++ b/app/forms/journeys/further_education_payments/claim_submission_form.rb @@ -4,7 +4,11 @@ class ClaimSubmissionForm < ::ClaimSubmissionBaseForm def save super - ClaimMailer.further_education_payment_provider_verification_email(claim).deliver_later + unless Policies::FurtherEducationPayments.duplicate_claim?(claim) + ClaimMailer.further_education_payment_provider_verification_email(claim).deliver_later + end + + true end private diff --git a/app/models/policies/further_education_payments.rb b/app/models/policies/further_education_payments.rb index bfe08cf440..4da683d7be 100644 --- a/app/models/policies/further_education_payments.rb +++ b/app/models/policies/further_education_payments.rb @@ -28,5 +28,20 @@ def notify_reply_to_id def verification_due_date_for_claim(claim) (claim.created_at + 2.weeks).to_date end + + def duplicate_claim?(claim) + Eligibility + .joins(:claim) + .where(school_id: claim.eligibility.school_id) + .merge( + Claim.where( + first_name: claim.first_name, + surname: claim.surname, + email_address: claim.email_address + ) + .where.not(id: claim.id) + ) + .exists? + end end end diff --git a/spec/forms/journeys/further_education_payments/claim_submission_form_spec.rb b/spec/forms/journeys/further_education_payments/claim_submission_form_spec.rb index 526165d6fd..aa10a438d5 100644 --- a/spec/forms/journeys/further_education_payments/claim_submission_form_spec.rb +++ b/spec/forms/journeys/further_education_payments/claim_submission_form_spec.rb @@ -83,5 +83,20 @@ ) ) end + + it "doesn't email the provider if the claim is a duplicate" do + allow(ClaimVerifierJob).to receive(:perform_later) + + allow(ClaimMailer).to( + receive(:further_education_payment_provider_verification_email) + ).and_return(double(deliver_later: nil)) + + 2.times { described_class.new(journey_session: journey_session).save } + + expect(ClaimMailer).to( + have_received(:further_education_payment_provider_verification_email) + .exactly(1).times + ) + end end end diff --git a/spec/models/policies/further_education_payments_spec.rb b/spec/models/policies/further_education_payments_spec.rb new file mode 100644 index 0000000000..d4f06dc146 --- /dev/null +++ b/spec/models/policies/further_education_payments_spec.rb @@ -0,0 +1,88 @@ +require "rails_helper" + +RSpec.describe Policies::FurtherEducationPayments do + describe ".duplicate_claim?" do + subject { described_class.duplicate_claim?(candidate_claim) } + + let(:existing_claim) do + create( + :claim, + policy: Policies::FurtherEducationPayments, + eligibility: eligibility_1, + first_name: "Edna", + surname: "Krabappel", + email_address: "edna.krabappel@springfield-elementary.edu" + ) + end + + let(:eligibility_1) do + create( + :further_education_payments_eligibility, + school: create(:school) + ) + end + + let(:eligibility_2) do + create( + :further_education_payments_eligibility, + school: school + ) + end + + let(:candidate_claim) do + create( + :claim, + policy: Policies::FurtherEducationPayments, + eligibility: eligibility_2, + first_name: first_name, + surname: surname, + email_address: email_address + ) + end + + context "when the schools are different" do + let(:first_name) { existing_claim.first_name } + let(:surname) { existing_claim.surname } + let(:email_address) { existing_claim.email_address } + let(:school) { create(:school) } + + it { is_expected.to be false } + end + + context "when the first names are different" do + let(:first_name) { existing_claim.first_name + "different" } + let(:surname) { existing_claim.surname } + let(:email_address) { existing_claim.email_address } + let(:school) { existing_claim.eligibility.school } + + it { is_expected.to be false } + end + + context "when the last names are different" do + let(:first_name) { existing_claim.first_name } + let(:surname) { existing_claim.surname + "different" } + let(:email_address) { existing_claim.email_address } + let(:school) { existing_claim.eligibility.school } + + it { is_expected.to be false } + end + + context "when the email_address addresses are different" do + let(:first_name) { existing_claim.first_name } + let(:surname) { existing_claim.surname } + let(:email_address) { "different" + existing_claim.email_address } + let(:school) { existing_claim.eligibility.school } + + it { is_expected.to be false } + end + + context "when the details match" do + let(:first_name) { existing_claim.first_name } + let(:surname) { existing_claim.surname } + let(:email_address) { existing_claim.email_address } + let(:school) { existing_claim.eligibility.school } + + it { is_expected.to be true } + end + end +end