Browse Source

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
pull/777/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
17ed0f1b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/models/case_log.rb
  2. 8
      app/models/form/setup/questions/scheme_id.rb
  3. 25
      spec/models/case_log_spec.rb
  4. 2
      spec/models/form/setup/questions/location_id_spec.rb
  5. 27
      spec/requests/form_controller_spec.rb
  6. 7
      spec/services/exports/case_log_export_service_spec.rb

7
app/models/case_log.rb

@ -539,10 +539,17 @@ private
self.created_by = nil if created_by.organisation != owning_organisation self.created_by = nil if created_by.organisation != owning_organisation
end end
def reset_scheme
return unless scheme && owning_organisation
self.scheme = nil if scheme.owning_organisation != owning_organisation
end
def reset_invalidated_dependent_fields! def reset_invalidated_dependent_fields!
return unless form return unless form
reset_created_by reset_created_by
reset_scheme
reset_not_routed_questions reset_not_routed_questions
reset_derived_questions reset_derived_questions
end end

8
app/models/form/setup/questions/scheme_id.rb

@ -20,11 +20,11 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
end end
def displayed_answer_options(case_log) def displayed_answer_options(case_log)
return {} unless case_log.created_by organisation = case_log.owning_organisation || case_log.created_by&.organisation
schemes = organisation ? Scheme.select(:id).where(owning_organisation_id: organisation.id) : Scheme.select(:id)
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) 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| 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
end end

25
spec/models/case_log_spec.rb

@ -1875,6 +1875,28 @@ RSpec.describe CaseLog do
expect { case_log.update!(owning_organisation: organisation_2) } expect { case_log.update!(owning_organisation: organisation_2) }
.to change { case_log.reload.created_by }.from(created_by_user).to(nil) .to change { case_log.reload.created_by }.from(created_by_user).to(nil)
end 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
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(:csv_export_file) { File.open("spec/fixtures/files/case_logs_download.csv", "r:UTF-8") }
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { FactoryBot.create(:scheme) }
let(:location) { FactoryBot.create(:location, scheme:, type_of_unit: 6, postcode: "SE11TE") } let(:location) { FactoryBot.create(:location, scheme:, type_of_unit: 6, postcode: "SE11TE") }
let(:user) { FactoryBot.create(:user, organisation: location.scheme.owning_organisation) }
before do before do
Timecop.freeze(Time.utc(2022, 6, 5)) Timecop.freeze(Time.utc(2022, 6, 5))
end end
it "generates a correct csv from a case log" do 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 = csv_export_file.read
expected_content.sub!(/\{id\}/, case_log["id"].to_s) expected_content.sub!(/\{id\}/, case_log["id"].to_s)
expected_content.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s) expected_content.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s)

2
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 context "when getting available locations" do
let(:scheme) { FactoryBot.create(:scheme) } 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 context "when there are no locations" do
it "the displayed_answer_options is an empty hash" do it "the displayed_answer_options is an empty hash" do

27
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?") expect(response.body).to match("What counts as income?")
end end
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("<option value=").count).to eq(6)
end
end
end
end
end end
context "when displaying check answers pages" do context "when displaying check answers pages" do

7
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 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(: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 it "generates an XML export file with the expected content" do
expected_content = replace_entity_ids(case_log, export_file.read) expected_content = replace_entity_ids(case_log, export_file.read)

Loading…
Cancel
Save