Skip to content

Commit

Permalink
Update stage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasleese committed Sep 27, 2023
1 parent 2c4da3e commit fbaa384
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/lib/application_form_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/application_form_status_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -78,6 +95,7 @@
end

include_examples "doesn't change action required by"
include_examples "changes stage", "completed"
include_examples "changes status", "withdrawn"
end

Expand All @@ -90,6 +108,7 @@
end

include_examples "doesn't change action required by"
include_examples "changes stage", "completed"
include_examples "changes status", "declined"
end

Expand All @@ -102,6 +121,7 @@
end

include_examples "doesn't change action required by"
include_examples "changes stage", "completed"
include_examples "changes status", "awarded"
end

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -382,13 +415,15 @@
end

include_examples "changes action required by", "assessor"
include_examples "changes stage", "assessment"
include_examples "changes status", "assessment_in_progress"
end

context "with a submitted_at date" do
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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit fbaa384

Please sign in to comment.