Skip to content

Commit

Permalink
Merge pull request #2793 from DFE-Digital/current-school-journey
Browse files Browse the repository at this point in the history
Store current school on the journey session
  • Loading branch information
felixclack authored Jun 10, 2024
2 parents 30da7fc + 05c54af commit e43a4df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
17 changes: 10 additions & 7 deletions app/forms/current_school_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ def initialize(claim:, journey_session:, journey:, params:)
super

load_schools
self.current_school_id = permitted_params[:current_school_id]
end

def save
return false unless valid?

update!({"eligibility_attributes" => {"current_school_id" => current_school_id}})
journey_session.answers.assign_attributes(current_school_id:)
journey_session.save!
update!("eligibility_attributes" => {"current_school_id" => current_school_id})
end

def current_school_name
claim.eligibility.current_school_name
end
delegate :name, to: :current_school, prefix: true, allow_nil: true

def no_search_results?
params[:school_search].present? && errors.empty?
end

private

def current_school
@current_school ||= School.find_by(id: current_school_id)
end

def load_schools
return unless params[:school_search]

Expand All @@ -40,8 +43,8 @@ def load_schools
end

def current_school_must_be_open
if (school = School.find_by(id: current_school_id))
errors.add(:current_school_id, i18n_errors_path("the_selected_school_is_closed")) unless school.open?
if current_school
errors.add(:current_school_id, i18n_errors_path("the_selected_school_is_closed")) unless current_school.open?
else
errors.add(:current_school_id, i18n_errors_path("school_not_found"))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ def induction_question_required?
end

def ecp_school_selected?
return false unless claim.eligibility.current_school
school = answers&.current_school || claim.eligibility.current_school
return false unless school

Policies::EarlyCareerPayments::SchoolEligibility.new(claim.eligibility.current_school).eligible?
Policies::EarlyCareerPayments::SchoolEligibility.new(school).eligible?
end

# We only retrieve dqt teacher status when the user is signed in with DfE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def employment

def identity_confirmation
[
["Current school", eligibility.current_school.name],
["Contact number", eligibility.current_school.phone_number]
["Current school", eligibility.current_school&.name],
["Contact number", eligibility.current_school&.phone_number]
]
end

Expand Down
7 changes: 5 additions & 2 deletions spec/forms/current_school_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CurrentClaim.new(claims: claims)
end

let(:journey_session) { build(:"#{journey::I18N_NAMESPACE}_session") }
let(:journey_session) { create(:"#{journey::I18N_NAMESPACE}_session") }

let(:slug) { "current-school" }

Expand Down Expand Up @@ -82,9 +82,12 @@
describe "#save" do
context "current_school_id submitted" do
let(:params) { ActionController::Parameters.new({slug: slug, claim: {current_school_id: school.id}}) }

let(:school) { create(:school, :eligible_for_journey, journey: journey) }

it "updates the journey_session" do
expect { form.save }.to change { journey_session.reload.answers.current_school_id }.to(school.id)
end

context "claim eligibility didn't have current_school" do
let(:current_claim) do
claims = journey::POLICIES.map { |policy| create(:claim, policy: policy) }
Expand Down

0 comments on commit e43a4df

Please sign in to comment.