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/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/services/exports/case_log_export_service_spec.rb b/spec/services/exports/case_log_export_service_spec.rb index 36a17fa5d..72e4ee518 100644 --- a/spec/services/exports/case_log_export_service_spec.rb +++ b/spec/services/exports/case_log_export_service_spec.rb @@ -247,9 +247,12 @@ RSpec.describe Exports::CaseLogExportService do context "when exporting a supporting housing case logs in XML" do let(:export_file) { File.open("spec/fixtures/exports/supported_housing_logs.xml", "r:UTF-8") } - let(:location) { FactoryBot.create(:location, :export) } + let(:organisation) { FactoryBot.create(:organisation, provider_type: "LA") } + let(:user) { FactoryBot.create(:user, organisation:) } + let(:scheme) { FactoryBot.create(:scheme, :export, owning_organisation: organisation) } + let(:location) { FactoryBot.create(:location, :export, scheme:) } - let(:case_log) { FactoryBot.create(:case_log, :completed, :export, :sh, scheme: location.scheme, location:) } + let(:case_log) { FactoryBot.create(:case_log, :completed, :export, :sh, scheme:, location:, created_by: user, owning_organisation: organisation) } it "generates an XML export file with the expected content" do expected_content = replace_entity_ids(case_log, export_file.read)