From 0fc63711a6cee587af9a1c53fb4156629134ef65 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 31 Mar 2023 14:07:33 +0100 Subject: [PATCH] CLDC-2222 Update access needs mapping and validations (#1501) * Set housingneeds to yes if field 59 is 0 * Only display errors on selected housing needs * Add errors to relevant fields when no housingneeds are selected with housingneeds options * Add errors to correct fields when housingneeds don't know is selected * Refactor validations * Update 2023 housingneeds validations --- .../lettings/year2022/row_parser.rb | 24 +++-- .../lettings/year2023/row_parser.rb | 24 +++-- .../lettings/year2022/row_parser_spec.rb | 98 ++++++++++++++++++- .../lettings/year2023/row_parser_spec.rb | 98 ++++++++++++++++++- 4 files changed, 222 insertions(+), 22 deletions(-) diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 13e2f9d02..6c697de14 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -532,22 +532,26 @@ private end def validate_dont_know_disabled_needs_conjunction - if field_60 == 1 && [field_55, field_56, field_57, field_58].compact.count.positive? - errors.add(:field_60, I18n.t("validations.household.housingneeds.dont_know_disabled_needs_conjunction")) + if field_60 == 1 && [field_55, field_56, field_57, field_58].count(1).positive? + %i[field_60 field_55 field_56 field_57 field_58].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds.dont_know_disabled_needs_conjunction")) if send(field) == 1 + end end end def validate_no_disabled_needs_conjunction - if field_59 == 1 && [field_55, field_56, field_57, field_58].compact.count.positive? - errors.add(:field_59, I18n.t("validations.household.housingneeds.no_disabled_needs_conjunction")) + if field_59 == 1 && [field_55, field_56, field_57, field_58].count(1).positive? + %i[field_59 field_55 field_56 field_57 field_58].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds.no_disabled_needs_conjunction")) if send(field) == 1 + end end end def validate_only_one_housing_needs_type - if [field_55, field_56, field_57].compact.count > 1 - errors.add(:field_55, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) - errors.add(:field_56, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) - errors.add(:field_57, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) + if [field_55, field_56, field_57].count(1) > 1 + %i[field_55 field_56 field_57].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) if send(field) == 1 + end end end @@ -1240,8 +1244,8 @@ private 2 elsif field_60 == 1 3 - else - 2 + elsif field_59&.zero? + 1 end end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 3af97b50f..6f3047def 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -431,22 +431,26 @@ private end def validate_dont_know_disabled_needs_conjunction - if field_88 == 1 && [field_83, field_84, field_85, field_86].compact.count.positive? - errors.add(:field_88, I18n.t("validations.household.housingneeds.dont_know_disabled_needs_conjunction")) + if field_88 == 1 && [field_83, field_84, field_85, field_86].count(1).positive? + %i[field_88 field_83 field_84 field_85 field_86].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds.dont_know_disabled_needs_conjunction")) if send(field) == 1 + end end end def validate_no_disabled_needs_conjunction - if field_87 == 1 && [field_83, field_84, field_85, field_86].compact.count.positive? - errors.add(:field_87, I18n.t("validations.household.housingneeds.no_disabled_needs_conjunction")) + if field_87 == 1 && [field_83, field_84, field_85, field_86].count(1).positive? + %i[field_87 field_83 field_84 field_85 field_86].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds.no_disabled_needs_conjunction")) if send(field) == 1 + end end end def validate_only_one_housing_needs_type - if [field_83, field_84, field_85].compact.count > 1 - errors.add(:field_83, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) - errors.add(:field_84, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) - errors.add(:field_85, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) + if [field_83, field_84, field_85].count(1) > 1 + %i[field_83 field_84 field_85].each do |field| + errors.add(field, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) if send(field) == 1 + end end end @@ -1173,8 +1177,8 @@ private 2 elsif field_88 == 1 3 - else - 2 + elsif field_87&.zero? + 1 end end diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index a03516852..ca4fc0b0b 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -97,6 +97,12 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do field_118: "2", + field_55: "1", + field_56: "0", + field_57: "0", + field_58: "1", + field_59: "0", + field_66: "5", field_67: "2", field_52: "31", @@ -501,7 +507,6 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do it "is not permitted" do expect(parser.errors[:field_55]).to be_present expect(parser.errors[:field_56]).to be_present - expect(parser.errors[:field_57]).to be_present end end end @@ -1414,6 +1419,97 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do expect(parser.log.housingneeds).to eq(3) end end + + context "when housingneeds are given" do + let(:attributes) { { bulk_upload:, field_59: "0", field_57: "1", field_58: "1" } } + + it "sets correct housingneeds" do + expect(parser.log.housingneeds).to eq(1) + expect(parser.log.housingneeds_type).to eq(2) + expect(parser.log.housingneeds_other).to eq(1) + end + end + + context "when housingneeds a and b are selected" do + let(:attributes) { { bulk_upload:, field_55: "1", field_56: "1" } } + + it "sets error on housingneeds a and b" do + parser.valid? + expect(parser.errors[:field_55]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_56]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_57]).to be_blank + end + end + + context "when housingneeds a and c are selected" do + let(:attributes) { { bulk_upload:, field_55: "1", field_57: "1" } } + + it "sets error on housingneeds a and c" do + parser.valid? + expect(parser.errors[:field_55]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_57]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_56]).to be_blank + end + end + + context "when housingneeds b and c are selected" do + let(:attributes) { { bulk_upload:, field_56: "1", field_57: "1" } } + + it "sets error on housingneeds b and c" do + parser.valid? + expect(parser.errors[:field_56]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_57]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_55]).to be_blank + end + end + + context "when housingneeds a and g are selected" do + let(:attributes) { { bulk_upload:, field_55: "1", field_59: "1" } } + + it "sets error on housingneeds a and g" do + parser.valid? + expect(parser.errors[:field_59]).to include("No disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_55]).to include("No disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_56]).to be_blank + expect(parser.errors[:field_57]).to be_blank + end + end + + context "when only housingneeds g is selected" do + let(:attributes) { { bulk_upload:, field_55: "0", field_59: "1" } } + + it "does not add any housingneeds errors" do + parser.valid? + expect(parser.errors[:field_59]).to be_blank + expect(parser.errors[:field_55]).to be_blank + expect(parser.errors[:field_56]).to be_blank + expect(parser.errors[:field_57]).to be_blank + end + end + + context "when housingneeds a and h are selected" do + let(:attributes) { { bulk_upload:, field_55: "1", field_60: "1" } } + + it "sets error on housingneeds a and h" do + parser.valid? + expect(parser.errors[:field_60]).to include("Don’t know disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_55]).to include("Don’t know disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_56]).to be_blank + expect(parser.errors[:field_57]).to be_blank + end + end + + context "when only housingneeds h is selected" do + let(:attributes) { { bulk_upload:, field_55: "0", field_60: "1" } } + + it "does not add any housingneeds errors" do + parser.valid? + expect(parser.errors[:field_60]).to be_blank + expect(parser.errors[:field_55]).to be_blank + expect(parser.errors[:field_56]).to be_blank + expect(parser.errors[:field_57]).to be_blank + end + end end describe "#housingneeds_type" do diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index aff3b0516..67dc69bbe 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -165,6 +165,12 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do field_82: "1", + field_83: "1", + field_84: "0", + field_85: "0", + field_86: "1", + field_87: "0", + field_89: "2", field_100: "5", @@ -517,7 +523,6 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do it "is not permitted" do expect(parser.errors[:field_83]).to be_present expect(parser.errors[:field_84]).to be_present - expect(parser.errors[:field_85]).to be_present end end end @@ -1452,6 +1457,97 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do expect(parser.log.housingneeds).to eq(3) end end + + context "when housingneeds are given" do + let(:attributes) { { bulk_upload:, field_87: "0", field_85: "1", field_86: "1" } } + + it "sets correct housingneeds" do + expect(parser.log.housingneeds).to eq(1) + expect(parser.log.housingneeds_type).to eq(2) + expect(parser.log.housingneeds_other).to eq(1) + end + end + + context "when housingneeds a and b are selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_84: "1" } } + + it "sets error on housingneeds a and b" do + parser.valid? + expect(parser.errors[:field_83]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_84]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_85]).to be_blank + end + end + + context "when housingneeds a and c are selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_85: "1" } } + + it "sets error on housingneeds a and c" do + parser.valid? + expect(parser.errors[:field_83]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_85]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_84]).to be_blank + end + end + + context "when housingneeds b and c are selected" do + let(:attributes) { { bulk_upload:, field_84: "1", field_85: "1" } } + + it "sets error on housingneeds b and c" do + parser.valid? + expect(parser.errors[:field_84]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_85]).to include("Only one disabled access need: fully wheelchair-accessible housing, wheelchair access to essential rooms or level access housing, can be selected") + expect(parser.errors[:field_83]).to be_blank + end + end + + context "when housingneeds a and g are selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_87: "1" } } + + it "sets error on housingneeds a and g" do + parser.valid? + expect(parser.errors[:field_87]).to include("No disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_83]).to include("No disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_84]).to be_blank + expect(parser.errors[:field_85]).to be_blank + end + end + + context "when only housingneeds g is selected" do + let(:attributes) { { bulk_upload:, field_83: "0", field_87: "1" } } + + it "does not add any housingneeds errors" do + parser.valid? + expect(parser.errors[:field_59]).to be_blank + expect(parser.errors[:field_83]).to be_blank + expect(parser.errors[:field_84]).to be_blank + expect(parser.errors[:field_85]).to be_blank + end + end + + context "when housingneeds a and h are selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_88: "1" } } + + it "sets error on housingneeds a and h" do + parser.valid? + expect(parser.errors[:field_88]).to include("Don’t know disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_83]).to include("Don’t know disabled access needs can’t be selected if you have selected fully wheelchair-accessible housing, wheelchair access to essential rooms, level access housing or other disabled access needs") + expect(parser.errors[:field_84]).to be_blank + expect(parser.errors[:field_85]).to be_blank + end + end + + context "when only housingneeds h is selected" do + let(:attributes) { { bulk_upload:, field_83: "0", field_88: "1" } } + + it "does not add any housingneeds errors" do + parser.valid? + expect(parser.errors[:field_88]).to be_blank + expect(parser.errors[:field_83]).to be_blank + expect(parser.errors[:field_84]).to be_blank + expect(parser.errors[:field_85]).to be_blank + end + end end describe "#housingneeds_type" do