From d525be5a7336a6212aeeae55d36c4b2939ee9788 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Tue, 26 Nov 2024 17:56:10 +0000 Subject: [PATCH] fix EY started_at timestamp --- app/forms/claim_submission_base_form.rb | 6 +---- .../practitioner/claim_submission_form.rb | 16 +++++++++---- .../claim_submission_form_spec.rb | 23 ++++++++++++------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/forms/claim_submission_base_form.rb b/app/forms/claim_submission_base_form.rb index 58f34f3e8d..0b6059065c 100644 --- a/app/forms/claim_submission_base_form.rb +++ b/app/forms/claim_submission_base_form.rb @@ -39,7 +39,7 @@ def main_eligibility end def build_claim - new_or_find_claim.tap do |claim| + Claim.new.tap do |claim| claim.eligibility ||= main_eligibility claim.started_at = journey_session.created_at answers.attributes.each do |name, value| @@ -50,10 +50,6 @@ def build_claim end end - def new_or_find_claim - Claim.new - end - def eligibilities @eligibilities ||= journey::POLICIES.map do |policy| policy::Eligibility.new.tap do |eligibility| diff --git a/app/forms/journeys/early_years_payment/practitioner/claim_submission_form.rb b/app/forms/journeys/early_years_payment/practitioner/claim_submission_form.rb index 72cdaf709d..f26e591478 100644 --- a/app/forms/journeys/early_years_payment/practitioner/claim_submission_form.rb +++ b/app/forms/journeys/early_years_payment/practitioner/claim_submission_form.rb @@ -18,10 +18,18 @@ def generate_policy_options_provided [] end - def new_or_find_claim - (Claim.find_by(reference: journey_session.answers.reference_number) || Claim.new).tap do |c| - if c.eligibility - c.eligibility.practitioner_claim_started_at = journey_session.answers.practitioner_claim_started_at + def existing_or_new_claim + Claim.find_by(reference: journey_session.answers.reference_number) || Claim.new + end + + def build_claim + existing_or_new_claim.tap do |claim| + claim.eligibility ||= main_eligibility + claim.eligibility.practitioner_claim_started_at = journey_session.answers.practitioner_claim_started_at + answers.attributes.each do |name, value| + if claim.respond_to?(:"#{name}=") + claim.public_send(:"#{name}=", value) + end end end end diff --git a/spec/forms/journeys/early_years_payment/practitioner/claim_submission_form_spec.rb b/spec/forms/journeys/early_years_payment/practitioner/claim_submission_form_spec.rb index aac21f5213..43778ad201 100644 --- a/spec/forms/journeys/early_years_payment/practitioner/claim_submission_form_spec.rb +++ b/spec/forms/journeys/early_years_payment/practitioner/claim_submission_form_spec.rb @@ -5,23 +5,23 @@ create(:journey_configuration, :early_years_payment_practitioner) end - let(:journey) { Journeys::EarlyYearsPayment::Practitioner } + subject { described_class.new(journey_session: journey_session) } + let(:journey) { Journeys::EarlyYearsPayment::Practitioner } let(:journey_session) { create(:early_years_payment_practitioner_session, answers: answers) } - let(:form) { described_class.new(journey_session: journey_session) } - let!(:existing_claim) { create(:claim, :early_years_provider_submitted, policy: Policies::EarlyYearsPayments) } + let!(:existing_claim) { create(:claim, :early_years_provider_submitted, policy: Policies::EarlyYearsPayments, started_at:) } + let(:started_at) { Time.new(2000, 1, 1, 12) } describe "#save" do - subject { form.save } - - let(:claim) { form.claim } + let(:claim) { subject.claim } let(:eligibility) { claim.eligibility } let(:answers) { build(:early_years_payment_practitioner_answers, :submittable, reference_number: existing_claim.reference) } it { is_expected.to be_truthy } it "saves some answers into the Claim model" do - subject + subject.save + expect(claim.submitted_at).to be_present expect(claim.eligibility_type).to eq "Policies::EarlyYearsPayments::Eligibility" expect(claim.first_name).to eq answers.first_name @@ -35,8 +35,15 @@ end it "saves some answers into the Eligibility model" do - subject + subject.save + expect(eligibility.practitioner_claim_started_at).to be_present end + + it "does not overwrite claim#started_at" do + expect { + subject.save + }.not_to change { existing_claim.reload.started_at } + end end end