From bb5621ed7658b533972ee0c6b531cd8295d0f69a Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Fri, 15 Mar 2024 10:33:13 +0000 Subject: [PATCH] CLDC-3308: Enforce that the declaration has been answered before moving onto other sections in lettings logs (#2314) --- app/models/lettings_log.rb | 9 ++++++- spec/models/lettings_log_spec.rb | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index c8ab06b3d..584158000 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -581,7 +581,14 @@ class LettingsLog < Log end def non_location_setup_questions_completed? - [needstype, renewal, rent_type, startdate, owning_organisation_id, created_by_id].all?(&:present?) + form.setup_sections.all? do |section| + section.subsections.all? do |subsection| + relevant_qs = subsection.applicable_questions(self).reject { |q| optional_fields.include?(q.id) || %w[scheme_id location].include?(q.id) } + relevant_qs.all? do |question| + question.completed?(self) + end + end + end end def resolve! diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index d702a3805..41d9f0646 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -3507,5 +3507,45 @@ RSpec.describe LettingsLog do end end end + + describe "#non_location_setup_questions_completed" do + before do + Timecop.return + allow(FormHandler.instance).to receive(:current_lettings_form).and_call_original + Singleton.__init__(FormHandler) + end + + context "when setup section has been completed" do + let(:lettings_log) { build(:lettings_log, :setup_completed) } + + it "returns true" do + expect(lettings_log).to be_non_location_setup_questions_completed + end + end + + context "when the declaration has not been completed for a 2024 log" do + let(:lettings_log) { build(:lettings_log, :setup_completed, startdate: Time.utc(2024, 10, 1), declaration: nil) } + + it "returns false" do + expect(lettings_log).not_to be_non_location_setup_questions_completed + end + end + + context "when an optional question has not been completed" do + let(:lettings_log) { build(:lettings_log, :setup_completed, propcode: nil) } + + it "returns true" do + expect(lettings_log).to be_non_location_setup_questions_completed + end + end + + context "when scheme and location have not been completed" do + let(:lettings_log) { build(:lettings_log, :setup_completed, :sh, scheme_id: nil, location: nil) } + + it "returns true" do + expect(lettings_log).to be_non_location_setup_questions_completed + end + end + end end # rubocop:enable RSpec/MessageChain