From 17ed0f1b0553b06957021e48b17658250ffe04c7 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Fri, 22 Jul 2022 15:31:14 +0100 Subject: [PATCH] Fix scheme question when case log doesn't have an organisation yet (#774) * Fix scheme question when case log doesn't have an organisation yet * Reset scheme if it doesn't match org --- app/models/case_log.rb | 7 +++++ app/models/form/setup/questions/scheme_id.rb | 8 +++--- spec/models/case_log_spec.rb | 25 ++++++++++++++++- .../form/setup/questions/location_id_spec.rb | 2 +- spec/requests/form_controller_spec.rb | 27 +++++++++++++++++++ .../exports/case_log_export_service_spec.rb | 7 +++-- 6 files changed, 68 insertions(+), 8 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index c8ab60ed0..93e3a4cc4 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -539,10 +539,17 @@ private self.created_by = nil if created_by.organisation != owning_organisation end + def reset_scheme + return unless scheme && owning_organisation + + self.scheme = nil if scheme.owning_organisation != owning_organisation + end + def reset_invalidated_dependent_fields! return unless form reset_created_by + reset_scheme reset_not_routed_questions reset_derived_questions end diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index fe811f28a..cf6173591 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -20,11 +20,11 @@ class Form::Setup::Questions::SchemeId < ::Form::Question end def displayed_answer_options(case_log) - return {} unless case_log.created_by - - user_org_scheme_ids = Scheme.select(:id).where(owning_organisation_id: case_log.created_by.organisation_id).joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.today)).map(&:id) + organisation = case_log.owning_organisation || case_log.created_by&.organisation + schemes = organisation ? Scheme.select(:id).where(owning_organisation_id: organisation.id) : Scheme.select(:id) + filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.today)).map(&:id) answer_options.select do |k, _v| - user_org_scheme_ids.include?(k.to_i) || k.blank? + filtered_scheme_ids.include?(k.to_i) || k.blank? end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index af64ab06f..81da18de9 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1875,6 +1875,28 @@ RSpec.describe CaseLog do expect { case_log.update!(owning_organisation: organisation_2) } .to change { case_log.reload.created_by }.from(created_by_user).to(nil) end + + context "when the organisation selected doesn't match the scheme set" do + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: created_by_user.organisation) } + let(:location) { FactoryBot.create(:location, scheme:) } + let(:case_log) { FactoryBot.create(:case_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) } + + it "clears the scheme value" do + case_log.update!(owning_organisation: organisation_2) + expect(case_log.reload.scheme).to be nil + end + end + + context "when the organisation selected still matches the scheme set" do + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation_2) } + let(:location) { FactoryBot.create(:location, scheme:) } + let(:case_log) { FactoryBot.create(:case_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) } + + it "does not clear the scheme value" do + case_log.update!(owning_organisation: organisation_2) + expect(case_log.reload.scheme_id).to eq(scheme.id) + end + end end end @@ -2240,13 +2262,14 @@ RSpec.describe CaseLog do let(:csv_export_file) { File.open("spec/fixtures/files/case_logs_download.csv", "r:UTF-8") } let(:scheme) { FactoryBot.create(:scheme) } let(:location) { FactoryBot.create(:location, scheme:, type_of_unit: 6, postcode: "SE11TE") } + let(:user) { FactoryBot.create(:user, organisation: location.scheme.owning_organisation) } before do Timecop.freeze(Time.utc(2022, 6, 5)) end it "generates a correct csv from a case log" do - case_log = FactoryBot.create(:case_log, needstype: 2, scheme:, location:) + case_log = FactoryBot.create(:case_log, needstype: 2, scheme:, location:, owning_organisation: scheme.owning_organisation, created_by: user) expected_content = csv_export_file.read expected_content.sub!(/\{id\}/, case_log["id"].to_s) expected_content.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s) diff --git a/spec/models/form/setup/questions/location_id_spec.rb b/spec/models/form/setup/questions/location_id_spec.rb index c8681810f..1d544ebad 100644 --- a/spec/models/form/setup/questions/location_id_spec.rb +++ b/spec/models/form/setup/questions/location_id_spec.rb @@ -39,7 +39,7 @@ RSpec.describe Form::Setup::Questions::LocationId, type: :model do context "when getting available locations" do let(:scheme) { FactoryBot.create(:scheme) } - let(:case_log) { FactoryBot.create(:case_log, scheme:, needstype: 2) } + let(:case_log) { FactoryBot.create(:case_log, owning_organisation: scheme.owning_organisation, scheme:, needstype: 2) } context "when there are no locations" do it "the displayed_answer_options is an empty hash" do diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 0ad7494e0..d6b4a5a30 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -93,6 +93,33 @@ RSpec.describe FormController, type: :request do expect(response.body).to match("What counts as income?") end end + + context "when viewing the setup section schemes page" do + context "when the user is support" do + let(:user) { FactoryBot.create(:user, :support) } + + context "when organisation and user have not been selected yet" do + let(:case_log) do + FactoryBot.create( + :case_log, + owning_organisation: nil, + managing_organisation: nil, + created_by: nil, + needstype: 2, + ) + end + + before do + FactoryBot.create_list(:location, 5) + end + + it "returns an unfiltered list of schemes" do + get "/logs/#{case_log.id}/scheme", headers: headers, params: {} + expect(response.body.scan("