Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always record assessment section timeline events #2226

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/forms/assessor_interface/assessment_section_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def save
return false unless valid?

AssessAssessmentSection.call(
assessment_section:,
assessment_section,
user:,
passed:
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ def english_language_section(assessment)
private

def save_english_language_status
return true unless update_english_language_status?

AssessAssessmentSection.call(
assessment_section:
if update_english_language_status?
AssessAssessmentSection.call(
self.class.english_language_section(assessment_section.assessment),
user:,
passed: english_language_section_passed,
)
user:,
passed: english_language_section_passed,
)
end

true
end

def update_english_language_status?
Expand Down
6 changes: 3 additions & 3 deletions app/lib/fake_data/application_form_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def pre_assess_application_form(decline:)
date_generator.travel_to_next_short do
if decline
AssessAssessmentSection.call(
assessment_section:,
assessment_section,
user:,
passed: false,
selected_failure_reasons: {
Expand All @@ -159,7 +159,7 @@ def pre_assess_application_form(decline:)
},
)
else
AssessAssessmentSection.call(assessment_section:, user:, passed: true)
AssessAssessmentSection.call(assessment_section, user:, passed: true)
end
end
end
Expand Down Expand Up @@ -235,7 +235,7 @@ def assess_application_form(decline: false, further_information: false)
)
end

AssessAssessmentSection.call(assessment_section:, user:, **params)
AssessAssessmentSection.call(assessment_section, user:, **params)
end
end
end
Expand Down
59 changes: 27 additions & 32 deletions app/services/assess_assessment_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AssessAssessmentSection
include ServicePattern

def initialize(
assessment_section:,
assessment_section,
user:,
passed:,
selected_failure_reasons: {}
Expand All @@ -19,35 +19,12 @@ def call
old_status = assessment_section.status

ActiveRecord::Base.transaction do
selected_keys = selected_failure_reasons.keys

assessment_section
.selected_failure_reasons
.where.not(key: selected_keys)
.destroy_all

selected_failure_reasons.each do |key, assessor_feedback|
failure_reason =
assessment_section.selected_failure_reasons.find_or_initialize_by(
key:,
)
failure_reason.update!(assessor_feedback: assessor_feedback[:notes])
next unless assessor_feedback[:work_histories]
failure_reason.update!(
work_histories: assessor_feedback[:work_histories],
)
end

unless assessment_section.update(passed:, assessed_at: Time.zone.now)
next false
end

update_selected_failure_reasons
update_passed_and_assessed_at
update_application_form_assessor
create_timeline_event(old_status:)
update_assessment_started_at
update_application_form_state

true
end
end

Expand All @@ -58,6 +35,28 @@ def call
delegate :assessment, to: :assessment_section
delegate :application_form, to: :assessment

def update_selected_failure_reasons
selected_keys = selected_failure_reasons.keys

assessment_section
.selected_failure_reasons
.where.not(key: selected_keys)
.destroy_all

selected_failure_reasons.each do |key, assessor_feedback|
failure_reason =
assessment_section.selected_failure_reasons.find_or_initialize_by(key:)

failure_reason.update!(assessor_feedback: assessor_feedback[:notes])
next unless assessor_feedback[:work_histories]
failure_reason.update!(work_histories: assessor_feedback[:work_histories])
end
end

def update_passed_and_assessed_at
assessment_section.update!(passed:, assessed_at: Time.zone.now)
end

def update_application_form_assessor
if application_form.assessor.nil?
AssignApplicationFormAssessor.call(
Expand All @@ -69,22 +68,18 @@ def update_application_form_assessor
end

def create_timeline_event(old_status:)
new_status = assessment_section.status
return if old_status == new_status

CreateTimelineEvent.call(
"assessment_section_recorded",
application_form:,
user:,
assessment_section:,
old_value: old_status,
new_value: new_status,
new_value: assessment_section.status,
)
end

def update_assessment_started_at
return if assessment.started_at
assessment.update!(started_at: Time.zone.now)
assessment.update!(started_at: Time.zone.now) if assessment.started_at.nil?
end

def update_application_form_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

it "calls the update assessment service" do
expect(AssessAssessmentSection).to receive(:call).with(
assessment_section:,
assessment_section,
user:,
passed: true,
selected_failure_reasons: {
Expand All @@ -163,7 +163,7 @@

it "calls the update assessment service" do
expect(AssessAssessmentSection).to receive(:call).with(
assessment_section:,
assessment_section,
user:,
passed: false,
selected_failure_reasons: {
Expand Down
Loading
Loading