diff --git a/app/controllers/concerns/form_submittable.rb b/app/controllers/concerns/form_submittable.rb index fd83f1ba6f..585ea29521 100644 --- a/app/controllers/concerns/form_submittable.rb +++ b/app/controllers/concerns/form_submittable.rb @@ -165,7 +165,7 @@ def log_event(callback_name) end def load_form_if_exists - @form ||= journey.form(journey_session:, params:) + @form ||= journey.form(journey_session:, params:, session:) end end end diff --git a/app/forms/current_school_form.rb b/app/forms/current_school_form.rb index 2ea4305f32..b318de309d 100644 --- a/app/forms/current_school_form.rb +++ b/app/forms/current_school_form.rb @@ -7,7 +7,7 @@ class CurrentSchoolForm < Form validates :current_school_id, presence: {message: i18n_error_message(:select_the_school_you_teach_at)} validate :current_school_must_be_open, if: -> { current_school_id.present? } - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super load_schools diff --git a/app/forms/form.rb b/app/forms/form.rb index 3fddc0005c..6a3bc2ddfd 100644 --- a/app/forms/form.rb +++ b/app/forms/form.rb @@ -8,6 +8,7 @@ class Form attr_accessor :journey attr_accessor :journey_session attr_accessor :params + attr_accessor :session delegate :answers, to: :journey_session @@ -20,7 +21,7 @@ def self.i18n_error_message(path, args = {}) end # TODO RL: remove journey param and pull it from the journey_session - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super assign_attributes(attributes_with_current_value) diff --git a/app/forms/journeys/early_years_payment/provider/authenticated/current_nursery_form.rb b/app/forms/journeys/early_years_payment/provider/authenticated/current_nursery_form.rb index 27f1260394..2fec5bb0f7 100644 --- a/app/forms/journeys/early_years_payment/provider/authenticated/current_nursery_form.rb +++ b/app/forms/journeys/early_years_payment/provider/authenticated/current_nursery_form.rb @@ -12,7 +12,7 @@ class CurrentNurseryForm < Form attr_reader :selectable_nurseries - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super @selectable_nurseries = EligibleEyProvider.for_email(journey_session.answers.provider_email_address) diff --git a/app/forms/journeys/early_years_payment/provider/authenticated/returner_form.rb b/app/forms/journeys/early_years_payment/provider/authenticated/returner_form.rb index d23f867f11..6bd2440924 100644 --- a/app/forms/journeys/early_years_payment/provider/authenticated/returner_form.rb +++ b/app/forms/journeys/early_years_payment/provider/authenticated/returner_form.rb @@ -12,6 +12,9 @@ def save return false if invalid? journey_session.answers.assign_attributes(returning_within_6_months:) + + reset_dependent_answers + journey_session.save! end @@ -22,6 +25,20 @@ def start_date def six_months_before_start_date start_date - 6.months end + + private + + def reset_dependent_answers + if !journey_session.answers.returning_within_6_months + journey_session.answers.assign_attributes( + returner_worked_with_children: nil, + returner_contract_type: nil + ) + + session.fetch(:slugs, {}).delete("returner-worked-with-children") + session.fetch(:slugs, {}).delete("returner-contract-type") + end + end end end end diff --git a/app/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form.rb b/app/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form.rb index 0c7120512b..ea415c1619 100644 --- a/app/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form.rb +++ b/app/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form.rb @@ -12,8 +12,23 @@ def save return false if invalid? journey_session.answers.assign_attributes(returner_worked_with_children:) + + reset_dependent_answers + journey_session.save! end + + private + + def reset_dependent_answers + if !journey_session.answers.returner_worked_with_children + journey_session.answers.assign_attributes( + returner_contract_type: nil + ) + + session.fetch(:slugs, {}).delete("returner-contract-type") + end + end end end end diff --git a/app/forms/journeys/early_years_payment/provider/authenticated/start_date_form.rb b/app/forms/journeys/early_years_payment/provider/authenticated/start_date_form.rb index 9c34e4a204..174be7ebff 100644 --- a/app/forms/journeys/early_years_payment/provider/authenticated/start_date_form.rb +++ b/app/forms/journeys/early_years_payment/provider/authenticated/start_date_form.rb @@ -18,7 +18,7 @@ class StartDateForm < Form if: :start_date validate :start_year_has_four_digits, if: :start_date - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super # Handle setting date from multi part params see diff --git a/app/forms/journeys/get_a_teacher_relocation_payment/entry_date_form.rb b/app/forms/journeys/get_a_teacher_relocation_payment/entry_date_form.rb index 90bf5e222d..db272f8014 100644 --- a/app/forms/journeys/get_a_teacher_relocation_payment/entry_date_form.rb +++ b/app/forms/journeys/get_a_teacher_relocation_payment/entry_date_form.rb @@ -15,7 +15,7 @@ class EntryDateForm < Form message: i18n_error_message(:date_not_in_future) }, if: :date_of_entry - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super # Handle setting date from multi part params see diff --git a/app/forms/journeys/get_a_teacher_relocation_payment/start_date_form.rb b/app/forms/journeys/get_a_teacher_relocation_payment/start_date_form.rb index c391b86a4d..9ac4e2c80e 100644 --- a/app/forms/journeys/get_a_teacher_relocation_payment/start_date_form.rb +++ b/app/forms/journeys/get_a_teacher_relocation_payment/start_date_form.rb @@ -16,7 +16,7 @@ class StartDateForm < Form message: i18n_error_message(:date_not_in_future) }, if: :start_date - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super # Handle setting date from multi part params see diff --git a/app/forms/journeys/teacher_student_loan_reimbursement/claim_school_form.rb b/app/forms/journeys/teacher_student_loan_reimbursement/claim_school_form.rb index 5111bc96b2..7412212e9f 100644 --- a/app/forms/journeys/teacher_student_loan_reimbursement/claim_school_form.rb +++ b/app/forms/journeys/teacher_student_loan_reimbursement/claim_school_form.rb @@ -9,7 +9,7 @@ class ClaimSchoolForm < Form validates :claim_school_id, presence: {message: i18n_error_message(:select_a_school)} validate :claim_school_must_exist, if: -> { claim_school_id.present? } - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super load_schools diff --git a/app/forms/personal_details_form.rb b/app/forms/personal_details_form.rb index 38a6e4bcc0..e1037d773d 100644 --- a/app/forms/personal_details_form.rb +++ b/app/forms/personal_details_form.rb @@ -36,7 +36,7 @@ def future? validates :national_insurance_number, presence: {message: "Enter a National Insurance number in the correct format"} validate :ni_number_is_correct_format - def initialize(journey_session:, journey:, params:) + def initialize(journey_session:, journey:, params:, session: {}) super assign_date_attributes end diff --git a/app/models/journeys/base.rb b/app/models/journeys/base.rb index 4cb1844717..060f0ebbd6 100644 --- a/app/models/journeys/base.rb +++ b/app/models/journeys/base.rb @@ -35,10 +35,10 @@ def slug_sequence self::SlugSequence end - def form(journey_session:, params:) + def form(journey_session:, params:, session:) form = SHARED_FORMS.deep_merge(forms).dig(params[:controller].split("/").last, params[:slug]) - form&.new(journey: self, journey_session:, params:) + form&.new(journey: self, journey_session:, params:, session:) end def forms diff --git a/app/models/journeys/early_years_payment/provider/authenticated/answers_presenter.rb b/app/models/journeys/early_years_payment/provider/authenticated/answers_presenter.rb index 4e14ea8422..8f2939c41b 100644 --- a/app/models/journeys/early_years_payment/provider/authenticated/answers_presenter.rb +++ b/app/models/journeys/early_years_payment/provider/authenticated/answers_presenter.rb @@ -79,7 +79,7 @@ def returner_worked_with_children def returner_contract_type [ - "Contact type for previous role in an early years setting", + "Contract type for previous role in an early years setting", answers.returner_contract_type, "returner-contract-type" ] diff --git a/spec/features/early_years_payment/provider/authenticated/changing_answers_spec.rb b/spec/features/early_years_payment/provider/authenticated/changing_answers_spec.rb index 9984c7a556..bcb1938861 100644 --- a/spec/features/early_years_payment/provider/authenticated/changing_answers_spec.rb +++ b/spec/features/early_years_payment/provider/authenticated/changing_answers_spec.rb @@ -18,4 +18,72 @@ expect(page.current_path).to eq "/early-years-payment-provider/check-your-answers" end + + scenario "changing answers on the check-your-answers page that impacts journey" do + when_early_years_payment_provider_authenticated_journey_configuration_exists + when_early_years_payment_provider_start_journey_completed + + nursery = EligibleEyProvider.last || create(:eligible_ey_provider, primary_key_contact_email_address: "johndoe@example.com") + + visit magic_link + check "I confirm that I have obtained consent from my employee and have provided them with the relevant privacy notice." + click_button "Continue" + + choose nursery.nursery_name + click_button "Continue" + + fill_in "claim-paye-reference-field", with: "123/123456SE90" + click_button "Continue" + + fill_in "First name", with: "Bobby" + fill_in "Last name", with: "Bobberson" + click_button "Continue" + + date = Date.yesterday + fill_in("Day", with: date.day) + fill_in("Month", with: date.month) + fill_in("Year", with: date.year) + click_button "Continue" + + expect(page).to have_content "most of their time in their job working directly with children?" + choose "Yes" + click_button "Continue" + + expect(page).to have_content "work in early years between" + choose "Yes" + click_button "Continue" + + expect(page).to have_content "previous role in an early years setting involve mostly working directly with children?" + choose "Yes" + click_button "Continue" + + expect(page).to have_content "Select the contract type" + choose "casual or temporary" + click_button "Continue" + + fill_in "claim-practitioner-email-address-field", with: "practitioner@example.com" + click_button "Continue" + + expect(page).to have_content "Check your answers before submitting this claim" + find("a[href='#{claim_path(Journeys::EarlyYearsPayment::Provider::Authenticated::ROUTING_NAME, "returner-worked-with-children")}']").click + + expect(page).to have_content("previous role in an early years setting involve mostly working directly with children?") + choose "No" + click_button "Continue" + + expect(page).to have_content "Check your answers before submitting this claim" + expect(page).not_to have_content "Contract type" + find("a[href='#{claim_path(Journeys::EarlyYearsPayment::Provider::Authenticated::ROUTING_NAME, "returner-worked-with-children")}']").click + + expect(page).to have_content("previous role in an early years setting involve mostly working directly with children?") + choose "Yes" + click_button "Continue" + + expect(page).to have_content "Select the contract type" + choose "casual or temporary" + click_button "Continue" + + expect(page).to have_content "Check your answers before submitting this claim" + expect(page).to have_content "Contract type" + end end diff --git a/spec/forms/journeys/early_years_payment/provider/authenticated/returner_form_spec.rb b/spec/forms/journeys/early_years_payment/provider/authenticated/returner_form_spec.rb index caf111158a..0fa1967c87 100644 --- a/spec/forms/journeys/early_years_payment/provider/authenticated/returner_form_spec.rb +++ b/spec/forms/journeys/early_years_payment/provider/authenticated/returner_form_spec.rb @@ -28,12 +28,40 @@ end describe "#save" do - let(:returning_within_6_months) { "true" } + context "when returning within 6 months" do + let(:returning_within_6_months) { "true" } - it "updates the journey session" do - expect { expect(subject.save).to be(true) }.to( - change { journey_session.reload.answers.returning_within_6_months }.to(true) - ) + it "updates the journey session" do + expect { expect(subject.save).to be(true) }.to( + change { journey_session.reload.answers.returning_within_6_months }.to(true) + ) + end + end + + context "when not returning within 6 months" do + let(:returning_within_6_months) { "false" } + + let(:journey_session) do + create( + :early_years_payment_provider_authenticated_session, + answers: + ) + end + + let(:answers) do + { + returner_worked_with_children: true, + returner_contract_type: "casual or temporary" + } + end + + it "updates the journey session and resets dependent answers" do + expect { expect(subject.save).to be(true) }.to( + change { journey_session.reload.answers.returning_within_6_months }.to(false) + .and(change { journey_session.answers.returner_worked_with_children }.to(nil) + .and(change { journey_session.answers.returner_contract_type }.to(nil))) + ) + end end end end diff --git a/spec/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form_spec.rb b/spec/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form_spec.rb index 5171fe5e4c..1a4637d6ef 100644 --- a/spec/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form_spec.rb +++ b/spec/forms/journeys/early_years_payment/provider/authenticated/returner_worked_with_children_form_spec.rb @@ -28,12 +28,36 @@ end describe "#save" do - let(:returner_worked_with_children) { "true" } + context "when returner worked with children" do + let(:returner_worked_with_children) { "true" } - it "updates the journey session" do - expect { expect(subject.save).to be(true) }.to( - change { journey_session.reload.answers.returner_worked_with_children }.to(true) - ) + it "updates the journey session" do + expect { expect(subject.save).to be(true) }.to( + change { journey_session.reload.answers.returner_worked_with_children }.to(true) + ) + end + end + + context "when returner did work with children" do + let(:returner_worked_with_children) { "false" } + let(:journey_session) do + create( + :early_years_payment_provider_authenticated_session, + answers: + ) + end + let(:answers) do + { + returner_contract_type: "casual or temporary" + } + end + + it "updates the journey session and resets dependent answers" do + expect { expect(subject.save).to be(true) }.to( + change { journey_session.reload.answers.returner_worked_with_children }.to(false) + .and(change { journey_session.answers.returner_contract_type }.to(nil)) + ) + end end end end diff --git a/spec/support/steps/early_years_provider_journey_authenticated.rb b/spec/support/steps/early_years_provider_journey_authenticated.rb index 1169c3010e..37f1db6ae7 100644 --- a/spec/support/steps/early_years_provider_journey_authenticated.rb +++ b/spec/support/steps/early_years_provider_journey_authenticated.rb @@ -21,9 +21,11 @@ def when_early_years_payment_provider_authenticated_journey_ready_to_submit fill_in("Year", with: date.year) click_button "Continue" + expect(page).to have_content "most of their time in their job working directly with children?" choose "Yes" click_button "Continue" + expect(page).to have_content "work in early years between" choose "No" click_button "Continue"