Skip to content

Commit

Permalink
Merge pull request #3409 from DFE-Digital/ey-dependent-answers
Browse files Browse the repository at this point in the history
[CAPT-1984] EY clear dependent answers
  • Loading branch information
asmega authored Nov 18, 2024
2 parents f0d5415 + 6b65dee commit 1aa044e
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/form_submittable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/forms/current_school_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/forms/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Form
attr_accessor :journey
attr_accessor :journey_session
attr_accessor :params
attr_accessor :session

delegate :answers, to: :journey_session

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/forms/personal_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/journeys/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]")

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: "[email protected]"
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 1aa044e

Please sign in to comment.