From 74dea157d655a9fbc95c146f879cadbd3032c22b Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 1 Mar 2024 10:02:45 +0000 Subject: [PATCH] CLDC-3253 Validate nationality in bulk upload (#2257) * Validate nationality in bulk upload * Update valid_nationality_options --- .../lettings/year2024/row_parser.rb | 14 +++++++++- .../bulk_upload/sales/year2024/row_parser.rb | 22 ++++++++++++++-- config/locales/en.yml | 1 + .../lettings/year2024/row_parser_spec.rb | 13 +++++++++- .../sales/year2024/row_parser_spec.rb | 26 +++++++++++++++++-- 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app/services/bulk_upload/lettings/year2024/row_parser.rb b/app/services/bulk_upload/lettings/year2024/row_parser.rb index 08fbec85a..6ba9ab69a 100644 --- a/app/services/bulk_upload/lettings/year2024/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2024/row_parser.rb @@ -383,6 +383,7 @@ class BulkUpload::Lettings::Year2024::RowParser validate :validate_incomplete_soft_validations, on: :after_log validate :validate_all_charges_given, on: :after_log, if: proc { is_carehome.zero? } + validate :validate_nationality, on: :after_log def self.question_for_field(field) QUESTIONS[field] @@ -557,6 +558,12 @@ private end end + def validate_nationality + if field_45.present? && !valid_nationality_options.include?(field_45.to_s) + errors.add(:field_45, I18n.t("validations.household.nationality")) + end + end + def duplicate_check_fields [ "startdate", @@ -914,6 +921,7 @@ private ethnic_group: %i[field_44], ethnic: %i[field_44], nationality_all: %i[field_45], + nationality_all_group: %i[field_45], relat2: %i[field_47], relat3: %i[field_51], @@ -1087,7 +1095,7 @@ private attributes["ethnic_group"] = ethnic_group_from_ethnic attributes["ethnic"] = field_44 - attributes["nationality_all"] = field_45 + attributes["nationality_all"] = field_45 if field_45.present? && valid_nationality_options.include?(field_45.to_s) attributes["nationality_all_group"] = nationality_group(attributes["nationality_all"]) attributes["relat2"] = field_47 @@ -1486,6 +1494,10 @@ private end end + def valid_nationality_options + %w[0] + GlobalConstants::COUNTRIES_ANSWER_OPTIONS.keys # 0 is "Prefers not to say" + end + def nationality_group(nationality_value) return unless nationality_value return 0 if nationality_value.zero? diff --git a/app/services/bulk_upload/sales/year2024/row_parser.rb b/app/services/bulk_upload/sales/year2024/row_parser.rb index 2c1b6089a..70aaee49a 100644 --- a/app/services/bulk_upload/sales/year2024/row_parser.rb +++ b/app/services/bulk_upload/sales/year2024/row_parser.rb @@ -469,6 +469,8 @@ class BulkUpload::Sales::Year2024::RowParser validate :validate_if_log_already_exists, on: :after_log, if: -> { FeatureToggle.bulk_upload_duplicate_log_check_enabled? } validate :validate_buyers_organisations, on: :after_log + validate :validate_nationality, on: :after_log + validate :validate_buyer_2_nationality, on: :after_log def self.question_for_field(field) QUESTIONS[field] @@ -834,7 +836,7 @@ private attributes["ethnic_group"] = ethnic_group_from_ethnic attributes["ethnic"] = field_33 - attributes["nationality_all"] = field_34 + attributes["nationality_all"] = field_34 if field_34.present? && valid_nationality_options.include?(field_34.to_s) attributes["nationality_all_group"] = nationality_group(attributes["nationality_all"]) attributes["income1nk"] = field_77 == "R" ? 1 : 0 @@ -942,7 +944,7 @@ private attributes["ethnic_group2"] = infer_buyer2_ethnic_group_from_ethnic attributes["ethnicbuy2"] = field_40 - attributes["nationality_all_buyer2"] = field_41 + attributes["nationality_all_buyer2"] = field_41 if field_41.present? && valid_nationality_options.include?(field_41.to_s) attributes["nationality_all_buyer2_group"] = nationality_group(attributes["nationality_all_buyer2"]) attributes["buy2living"] = field_70 @@ -1341,4 +1343,20 @@ private errors.add(:field_35, "Buyer 1 cannot be a child under 16") end end + + def validate_nationality + if field_34.present? && !valid_nationality_options.include?(field_34.to_s) + errors.add(:field_34, I18n.t("validations.household.nationality")) + end + end + + def validate_buyer_2_nationality + if field_41.present? && !valid_nationality_options.include?(field_41.to_s) + errors.add(:field_41, I18n.t("validations.household.nationality")) + end + end + + def valid_nationality_options + %w[0] + GlobalConstants::COUNTRIES_ANSWER_OPTIONS.keys # 0 is "Prefers not to say" + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9746ba13f..0ed12c806 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -552,6 +552,7 @@ en: buylivein: buyers_will_live_in_property_values_inconsistent_setup: "You have already told us that both buyer 1 and buyer 2 will not live in the property" buyers_will_live_in_property_values_inconsistent: "You have already told us that the buyers will live in the property. Either buyer 1 or buyer 2 must live in the property" + nationality: "Select a valid nationality" tenancy: length: diff --git a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb index dddf8a744..73e639449 100644 --- a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb @@ -149,7 +149,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do field_73: "M", field_44: "17", - field_45: "18", + field_45: "826", field_47: "P", field_51: "C", @@ -1594,6 +1594,17 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do expect(parser.log.nationality_all_group).to be(826) end end + + context "when field_45 is not a valid option" do + let(:attributes) { setup_section_params.merge({ field_45: "123123" }) } + + it "is correctly set" do + parser.log.save! + expect(parser.log.nationality_all).to be(nil) + expect(parser.log.nationality_all_group).to be(nil) + expect(parser.errors["field_45"]).to include("Select a valid nationality") + end + end end describe "soft validations" do diff --git a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb index 5a1a9e4df..6a357f46a 100644 --- a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb @@ -57,14 +57,14 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do field_31: "32", field_32: "M", field_33: "12", - field_34: "18", + field_34: "28", field_35: "1", field_36: "1", field_37: "R", field_38: "32", field_39: "F", field_40: "17", - field_41: "13", + field_41: "28", field_42: "2", field_43: "1", field_44: "0", @@ -1208,6 +1208,17 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do expect(parser.log.nationality_all_group).to be(826) end end + + context "when field_34 is not a valid option" do + let(:attributes) { setup_section_params.merge({ field_34: "123123" }) } + + it "is correctly set" do + parser.valid? + expect(parser.log.nationality_all).to be(nil) + expect(parser.log.nationality_all_group).to be(nil) + expect(parser.errors["field_34"]).to include("Select a valid nationality") + end + end end describe "#nationality_all_buyer2" do @@ -1282,6 +1293,17 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do expect(parser.log.nationality_all_buyer2_group).to be(826) end end + + context "when field_41 is not a valid option" do + let(:attributes) { setup_section_params.merge({ field_41: "123123" }) } + + it "is correctly set" do + parser.valid? + expect(parser.log.nationality_all_buyer2).to be(nil) + expect(parser.log.nationality_all_buyer2_group).to be(nil) + expect(parser.errors["field_41"]).to include("Select a valid nationality") + end + end end describe "#buy2living" do