Skip to content

Commit

Permalink
Update action_required_by
Browse files Browse the repository at this point in the history
This adds the ability for the status updater class to also update the
actionable by value, ensuring that it's kept up to date with the status
and captures who can perform which tasks.
  • Loading branch information
thomasleese committed Sep 24, 2023
1 parent f0670b6 commit 6ec3e2e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 12 deletions.
53 changes: 42 additions & 11 deletions app/lib/application_form_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def initialize(application_form:, user:)
end

def call
old_status = application_form.status

ActiveRecord::Base.transaction do
application_form.update!(
overdue_further_information:,
Expand All @@ -27,10 +25,24 @@ def call
waiting_on_reference:,
)

next if old_status == new_status
if (old_status = application_form.status) != status
application_form.update!(status:)
create_timeline_event(
event_type: "state_changed",
old_state: old_status,
new_state: status,
)
end

application_form.update!(status: new_status)
create_timeline_event(old_state: old_status, new_state: new_status)
if (old_action_required_by = application_form.action_required_by) !=
action_required_by
application_form.update!(action_required_by:)
create_timeline_event(
event_type: "action_required_by_changed",
old_value: old_action_required_by,
new_value: action_required_by,
)
end
end
end

Expand Down Expand Up @@ -117,8 +129,8 @@ def waiting_on_reference
waiting_on?(requestables: reference_requests)
end

def new_status
@new_status ||=
def status
@status ||=
if dqt_trn_request&.potential_duplicate?
"potential_duplicate_in_dqt"
elsif application_form.withdrawn_at.present?
Expand Down Expand Up @@ -150,6 +162,26 @@ def new_status
end
end

def action_required_by
@action_required_by ||=
if status == "preliminary_check"
"admin"
elsif %w[
potential_duplicate_in_dqt
awarded_pending_checks
overdue
received
assessment_in_progress
submitted
].include?(status)
"assessor"
elsif status == "waiting_on"
"external"
else
"none"
end
end

delegate :assessment,
:dqt_trn_request,
:region,
Expand Down Expand Up @@ -198,17 +230,16 @@ def received?(requestables:)
requestables.reject(&:reviewed?).any?(&:received?)
end

def create_timeline_event(old_state:, new_state:)
def create_timeline_event(event_type:, **kwargs)
creator = user.is_a?(String) ? nil : user
creator_name = user.is_a?(String) ? user : ""

TimelineEvent.create!(
application_form:,
event_type: "state_changed",
event_type:,
creator:,
creator_name:,
new_state:,
old_state:,
**kwargs,
)
end
end
62 changes: 61 additions & 1 deletion spec/lib/application_form_status_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,42 @@
let(:application_form) { create(:application_form) }
let(:user) { create(:staff) }

shared_examples "changes action required by" do |new_action_required_by|
it "changes action required by to #{new_action_required_by}" do
expect { call }.to change(application_form, :action_required_by).to(
new_action_required_by,
)
end

it "records a timeline event" do
expect { call }.to have_recorded_timeline_event(
:action_required_by_changed,
creator: user,
application_form:,
old_value: "none",
new_value: new_action_required_by,
)
end
end

shared_examples "doesn't change action required by" do
it "doesn't change action required by from none" do
expect { call }.to_not change(application_form, :action_required_by).from(
"none",
)
end

it "doesn't record a timeline event" do
expect { call }.to_not have_recorded_timeline_event(
:action_required_by_changed,
creator: user,
application_form:,
)
end
end

shared_examples "changes status" do |new_status|
it "changes the status to #{new_status}" do
it "changes status to #{new_status}" do
expect { call }.to change(application_form, :status).to(new_status)
end

Expand All @@ -31,6 +65,7 @@
create(:dqt_trn_request, :potential_duplicate, application_form:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "potential_duplicate_in_dqt"
end

Expand All @@ -42,6 +77,7 @@
)
end

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

Expand All @@ -53,6 +89,7 @@
)
end

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

Expand All @@ -64,6 +101,7 @@
)
end

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

Expand All @@ -73,6 +111,7 @@
create(:dqt_trn_request, application_form:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "awarded_pending_checks"
end

Expand All @@ -84,6 +123,7 @@
create(:further_information_request, :received, assessment:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "received"

it "changes received_further_information" do
Expand All @@ -102,6 +142,7 @@
create(:further_information_request, :requested, assessment:)
end

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "changes waiting_on_further_information" do
Expand All @@ -120,6 +161,7 @@
create(:professional_standing_request, :requested, assessment:)
end

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "changes waiting_on_professional_standing" do
Expand All @@ -142,6 +184,7 @@
create(:professional_standing_request, :received, assessment:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "submitted"

it "doesn't change received_professional_standing" do
Expand All @@ -158,6 +201,7 @@
create(:professional_standing_request, :received, assessment:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "received"

it "changes received_professional_standing" do
Expand All @@ -177,6 +221,7 @@
create(:qualification_request, :received, assessment:)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "received"

it "changes received_further_information" do
Expand All @@ -195,6 +240,7 @@
create(:qualification_request, :requested, assessment:)
end

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "changes waiting_on_qualification" do
Expand Down Expand Up @@ -228,6 +274,7 @@
)
end

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "doesn't change received_reference" do
Expand Down Expand Up @@ -256,6 +303,7 @@
end

context "and it's the only reference request" do
include_examples "changes action required by", "assessor"
include_examples "changes status", "received"

it "changes received_reference" do
Expand All @@ -269,6 +317,7 @@
context "and there are other reference requests" do
before { create(:reference_request, :requested, assessment:) }

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "doesn't change received_reference" do
Expand Down Expand Up @@ -297,6 +346,7 @@
)
end

include_examples "changes action required by", "assessor"
include_examples "changes status", "received"

it "changes received_reference" do
Expand All @@ -315,6 +365,7 @@
create(:reference_request, :requested, assessment:)
end

include_examples "changes action required by", "external"
include_examples "changes status", "waiting_on"

it "changes waiting_on_reference" do
Expand All @@ -330,12 +381,14 @@
create(:assessment, :started, application_form:)
end

include_examples "changes action required by", "assessor"
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 status", "submitted"
end

Expand All @@ -347,6 +400,8 @@
it "doesn't record a timeline event" do
expect { call }.to_not have_recorded_timeline_event(:state_changed)
end

include_examples "doesn't change action required by"
end

context "when preliminary check is required" do
Expand All @@ -363,6 +418,7 @@
create(:assessment_section, :preliminary, assessment:)
end

include_examples "changes action required by", "admin"
include_examples "changes status", "preliminary_check"

context "when teaching authority provides written statement" do
Expand All @@ -373,11 +429,13 @@
create(:professional_standing_request, assessment:)
end

include_examples "changes action required by", "admin"
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 status", "waiting_on"
end

Expand All @@ -390,11 +448,13 @@
preliminary_assessment_section.reload.update!(passed: false)
end

include_examples "changes action required by", "admin"
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 status", "declined"
end
end
Expand Down

0 comments on commit 6ec3e2e

Please sign in to comment.