From 8bd39704f940a2335858f8d0e69732c16d77464e Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 11 Jul 2022 11:33:10 +0100 Subject: [PATCH] Cldc 809 supported housing questions (#713) * infer case_log values from schemes * only display relevant property information questions * Add unittype_sh column * Display the correct number of completed sections * fix schema and migration * Fix test failing in CI This test was failing due to some different between UTC and BST time as the record from the DB comes back with a UTC timezone. Comparing the integer values of both will ensure that both timestamps are identical and should fix the issue in the CI where it thinks the values are different * only run query for scheme locations size once Co-authored-by: Dushan * extract TYPE_OF_UNIT_MAP * lint * Use find insted of select Co-authored-by: Dushan Despotovic --- app/helpers/tasklist_helper.rb | 4 +- .../derived_variables/case_log_variables.rb | 28 +++++++- app/models/form/subsection.rb | 2 +- app/views/case_logs/_tasklist.html.erb | 16 +++-- config/forms/2021_2022.json | 35 +++++++-- db/migrate/20220706104313_add_unit_type_sh.rb | 7 ++ db/schema.rb | 3 +- db/seeds.rb | 2 +- spec/factories/location.rb | 2 +- spec/features/form/tasklist_page_spec.rb | 2 +- spec/fixtures/exports/case_logs.csv | 4 +- spec/fixtures/exports/case_logs.xml | 1 + spec/helpers/tasklist_helper_spec.rb | 2 +- spec/models/case_log_spec.rb | 72 ++++++++++++++++++- 14 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20220706104313_add_unit_type_sh.rb diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 5ca37b56c..d9b402221 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -6,9 +6,9 @@ module TasklistHelper end def get_subsections_count(case_log, status = :all) - return case_log.form.subsections.count if status == :all + return case_log.form.subsections.count { |subsection| subsection.applicable_questions(case_log).count.positive? } if status == :all - case_log.form.subsections.count { |subsection| subsection.status(case_log) == status } + case_log.form.subsections.count { |subsection| subsection.status(case_log) == status && subsection.applicable_questions(case_log).count.positive? } end def next_page_or_check_answers(subsection, case_log, current_user) diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index 8a63727b8..33fcef3d1 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -1,5 +1,13 @@ module DerivedVariables::CaseLogVariables RENT_TYPE_MAPPING = { 0 => 1, 1 => 2, 2 => 2, 3 => 3, 4 => 3, 5 => 3 }.freeze + TYPE_OF_UNIT_MAP = { + "Self-contained flat or bedsit" => 1, + "Self-contained flat or bedsit with common facilities" => 2, + "Shared flat" => 3, + "Shared house or hostel" => 4, + "Bungalow" => 5, + "Self-contained house" => 6, + }.freeze def supported_housing_schemes_enabled? FeatureToggle.supported_housing_schemes_enabled? @@ -8,7 +16,8 @@ module DerivedVariables::CaseLogVariables def scheme_has_multiple_locations? return false unless scheme - scheme.locations.size > 1 + @scheme_locations_count ||= scheme.locations.size + @scheme_locations_count > 1 end def set_derived_fields! @@ -67,8 +76,21 @@ 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 + if location + self.la = location.county + self.postcode_full = location.postcode + self.unittype_sh = TYPE_OF_UNIT_MAP[location.type_of_unit] + self.builtype = form.questions.find { |x| x.id == "builtype" }.answer_options.find { |_key, value| value["value"] == location.type_of_building }.first + wheelchair_adaptation_map = { 1 => 1, 0 => 2 } + self.wchair = wheelchair_adaptation_map[location.wheelchair_adaptation.to_i] + end + if is_renewal? + self.voiddate = startdate + end end end diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index 7c6d86698..80a4db102 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -34,7 +34,7 @@ class Form::Subsection qs = applicable_questions(case_log) qs_optional_removed = qs.reject { |q| case_log.optional_fields.include?(q.id) } - return :not_started if qs.all? { |question| case_log[question.id].blank? || question.read_only? || question.derived? } + return :not_started if qs.count.positive? && qs.all? { |question| case_log[question.id].blank? || question.read_only? || question.derived? } return :completed if qs_optional_removed.all? { |question| question.completed?(case_log) } :in_progress diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index 14ff87274..ff64d5b74 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -9,13 +9,15 @@ <% end %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 49c38f47c..d93f04e22 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -54,7 +54,12 @@ "value": "Not known" } } - } + }, + "depends_on": [ + { + "needstype": 1 + } + ] }, "property_local_authority": { "header": "", @@ -388,7 +393,8 @@ }, "depends_on": [ { - "is_la_inferred": false + "is_la_inferred": false, + "needstype": 1 } ] }, @@ -617,7 +623,12 @@ } } } - } + }, + "depends_on": [ + { + "needstype": 1 + } + ] }, "property_building_type": { "header": "", @@ -637,7 +648,12 @@ } } } - } + }, + "depends_on": [ + { + "needstype": 1 + } + ] }, "property_wheelchair_accessible": { "header": "", @@ -657,7 +673,12 @@ } } } - } + }, + "depends_on": [ + { + "needstype": 1 + } + ] }, "property_number_of_bedrooms": { "header": "", @@ -6840,7 +6861,9 @@ }, "net_income_value_check": { "depends_on": [{ "net_income_soft_validation_triggered?": true }], - "title_text": { "translation": "soft_validations.net_income.title_text" }, + "title_text": { + "translation": "soft_validations.net_income.title_text" + }, "informative_text": { "translation": "soft_validations.net_income.hint_text", "arguments": [ diff --git a/db/migrate/20220706104313_add_unit_type_sh.rb b/db/migrate/20220706104313_add_unit_type_sh.rb new file mode 100644 index 000000000..fa7aa9732 --- /dev/null +++ b/db/migrate/20220706104313_add_unit_type_sh.rb @@ -0,0 +1,7 @@ +class AddUnitTypeSh < ActiveRecord::Migration[7.0] + def change + change_table :case_logs, bulk: true do |t| + t.integer :unittype_sh + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 429738353..10a390d90 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_07_05_130923) do +ActiveRecord::Schema[7.0].define(version: 2022_07_06_104313) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -200,6 +200,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_05_130923) do t.integer "vacdays" t.bigint "scheme_id" t.bigint "location_id" + t.integer "unittype_sh" t.index ["created_by_id"], name: "index_case_logs_on_created_by_id" t.index ["location_id"], name: "index_case_logs_on_location_id" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" diff --git a/db/seeds.rb b/db/seeds.rb index be13a937c..3ac0aa7d5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -115,7 +115,7 @@ unless Rails.env.test? postcode: "CU193AA", name: "Rectory Road", type_of_unit: 4, - type_of_building: "Purpose-built", + type_of_building: "Purpose built", county: "Mid Sussex", wheelchair_adaptation: 0, ) diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 53f8d2cd0..efec1aa76 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -4,7 +4,7 @@ FactoryBot.define do postcode { Faker::Address.postcode.delete(" ") } name { Faker::Address.street_name } type_of_unit { Faker::Number.within(range: 1..6) } - type_of_building { Faker::Lorem.word } + type_of_building { "Purpose built" } wheelchair_adaptation { 0 } county { Faker::Address.state } scheme diff --git a/spec/features/form/tasklist_page_spec.rb b/spec/features/form/tasklist_page_spec.rb index c2c4da0c6..25f2824ac 100644 --- a/spec/features/form/tasklist_page_spec.rb +++ b/spec/features/form/tasklist_page_spec.rb @@ -54,7 +54,7 @@ RSpec.describe "Task List" do it "shows number of completed sections if one section is completed" do visit("/logs/#{setup_completed_log.id}") - expect(page).to have_content("1 of 9 sections completed.") + expect(page).to have_content("1 of 8 sections completed.") end it "show skip link for next incomplete section" do diff --git a/spec/fixtures/exports/case_logs.csv b/spec/fixtures/exports/case_logs.csv index 95e47a3d6..3122967a2 100644 --- a/spec/fixtures/exports/case_logs.csv +++ b/spec/fixtures/exports/case_logs.csv @@ -1,2 +1,2 @@ -status,tenancycode,age1,sex1,ethnic,national,prevten,ecstat1,hhmemb,age2,sex2,ecstat2,age3,sex3,ecstat3,age4,sex4,ecstat4,age5,sex5,ecstat5,age6,sex6,ecstat6,age7,sex7,ecstat7,age8,sex8,ecstat8,homeless,underoccupation_benefitcap,leftreg,reservist,illness,preg_occ,startertenancy,tenancylength,tenancy,ppostcode_full,rsnvac,unittype_gn,beds,offered,wchair,earnings,incfreq,benefits,period,layear,waityear,postcode_full,reasonpref,cbl,chr,cap,reasonother,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,illness_type_1,illness_type_2,illness_type_3,illness_type_4,illness_type_8,illness_type_5,illness_type_6,illness_type_7,illness_type_9,illness_type_10,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,tenancyother,irproduct_other,reason,propcode,la,prevloc,hb,hbrentshortfall,mrcdate,incref,startdate,armedforces,unitletas,builtype,voiddate,renttype,needstype,lettype,totchild,totelder,totadult,nocharge,referral,brent,scharge,pscharge,supcharg,tcharge,tshortfall,chcharge,ppcodenk,has_benefits,renewal,wrent,wscharge,wpschrge,wsupchrg,wtcharge,wtshortfall,refused,housingneeds,wchchrg,newprop,relat2,relat3,relat4,relat5,relat6,relat7,relat8,lar,irproduct,joint,sheltered,hhtype,new_old,vacdays,form,owningorgid,owningorgname,hcnum,maningorgid,maningorgname,manhcnum,createddate,uploaddate -2,BZ737,35,F,2,4,6,0,2,32,M,6,,,,,,,,,,,,,,,,,,,1,0,1,0,1,2,0,5,1,SE26RT,6,7,3,2,1,68,1,1,2,2,1,NW15TY,1,1,1,2,,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,,,4,123,E09000003,E07000105,6,1,2020-05-05 10:36:49 UTC,0,2022-02-02 10:36:49 UTC,1,2,1,2019-11-03 00:00:00 UTC,2,1,7,0,0,2,0,,200.0,50.0,40.0,35.0,325.0,12.0,,1,1,0,100.0,25.0,20.0,17.5,162.5,6.0,0,1,,2,P,,,,,,,,,,0,4,2,638,{id},{owning_org_id},DLUHC,1234,{managing_org_id},DLUHC,1234,2022-02-08 16:52:15 UTC,2022-02-08 16:52:15 UTC +status,tenancycode,age1,sex1,ethnic,national,prevten,ecstat1,hhmemb,age2,sex2,ecstat2,age3,sex3,ecstat3,age4,sex4,ecstat4,age5,sex5,ecstat5,age6,sex6,ecstat6,age7,sex7,ecstat7,age8,sex8,ecstat8,homeless,underoccupation_benefitcap,leftreg,reservist,illness,preg_occ,startertenancy,tenancylength,tenancy,ppostcode_full,rsnvac,unittype_gn,beds,offered,wchair,earnings,incfreq,benefits,period,layear,waityear,postcode_full,reasonpref,cbl,chr,cap,reasonother,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,illness_type_1,illness_type_2,illness_type_3,illness_type_4,illness_type_8,illness_type_5,illness_type_6,illness_type_7,illness_type_9,illness_type_10,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,tenancyother,irproduct_other,reason,propcode,la,prevloc,hb,hbrentshortfall,mrcdate,incref,startdate,armedforces,unitletas,builtype,voiddate,renttype,needstype,lettype,totchild,totelder,totadult,nocharge,referral,brent,scharge,pscharge,supcharg,tcharge,tshortfall,chcharge,ppcodenk,has_benefits,renewal,wrent,wscharge,wpschrge,wsupchrg,wtcharge,wtshortfall,refused,housingneeds,wchchrg,newprop,relat2,relat3,relat4,relat5,relat6,relat7,relat8,lar,irproduct,joint,sheltered,hhtype,new_old,vacdays,unittype_sh,form,owningorgid,owningorgname,hcnum,maningorgid,maningorgname,manhcnum,createddate,uploaddate +2,BZ737,35,F,2,4,6,0,2,32,M,6,,,,,,,,,,,,,,,,,,,1,0,1,0,1,2,0,5,1,SE26RT,6,7,3,2,1,68,1,1,2,2,1,NW15TY,1,1,1,2,,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,,,4,123,E09000003,E07000105,6,1,2020-05-05 10:36:49 UTC,0,2022-02-02 10:36:49 UTC,1,2,1,2019-11-03 00:00:00 UTC,2,1,7,0,0,2,0,,200.0,50.0,40.0,35.0,325.0,12.0,,1,1,0,100.0,25.0,20.0,17.5,162.5,6.0,0,1,,2,P,,,,,,,,,,0,4,2,638,,{id},{owning_org_id},DLUHC,1234,{managing_org_id},DLUHC,1234,2022-02-08 16:52:15 UTC,2022-02-08 16:52:15 UTC diff --git a/spec/fixtures/exports/case_logs.xml b/spec/fixtures/exports/case_logs.xml index 469d8f2f0..2608bf92e 100644 --- a/spec/fixtures/exports/case_logs.xml +++ b/spec/fixtures/exports/case_logs.xml @@ -135,6 +135,7 @@ 4 2 638 +
{id}
{owning_org_id} DLUHC diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 0e13fa805..4401d1739 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -17,7 +17,7 @@ RSpec.describe TasklistHelper do describe "get sections count" do it "returns the total of sections if no status is given" do - expect(get_subsections_count(empty_case_log)).to eq(9) + expect(get_subsections_count(empty_case_log)).to eq(8) end it "returns 0 sections for completed sections if no sections are completed" do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 23176a005..ae5c550d6 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: 1, 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_sh from case_logs WHERE id=#{supported_housing_case_log.id}").to_a[0] + expect(record_from_db["unittype_sh"]).to eq(1) + 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:) } + + 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_i).to eq(supported_housing_case_log.startdate.to_i) + end + end end end