diff --git a/app/lib/application_form_status_updater.rb b/app/lib/application_form_status_updater.rb index f96c2b5286..17cbf1f158 100644 --- a/app/lib/application_form_status_updater.rb +++ b/app/lib/application_form_status_updater.rb @@ -10,30 +10,6 @@ def initialize(application_form:, user:) def call ActiveRecord::Base.transaction do - application_form.update!( - overdue_further_information:, - overdue_professional_standing: overdue_lops, - overdue_qualification:, - overdue_reference:, - received_further_information:, - received_professional_standing: received_lops, - received_qualification:, - received_reference:, - waiting_on_further_information:, - waiting_on_professional_standing: waiting_on_lops, - waiting_on_qualification:, - waiting_on_reference:, - ) - - 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 - if (old_action_required_by = application_form.action_required_by) != action_required_by application_form.update!(action_required_by:) @@ -61,117 +37,6 @@ def call attr_reader :application_form, :user - def overdue_further_information - overdue?(requestables: further_information_requests) - end - - def overdue_lops - return false if teaching_authority_provides_written_statement - overdue?(requestables: professional_standing_requests) - end - - def overdue_qualification - overdue?(requestables: qualification_requests) - end - - def overdue_reference - return false if references_verified - overdue?(requestables: reference_requests) - end - - def received_further_information - received?(requestables: further_information_requests) - end - - def received_lops - return false if teaching_authority_provides_written_statement - - professional_standing_requests - .reject(&:reviewed?) - .any? do |requestable| - requestable.received? || requestable.ready_for_review - end - end - - def received_qualification - received?(requestables: qualification_requests) - end - - def received_reference - return false unless received?(requestables: reference_requests) - - received_requests = reference_requests.filter(&:received?) - - months_count = - WorkHistoryDuration.new( - work_history_relation: - application_form.work_histories.where( - id: received_requests.map(&:work_history_id), - ), - ).count_months - - most_recent_reference_request = - reference_requests.max_by { |request| request.work_history.start_date } - - if months_count < 9 - false - elsif months_count >= 20 && - (region.checks_available? || most_recent_reference_request&.received?) - true - else - reference_requests.filter(&:requested?).empty? - end - end - - def waiting_on_further_information - waiting_on?(requestables: further_information_requests) - end - - def waiting_on_lops - waiting_on?(requestables: professional_standing_requests) - end - - def waiting_on_qualification - waiting_on?(requestables: qualification_requests) - end - - def waiting_on_reference - return false if references_verified - waiting_on?(requestables: reference_requests) - end - - def status - @status ||= - if dqt_trn_request&.potential_duplicate? - "potential_duplicate_in_dqt" - elsif application_form.withdrawn_at.present? - "withdrawn" - elsif application_form.declined_at.present? - "declined" - elsif application_form.awarded_at.present? - "awarded" - elsif dqt_trn_request.present? - "awarded_pending_checks" - elsif preliminary_check? - "preliminary_check" - elsif overdue_further_information || overdue_lops || - overdue_qualification || overdue_reference - "overdue" - elsif received_further_information || received_lops || - received_qualification || received_reference - "received" - elsif waiting_on_further_information || waiting_on_lops || - waiting_on_qualification || waiting_on_reference - "waiting_on" - elsif assessment&.any_not_preliminary_section_finished? - "assessment_in_progress" - elsif application_form.submitted_at.present? - "submitted" - else - "draft" - end - end - def action_required_by @action_required_by ||= if application_form.withdrawn_at.present? || @@ -276,6 +141,85 @@ def requestable_statuses .filter { |column| send(column) } end + def overdue_further_information + overdue?(requestables: further_information_requests) + end + + def overdue_lops + return false if teaching_authority_provides_written_statement + overdue?(requestables: professional_standing_requests) + end + + def overdue_qualification + overdue?(requestables: qualification_requests) + end + + def overdue_reference + return false if references_verified + overdue?(requestables: reference_requests) + end + + def received_further_information + received?(requestables: further_information_requests) + end + + def received_lops + return false if teaching_authority_provides_written_statement + + professional_standing_requests + .reject(&:reviewed?) + .any? do |requestable| + requestable.received? || requestable.ready_for_review + end + end + + def received_qualification + received?(requestables: qualification_requests) + end + + def received_reference + return false unless received?(requestables: reference_requests) + + received_requests = reference_requests.filter(&:received?) + + months_count = + WorkHistoryDuration.new( + work_history_relation: + application_form.work_histories.where( + id: received_requests.map(&:work_history_id), + ), + ).count_months + + most_recent_reference_request = + reference_requests.max_by { |request| request.work_history.start_date } + + if months_count < 9 + false + elsif months_count >= 20 && + (region.checks_available? || most_recent_reference_request&.received?) + true + else + reference_requests.filter(&:requested?).empty? + end + end + + def waiting_on_further_information + waiting_on?(requestables: further_information_requests) + end + + def waiting_on_lops + waiting_on?(requestables: professional_standing_requests) + end + + def waiting_on_qualification + waiting_on?(requestables: qualification_requests) + end + + def waiting_on_reference + return false if references_verified + waiting_on?(requestables: reference_requests) + end + def further_information_requests @further_information_requests ||= assessment&.further_information_requests&.to_a || [] diff --git a/spec/lib/application_form_status_updater_spec.rb b/spec/lib/application_form_status_updater_spec.rb index 5bf8ec1877..fc6437e449 100644 --- a/spec/lib/application_form_status_updater_spec.rb +++ b/spec/lib/application_form_status_updater_spec.rb @@ -62,22 +62,6 @@ 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) - end - - it "records a timeline event" do - expect { call }.to have_recorded_timeline_event( - :state_changed, - creator: user, - application_form:, - old_state: "draft", - new_state: new_status, - ) - end - end - describe "#call" do subject(:call) { described_class.call(application_form:, user:) } @@ -90,7 +74,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "review" include_examples "changes statuses", %w[potential_duplicate_in_dqt] - include_examples "changes status", "potential_duplicate_in_dqt" end context "with a withdrawn_at date" do @@ -104,7 +87,6 @@ include_examples "doesn't change action required by" include_examples "changes stage", "completed" include_examples "changes statuses", %w[withdrawn] - include_examples "changes status", "withdrawn" end context "with a declined_at date" do @@ -118,7 +100,6 @@ include_examples "doesn't change action required by" include_examples "changes stage", "completed" include_examples "changes statuses", %w[declined] - include_examples "changes status", "declined" end context "with an awarded_at date" do @@ -132,7 +113,6 @@ include_examples "doesn't change action required by" include_examples "changes stage", "completed" include_examples "changes statuses", %w[awarded] - include_examples "changes status", "awarded" end context "with a DQT TRN request" do @@ -144,7 +124,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "review" include_examples "changes statuses", %w[awarded_pending_checks] - include_examples "changes status", "awarded_pending_checks" end context "with a received further information request" do @@ -158,14 +137,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "assessment" include_examples "changes statuses", %w[received_further_information] - include_examples "changes status", "received" - - it "changes received_further_information" do - expect { call }.to change( - application_form, - :received_further_information, - ).from(false).to(true) - end end context "with a requested further information request" do @@ -179,14 +150,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "assessment" include_examples "changes statuses", %w[waiting_on_further_information] - include_examples "changes status", "waiting_on" - - it "changes waiting_on_further_information" do - expect { call }.to change( - application_form, - :waiting_on_further_information, - ).from(false).to(true) - end end context "with a requested profession standing request" do @@ -200,14 +163,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "verification" include_examples "changes statuses", %w[waiting_on_lops] - include_examples "changes status", "waiting_on" - - it "changes waiting_on_professional_standing" do - expect { call }.to change( - application_form, - :waiting_on_professional_standing, - ).from(false).to(true) - end end context "with a received profession standing request" do @@ -225,13 +180,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "not_started" include_examples "changes statuses", %w[assessment_not_started] - - it "doesn't change received_professional_standing" do - expect { call }.to_not change( - application_form, - :received_professional_standing, - ).from(false) - end end context "when the teaching authority doesn't provide the written statement" do @@ -243,14 +191,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "verification" include_examples "changes statuses", %w[received_lops] - include_examples "changes status", "received" - - it "changes received_professional_standing" do - expect { call }.to change( - application_form, - :received_professional_standing, - ).from(false).to(true) - end end end @@ -265,14 +205,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "verification" include_examples "changes statuses", %w[received_qualification] - include_examples "changes status", "received" - - it "changes received_further_information" do - expect { call }.to change( - application_form, - :received_qualification, - ).from(false).to(true) - end end context "with a requested qualification request" do @@ -286,14 +218,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "verification" include_examples "changes statuses", %w[waiting_on_qualification] - include_examples "changes status", "waiting_on" - - it "changes waiting_on_qualification" do - expect { call }.to change( - application_form, - :waiting_on_qualification, - ).from(false).to(true) - end end context "with a received reference request" do @@ -322,14 +246,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "verification" include_examples "changes statuses", %w[waiting_on_reference] - include_examples "changes status", "waiting_on" - - it "doesn't change received_reference" do - expect { call }.to_not change( - application_form, - :received_reference, - ).from(false) - end end context "with less than 20 months" do @@ -353,14 +269,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "verification" include_examples "changes statuses", %w[received_reference] - include_examples "changes status", "received" - - it "changes received_reference" do - expect { call }.to change( - application_form, - :received_reference, - ).from(false).to(true) - end end context "and there are other reference requests" do @@ -369,14 +277,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "verification" include_examples "changes statuses", %w[waiting_on_reference] - include_examples "changes status", "waiting_on" - - it "doesn't change received_reference" do - expect { call }.to_not change( - application_form, - :received_reference, - ).from(false) - end end end @@ -400,13 +300,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "verification" include_examples "changes statuses", %w[received_reference] - include_examples "changes status", "received" - - it "changes received_reference" do - expect { call }.to change(application_form, :received_reference).from( - false, - ).to(true) - end end end @@ -421,13 +314,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "verification" include_examples "changes statuses", %w[waiting_on_reference] - include_examples "changes status", "waiting_on" - - it "changes waiting_on_reference" do - expect { call }.to change(application_form, :waiting_on_reference).from( - false, - ).to(true) - end end context "with a started assessment" do @@ -439,7 +325,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "assessment" include_examples "changes statuses", %w[assessment_in_progress] - include_examples "changes status", "assessment_in_progress" end context "with an unstarted assessment" do @@ -451,7 +336,6 @@ include_examples "changes action required by", "assessor" include_examples "changes stage", "not_started" include_examples "changes statuses", %w[assessment_not_started] - include_examples "changes status", "submitted" end context "when status is unchanged" do @@ -497,7 +381,6 @@ include_examples "changes action required by", "admin" include_examples "changes stage", "pre_assessment" include_examples "changes statuses", %w[preliminary_check] - include_examples "changes status", "preliminary_check" context "when teaching authority provides written statement" do before do @@ -511,7 +394,6 @@ include_examples "changes stage", "pre_assessment" include_examples "changes statuses", %w[preliminary_check waiting_on_lops] - include_examples "changes status", "preliminary_check" context "when the preliminary check has passed" do before { preliminary_assessment_section.update!(passed: true) } @@ -519,7 +401,6 @@ include_examples "changes action required by", "external" include_examples "changes stage", "pre_assessment" include_examples "changes statuses", %w[waiting_on_lops] - include_examples "changes status", "waiting_on" end context "when the preliminary check has failed" do @@ -535,7 +416,6 @@ include_examples "changes stage", "pre_assessment" include_examples "changes statuses", %w[preliminary_check waiting_on_lops] - include_examples "changes status", "preliminary_check" context "and the application form is declined" do before { application_form.update!(declined_at: Time.zone.now) } @@ -543,7 +423,6 @@ include_examples "doesn't change action required by" include_examples "changes stage", "completed" include_examples "changes statuses", %w[declined] - include_examples "changes status", "declined" end end end