From f0a1e183252d2d78200942c611d43295da44992a Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Mon, 13 Feb 2023 15:40:20 +0000 Subject: [PATCH] CLDC-1885 Bulk upload type must match given needs type (#1288) * bulk upload type must match given needs type * refactor invert logic statement * tweak validation copy --- app/models/bulk_upload.rb | 4 ++ .../bulk_upload/lettings/row_parser.rb | 15 ++++++- config/locales/en.yml | 3 ++ .../bulk_upload/lettings/row_parser_spec.rb | 40 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 8385b9ba4..0adf0d84a 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -42,6 +42,10 @@ class BulkUpload < ApplicationRecord needstype == 1 end + def supported_housing? + needstype == 2 + end + private def generate_identifier diff --git a/app/services/bulk_upload/lettings/row_parser.rb b/app/services/bulk_upload/lettings/row_parser.rb index cee5fa319..42e762be1 100644 --- a/app/services/bulk_upload/lettings/row_parser.rb +++ b/app/services/bulk_upload/lettings/row_parser.rb @@ -148,7 +148,8 @@ class BulkUpload::Lettings::RowParser validate :validate_relevant_collection_window validate :validate_la_with_local_housing_referral validate :validate_cannot_be_la_referral_if_general_needs - validate :leaving_reason_for_renewal + validate :validate_leaving_reason_for_renewal + validate :validate_lettings_type_matches_bulk_upload def valid? errors.clear @@ -171,6 +172,16 @@ class BulkUpload::Lettings::RowParser private + def validate_lettings_type_matches_bulk_upload + if [1, 3, 5, 7, 9, 11].include?(field_1) && !bulk_upload.general_needs? + errors.add(:field_1, I18n.t("validations.setup.lettype.supported_housing_mismatch")) + end + + if [2, 4, 6, 8, 10, 12].include?(field_1) && !bulk_upload.supported_housing? + errors.add(:field_1, I18n.t("validations.setup.lettype.general_needs_mismatch")) + end + end + def validate_cannot_be_la_referral_if_general_needs if field_78 == 4 && bulk_upload.general_needs? errors.add :field_78, I18n.t("validations.household.referral.la_general_needs.prp_referred_by_la") @@ -183,7 +194,7 @@ private end end - def leaving_reason_for_renewal + def validate_leaving_reason_for_renewal if field_134 == 1 && ![40, 42].include?(field_52) errors.add(:field_52, I18n.t("validations.household.reason.renewal_reason_needed")) end diff --git a/config/locales/en.yml b/config/locales/en.yml index a97863c30..0a4ae34de 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -170,6 +170,9 @@ en: invalid: "Please select owning organisation or managing organisation that you belong to" created_by: invalid: "Please select owning organisation or managing organisation that you belong to" + lettype: + general_needs_mismatch: Lettings type must be a general needs type because you selected general needs when uploading the file + supported_housing_mismatch: Lettings type must be a supported housing type because you selected supported housing when uploading the file property: mrcdate: diff --git a/spec/services/bulk_upload/lettings/row_parser_spec.rb b/spec/services/bulk_upload/lettings/row_parser_spec.rb index 297b9fd46..29ce6efbb 100644 --- a/spec/services/bulk_upload/lettings/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/row_parser_spec.rb @@ -202,6 +202,46 @@ RSpec.describe BulkUpload::Lettings::RowParser do expect(parser.errors[:field_1]).to be_blank end end + + context "when bulk upload is for general needs" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: "1") } + + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_1: "1" } } + + it "is permitted" do + expect(parser.errors[:field_1]).to be_blank + end + end + + context "when supported housing option selected" do + let(:attributes) { { bulk_upload:, field_1: "2" } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to include("Lettings type must be a general needs type because you selected general needs when uploading the file") + end + end + end + + context "when bulk upload is for supported housing" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: "2") } + + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_1: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to include("Lettings type must be a supported housing type because you selected supported housing when uploading the file") + end + end + + context "when supported housing option selected" do + let(:attributes) { { bulk_upload:, field_1: "2" } } + + it "is permitted" do + expect(parser.errors[:field_1]).to be_blank + end + end + end end describe "#field_4" do