diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index 8a63727b8..65ac1f005 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -67,8 +67,19 @@ module DerivedVariables::CaseLogVariables self.new_old = new_or_existing_tenant self.vacdays = property_vacant_days - if is_supported_housing? && (scheme && scheme.locations.size == 1) - self.location = scheme.locations.first + if is_supported_housing? && scheme + if scheme.locations.size == 1 + self.location = scheme.locations.first + end + self.la = location.county + self.postcode_full = location.postcode + self.unittype_gn = form.questions.find { |x| x.id == "unittype_gn" }.answer_options.select { |_key, value| value["value"] == location.type_of_unit }.keys.first + self.builtype = form.questions.find { |x| x.id == "builtype" }.answer_options.select { |_key, value| value["value"] == location.type_of_building }.keys.first + wheelchair_adaptation_map = { 1 => 1, 0 => 2 } + self.wchair = wheelchair_adaptation_map[location.wheelchair_adaptation.to_i] + if is_renewal? + self.voiddate = startdate + end end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 23176a005..635abcb83 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1680,7 +1680,12 @@ RSpec.describe CaseLog do end context "when a case log is a supported housing log" do - before { case_log.needstype = 2 } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } + + before do + case_log.needstype = 2 + allow(FormHandler.instance).to receive(:get_form).and_return(real_2021_2022_form) + end context "and a scheme with a single log is selected" do let(:scheme) { FactoryBot.create(:scheme) } @@ -1694,6 +1699,71 @@ RSpec.describe CaseLog do expect(case_log["location_id"]).to eq(location.id) end end + + context "and not renewal" do + let(:scheme) { FactoryBot.create(:scheme) } + let(:location) { FactoryBot.create(:location, scheme:, county: "E07000041", type_of_unit: "House", type_of_building: "Purpose built", wheelchair_adaptation: 0) } + + let!(:supported_housing_case_log) do + described_class.create!({ + managing_organisation: owning_organisation, + owning_organisation:, + created_by: created_by_user, + needstype: 2, + scheme_id: scheme.id, + location_id: location.id, + renewal: 0, + }) + end + + it "correctly infers and saves la" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT la from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["la"]).to eq(location.county) + end + + it "correctly infers and saves postcode" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT postcode_full from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["postcode_full"]).to eq(location.postcode) + end + + it "correctly infers and saves type of unit" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT unittype_gn from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["unittype_gn"]).to eq(7) + end + + it "correctly infers and saves type of building" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT builtype from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["builtype"]).to eq(1) + end + + it "correctly infers and saves wchair" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT wchair from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["wchair"]).to eq(2) + end + end + + context "and renewal" do + let(:scheme) { FactoryBot.create(:scheme) } + let(:location) { FactoryBot.create(:location, scheme:, county: "E07000041", type_of_unit: "House", type_of_building: "Purpose built", wheelchair_adaptation: 0) } + + let!(:supported_housing_case_log) do + described_class.create!({ + managing_organisation: owning_organisation, + owning_organisation:, + created_by: created_by_user, + needstype: 2, + scheme_id: scheme.id, + location_id: location.id, + renewal: 1, + startdate: Time.zone.now, + }) + end + + it "correcly infers and saves the renewal date" do + record_from_db = ActiveRecord::Base.connection.execute("SELECT voiddate from case_logs where id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["voiddate"]).to eq(supported_housing_case_log.startdate) + end + end end end