From fbaa384470d97dab04b12166612bf7e7e7eee09c Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Thu, 14 Sep 2023 10:32:52 +0100 Subject: [PATCH] Update stage This adds the ability for the status updater class to also update the stage value, ensuring that it's kept up to date with the status. --- app/lib/application_form_status_updater.rb | 43 +++++++++++++++++ .../application_form_status_updater_spec.rb | 47 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/app/lib/application_form_status_updater.rb b/app/lib/application_form_status_updater.rb index 30b061175f..34466c9921 100644 --- a/app/lib/application_form_status_updater.rb +++ b/app/lib/application_form_status_updater.rb @@ -43,6 +43,15 @@ def call new_value: action_required_by, ) end + + if (old_stage = application_form.stage) != stage + application_form.update!(stage:) + create_timeline_event( + event_type: "stage_changed", + old_value: old_stage, + new_value: stage, + ) + end end end @@ -182,6 +191,40 @@ def action_required_by end end + def stage + @stage ||= + if application_form.withdrawn_at.present? || + application_form.declined_at.present? || + application_form.awarded_at.present? + "completed" + elsif dqt_trn_request.present? + "review" + elsif preliminary_check? || + ( + teaching_authority_provides_written_statement && + waiting_on_professional_standing + ) + "pre_assessment" + elsif overdue_professional_standing || overdue_qualification || + overdue_reference || + ( + !teaching_authority_provides_written_statement && + received_professional_standing + ) || received_qualification || received_reference || + waiting_on_professional_standing || waiting_on_qualification || + waiting_on_reference + "verification" + elsif overdue_further_information || received_further_information || + waiting_on_further_information || + assessment&.any_not_preliminary_section_finished? + "assessment" + elsif application_form.submitted_at.present? + "not_started" + else + "draft" + end + end + delegate :assessment, :dqt_trn_request, :region, diff --git a/spec/lib/application_form_status_updater_spec.rb b/spec/lib/application_form_status_updater_spec.rb index 45e05b8a41..2d785a1a9b 100644 --- a/spec/lib/application_form_status_updater_spec.rb +++ b/spec/lib/application_form_status_updater_spec.rb @@ -40,6 +40,22 @@ end end + shared_examples "changes stage" do |new_stage| + it "changes status to #{new_stage}" do + expect { call }.to change(application_form, :stage).to(new_stage) + end + + it "records a timeline event" do + expect { call }.to have_recorded_timeline_event( + :stage_changed, + creator: user, + application_form:, + old_value: "draft", + new_value: new_stage, + ) + end + end + shared_examples "changes status" do |new_status| it "changes status to #{new_status}" do expect { call }.to change(application_form, :status).to(new_status) @@ -66,6 +82,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "review" include_examples "changes status", "potential_duplicate_in_dqt" end @@ -78,6 +95,7 @@ end include_examples "doesn't change action required by" + include_examples "changes stage", "completed" include_examples "changes status", "withdrawn" end @@ -90,6 +108,7 @@ end include_examples "doesn't change action required by" + include_examples "changes stage", "completed" include_examples "changes status", "declined" end @@ -102,6 +121,7 @@ end include_examples "doesn't change action required by" + include_examples "changes stage", "completed" include_examples "changes status", "awarded" end @@ -112,6 +132,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "review" include_examples "changes status", "awarded_pending_checks" end @@ -124,6 +145,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "assessment" include_examples "changes status", "received" it "changes received_further_information" do @@ -143,6 +165,7 @@ end include_examples "changes action required by", "external" + include_examples "changes stage", "assessment" include_examples "changes status", "waiting_on" it "changes waiting_on_further_information" do @@ -162,6 +185,7 @@ end include_examples "changes action required by", "external" + include_examples "changes stage", "verification" include_examples "changes status", "waiting_on" it "changes waiting_on_professional_standing" do @@ -185,6 +209,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "not_started" include_examples "changes status", "submitted" it "doesn't change received_professional_standing" do @@ -202,6 +227,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "verification" include_examples "changes status", "received" it "changes received_professional_standing" do @@ -222,6 +248,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "verification" include_examples "changes status", "received" it "changes received_further_information" do @@ -241,6 +268,7 @@ end include_examples "changes action required by", "external" + include_examples "changes stage", "verification" include_examples "changes status", "waiting_on" it "changes waiting_on_qualification" do @@ -275,6 +303,7 @@ end include_examples "changes action required by", "external" + include_examples "changes stage", "verification" include_examples "changes status", "waiting_on" it "doesn't change received_reference" do @@ -304,6 +333,7 @@ context "and it's the only reference request" do include_examples "changes action required by", "assessor" + include_examples "changes stage", "verification" include_examples "changes status", "received" it "changes received_reference" do @@ -318,6 +348,7 @@ before { create(:reference_request, :requested, assessment:) } include_examples "changes action required by", "external" + include_examples "changes stage", "verification" include_examples "changes status", "waiting_on" it "doesn't change received_reference" do @@ -347,6 +378,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "verification" include_examples "changes status", "received" it "changes received_reference" do @@ -366,6 +398,7 @@ end include_examples "changes action required by", "external" + include_examples "changes stage", "verification" include_examples "changes status", "waiting_on" it "changes waiting_on_reference" do @@ -382,6 +415,7 @@ end include_examples "changes action required by", "assessor" + include_examples "changes stage", "assessment" include_examples "changes status", "assessment_in_progress" end @@ -389,6 +423,7 @@ before { application_form.update!(submitted_at: Time.zone.now) } include_examples "changes action required by", "assessor" + include_examples "changes stage", "not_started" include_examples "changes status", "submitted" end @@ -400,6 +435,13 @@ it "doesn't record a timeline event" do expect { call }.to_not have_recorded_timeline_event(:state_changed) end + it "doesn't change the stage from draft" do + expect { call }.to_not change(application_form, :stage).from("draft") + end + + it "doesn't record a timeline event" do + expect { call }.to_not have_recorded_timeline_event(:stage_changed) + end include_examples "doesn't change action required by" end @@ -419,6 +461,7 @@ end include_examples "changes action required by", "admin" + include_examples "changes stage", "pre_assessment" include_examples "changes status", "preliminary_check" context "when teaching authority provides written statement" do @@ -430,12 +473,14 @@ end include_examples "changes action required by", "admin" + include_examples "changes stage", "pre_assessment" include_examples "changes status", "preliminary_check" context "when the preliminary check has passed" do before { preliminary_assessment_section.update!(passed: true) } include_examples "changes action required by", "external" + include_examples "changes stage", "pre_assessment" include_examples "changes status", "waiting_on" end @@ -449,12 +494,14 @@ end include_examples "changes action required by", "admin" + include_examples "changes stage", "pre_assessment" include_examples "changes status", "preliminary_check" context "and the application form is declined" do before { application_form.update!(declined_at: Time.zone.now) } include_examples "doesn't change action required by" + include_examples "changes stage", "completed" include_examples "changes status", "declined" end end