From 35de06e324ff145f91233f3392edd0c8a4354b07 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Thu, 16 Mar 2023 10:08:10 +0000 Subject: [PATCH] CLDC-2067 Bulk upload for lettings 23/24 (#1417) * refactor questions from validator to row parser * able to switch between bulk upload parsers - depending on what year we are processing * spec tooling to support bulk upload multi year * row parser now has year in namespacing * add static data for 2023 row parser * add placeholder to log to csv for specs * bulk upload aribtrary 23/34 column ordering works * bulk upload supports 23/24 without headers * bulk upload 23/24 supports BOM + invalid chars * dupe tests * port 23/24 attributes_for_log * port 23/24 bulk upload validations * force crossover period * tweak max permitted columns * able to return column for given field * work out column for field for errors * add field_4 as 23/24 setup field * remove duplicate method * map schemes and locations correctly * handle arbitrary number of header rows * add missing fields to bulk upload support --- .../bulk_upload_error_row_component.rb | 9 +- ...oad_error_summary_table_component.html.erb | 2 +- ...lk_upload_error_summary_table_component.rb | 6 + .../bulk_upload_lettings_logs_controller.rb | 2 + app/mailers/bulk_upload_mailer.rb | 8 +- app/models/bulk_upload.rb | 22 + .../bulk_upload_lettings/prepare_your_file.rb | 2 + app/models/forms/bulk_upload_lettings/year.rb | 5 +- .../bulk_upload/lettings/log_creator.rb | 9 +- .../bulk_upload/lettings/validator.rb | 152 +- .../lettings/{ => year2022}/csv_parser.rb | 14 +- .../lettings/{ => year2022}/row_parser.rb | 150 +- .../lettings/year2023/csv_parser.rb | 80 + .../lettings/year2023/row_parser.rb | 1233 ++++++++++++++++ app/services/bulk_upload/sales/row_parser.rb | 176 --- app/services/bulk_upload/sales/validator.rb | 134 +- .../bulk_upload/sales/year2022/row_parser.rb | 308 ++++ config/initializers/feature_toggle.rb | 6 + ...load_error_summary_table_component_spec.rb | 8 +- .../forms/bulk_upload_lettings/year_spec.rb | 4 +- .../bulk_upload/lettings/log_creator_spec.rb | 8 +- .../bulk_upload/lettings/validator_spec.rb | 54 +- .../{ => year2022}/csv_parser_spec.rb | 33 +- .../{ => year2022}/row_parser_spec.rb | 2 +- .../lettings/year2023/csv_parser_spec.rb | 181 +++ .../lettings/year2023/row_parser_spec.rb | 1282 +++++++++++++++++ .../sales/{ => year2022}/row_parser_spec.rb | 2 +- spec/support/bulk_upload/log_to_csv.rb | 49 +- 28 files changed, 3423 insertions(+), 518 deletions(-) rename app/services/bulk_upload/lettings/{ => year2022}/csv_parser.rb (76%) rename app/services/bulk_upload/lettings/{ => year2022}/row_parser.rb (78%) create mode 100644 app/services/bulk_upload/lettings/year2023/csv_parser.rb create mode 100644 app/services/bulk_upload/lettings/year2023/row_parser.rb delete mode 100644 app/services/bulk_upload/sales/row_parser.rb create mode 100644 app/services/bulk_upload/sales/year2022/row_parser.rb rename spec/services/bulk_upload/lettings/{ => year2022}/csv_parser_spec.rb (67%) rename spec/services/bulk_upload/lettings/{ => year2022}/row_parser_spec.rb (99%) create mode 100644 spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb create mode 100644 spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb rename spec/services/bulk_upload/sales/{ => year2022}/row_parser_spec.rb (87%) diff --git a/app/components/bulk_upload_error_row_component.rb b/app/components/bulk_upload_error_row_component.rb index e19717954..13ac326ef 100644 --- a/app/components/bulk_upload_error_row_component.rb +++ b/app/components/bulk_upload_error_row_component.rb @@ -24,14 +24,7 @@ class BulkUploadErrorRowComponent < ViewComponent::Base end def question_for_field(field) - case bulk_upload.log_type - when "lettings" - BulkUpload::Lettings::Validator.question_for_field(field.to_sym) - when "sales" - BulkUpload::Sales::Validator.question_for_field(field.to_sym) - else - "Unknown question" - end + bulk_upload.prefix_namespace::RowParser.question_for_field(field.to_sym) end def bulk_upload diff --git a/app/components/bulk_upload_error_summary_table_component.html.erb b/app/components/bulk_upload_error_summary_table_component.html.erb index f1749ee92..9cf146dc0 100644 --- a/app/components/bulk_upload_error_summary_table_component.html.erb +++ b/app/components/bulk_upload_error_summary_table_component.html.erb @@ -16,7 +16,7 @@ <% body.row do |row| %> <% row.cell(text: error[0][0]) %> <% row.cell(text: error[1]) %> - <% row.cell(text: BulkUpload::Lettings::Validator.question_for_field(error[0][1].to_sym)) %> + <% row.cell(text: question_for_field(error[0][1].to_sym)) %> <% row.cell(text: error[0][2]) %> <% row.cell(text: error[0][1]) %> <% end %> diff --git a/app/components/bulk_upload_error_summary_table_component.rb b/app/components/bulk_upload_error_summary_table_component.rb index a3d295110..909fb5f0d 100644 --- a/app/components/bulk_upload_error_summary_table_component.rb +++ b/app/components/bulk_upload_error_summary_table_component.rb @@ -3,6 +3,8 @@ class BulkUploadErrorSummaryTableComponent < ViewComponent::Base attr_reader :bulk_upload + delegate :question_for_field, to: :row_parser_class + def initialize(bulk_upload:) @bulk_upload = bulk_upload @@ -27,4 +29,8 @@ private def display_threshold DISPLAY_THRESHOLD end + + def row_parser_class + bulk_upload.prefix_namespace::RowParser + end end diff --git a/app/controllers/bulk_upload_lettings_logs_controller.rb b/app/controllers/bulk_upload_lettings_logs_controller.rb index 9b37cd2b5..ec5975432 100644 --- a/app/controllers/bulk_upload_lettings_logs_controller.rb +++ b/app/controllers/bulk_upload_lettings_logs_controller.rb @@ -33,6 +33,8 @@ private end def in_crossover_period? + return true if FeatureToggle.force_crossover? + FormHandler.instance.lettings_in_crossover_period? end diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index fe1e81517..3d5fca769 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -73,11 +73,7 @@ class BulkUploadMailer < NotifyMailer start_bulk_upload_sales_logs_url end - validator_class = if bulk_upload.lettings? - BulkUpload::Lettings::Validator - else - BulkUpload::Sales::Validator - end + row_parser_class = bulk_upload.prefix_namespace::RowParser errors = bulk_upload .bulk_upload_errors @@ -87,7 +83,7 @@ class BulkUploadMailer < NotifyMailer .keys .sort_by { |_col, field| field } .map do |col, field| - "- Column #{col} (#{validator_class.question_for_field(field.to_sym)})" + "- Column #{col} (#{row_parser_class.question_for_field(field.to_sym)})" end send_email( diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 0adf0d84a..ff334b968 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -46,6 +46,28 @@ class BulkUpload < ApplicationRecord needstype == 2 end + def prefix_namespace + type_class = case log_type + when "lettings" + "Lettings" + when "sales" + "Sales" + else + raise "unknown log type" + end + + year_class = case year + when 2022 + "Year2022" + when 2023 + "Year2023" + else + raise "unknown year" + end + + "BulkUpload::#{type_class}::#{year_class}".constantize + end + private def generate_identifier diff --git a/app/models/forms/bulk_upload_lettings/prepare_your_file.rb b/app/models/forms/bulk_upload_lettings/prepare_your_file.rb index bfaa622b2..68b9dd5c6 100644 --- a/app/models/forms/bulk_upload_lettings/prepare_your_file.rb +++ b/app/models/forms/bulk_upload_lettings/prepare_your_file.rb @@ -44,6 +44,8 @@ module Forms private def in_crossover_period? + return true if FeatureToggle.force_crossover? + FormHandler.instance.lettings_in_crossover_period? end end diff --git a/app/models/forms/bulk_upload_lettings/year.rb b/app/models/forms/bulk_upload_lettings/year.rb index 80324e53e..d958f27d4 100644 --- a/app/models/forms/bulk_upload_lettings/year.rb +++ b/app/models/forms/bulk_upload_lettings/year.rb @@ -34,7 +34,10 @@ module Forms private def possible_years - [FormHandler.instance.lettings_forms["current_lettings"].start_date.year, FormHandler.instance.lettings_forms["previous_lettings"].start_date.year] + [ + FormHandler.instance.lettings_forms["next_lettings"].start_date.year, + FormHandler.instance.lettings_forms["current_lettings"].start_date.year, + ] end end end diff --git a/app/services/bulk_upload/lettings/log_creator.rb b/app/services/bulk_upload/lettings/log_creator.rb index 625d53f43..089639d4f 100644 --- a/app/services/bulk_upload/lettings/log_creator.rb +++ b/app/services/bulk_upload/lettings/log_creator.rb @@ -26,7 +26,14 @@ class BulkUpload::Lettings::LogCreator private def csv_parser - @csv_parser ||= BulkUpload::Lettings::CsvParser.new(path:) + @csv_parser ||= case bulk_upload.year + when 2022 + BulkUpload::Lettings::Year2022::CsvParser.new(path:) + when 2023 + BulkUpload::Lettings::Year2023::CsvParser.new(path:) + else + raise "csv parser not found" + end end def row_offset diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index f5450039f..ea5015e56 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -6,143 +6,6 @@ class BulkUpload::Lettings::Validator include ActiveModel::Validations - QUESTIONS = { - field_1: "What is the letting type?", - field_2: "This question has been removed", - field_3: "This question has been removed", - field_4: "Management group code", - field_5: "Scheme code", - field_6: "This question has been removed", - field_7: "What is the tenant code?", - field_8: "Is this a starter tenancy?", - field_9: "What is the tenancy type?", - field_10: "If 'Other', what is the tenancy type?", - field_11: "What is the length of the fixed-term tenancy to the nearest year?", - field_12: "Age of Person 1", - field_13: "Age of Person 2", - field_14: "Age of Person 3", - field_15: "Age of Person 4", - field_16: "Age of Person 5", - field_17: "Age of Person 6", - field_18: "Age of Person 7", - field_19: "Age of Person 8", - field_20: "Gender identity of Person 1", - field_21: "Gender identity of Person 2", - field_22: "Gender identity of Person 3", - field_23: "Gender identity of Person 4", - field_24: "Gender identity of Person 5", - field_25: "Gender identity of Person 6", - field_26: "Gender identity of Person 7", - field_27: "Gender identity of Person 8", - field_28: "Relationship to Person 1 for Person 2", - field_29: "Relationship to Person 1 for Person 3", - field_30: "Relationship to Person 1 for Person 4", - field_31: "Relationship to Person 1 for Person 5", - field_32: "Relationship to Person 1 for Person 6", - field_33: "Relationship to Person 1 for Person 7", - field_34: "Relationship to Person 1 for Person 8", - field_35: "Working situation of Person 1", - field_36: "Working situation of Person 2", - field_37: "Working situation of Person 3", - field_38: "Working situation of Person 4", - field_39: "Working situation of Person 5", - field_40: "Working situation of Person 6", - field_41: "Working situation of Person 7", - field_42: "Working situation of Person 8", - field_43: "What is the lead tenant's ethnic group?", - field_44: "What is the lead tenant's nationality?", - field_45: "Does anybody in the household have links to the UK armed forces?", - field_46: "Was the person seriously injured or ill as a result of serving in the UK armed forces?", - field_47: "Is anybody in the household pregnant?", - field_48: "Is the tenant likely to be receiving benefits related to housing?", - field_49: "How much of the household's income is from Universal Credit, state pensions or benefits?", - field_50: "How much income does the household have in total?", - field_51: "Do you know the household's income?", - field_52: "What is the tenant's main reason for the household leaving their last settled home?", - field_53: "If 'Other', what was the main reason for leaving their last settled home?", - field_54: "This question has been removed", - field_55: "Does anybody in the household have any disabled access needs?", - field_56: "Does anybody in the household have any disabled access needs?", - field_57: "Does anybody in the household have any disabled access needs?", - field_58: "Does anybody in the household have any disabled access needs?", - field_59: "Does anybody in the household have any disabled access needs?", - field_60: "Does anybody in the household have any disabled access needs?", - field_61: "Where was the household immediately before this letting?", - field_62: "What is the local authority of the household's last settled home?", - field_63: "Part 1 of postcode of last settled home", - field_64: "Part 2 of postcode of last settled home", - field_65: "Do you know the postcode of last settled home?", - field_66: "How long has the household continuously lived in the local authority area of the new letting?", - field_67: "How long has the household been on the waiting list for the new letting?", - field_68: "Was the tenant homeless directly before this tenancy?", - field_69: "Was the household given 'reasonable preference' by the local authority?", - field_70: "Reasonable preference. They were homeless or about to lose their home (within 56 days)", - field_71: "Reasonable preference. They were living in insanitary, overcrowded or unsatisfactory housing", - field_72: "Reasonable preference. They needed to move on medical and welfare grounds (including a disability)", - field_73: "Reasonable preference. They needed to move to avoid hardship to themselves or others", - field_74: "Reasonable preference. Don't know", - field_75: "Was the letting made under any of the following allocations systems?", - field_76: "Was the letting made under any of the following allocations systems?", - field_77: "Was the letting made under any of the following allocations systems?", - field_78: "What was the source of referral for this letting?", - field_79: "How often does the household pay rent and other charges?", - field_80: "What is the basic rent?", - field_81: "What is the service charge?", - field_82: "What is the personal service charge?", - field_83: "What is the support charge?", - field_84: "Total Charge", - field_85: "If this is a care home, how much does the household pay every [time period]?", - field_86: "Does the household pay rent or other charges for the accommodation?", - field_87: "After the household has received any housing-related benefits, will they still need to pay basic rent and other charges?", - field_88: "What do you expect the outstanding amount to be?", - field_89: "What is the void or renewal date?", - field_90: "What is the void or renewal date?", - field_91: "What is the void or renewal date?", - field_92: "What date were major repairs completed on?", - field_93: "What date were major repairs completed on?", - field_94: "What date were major repairs completed on?", - field_95: "This question has been removed", - field_96: "What date did the tenancy start?", - field_97: "What date did the tenancy start?", - field_98: "What date did the tenancy start?", - field_99: "Since becoming available, how many times has the property been previously offered?", - field_100: "What is the property reference?", - field_101: "How many bedrooms does the property have?", - field_102: "What type of unit is the property?", - field_103: "Which type of building is the property?", - field_104: "Is the property built or adapted to wheelchair-user standards?", - field_105: "What type was the property most recently let as?", - field_106: "What is the reason for the property being vacant?", - field_107: "What is the local authority of the property?", - field_108: "Part 1 of postcode of the property", - field_109: "Part 2 of postcode of the property", - field_110: "This question has been removed", - field_111: "Which organisation owns this property?", - field_112: "Username field", - field_113: "Which organisation manages this property?", - field_114: "Is the person still serving in the UK armed forces?", - field_115: "This question has been removed", - field_116: "How often does the household receive income?", - field_117: "Is this letting sheltered accommodation?", - field_118: "Does anybody in the household have a physical or mental health condition (or other illness) expected to last for 12 months or more?", - field_119: "Vision, for example blindness or partial sight", - field_120: "Hearing, for example deafness or partial hearing", - field_121: "Mobility, for example walking short distances or climbing stairs", - field_122: "Dexterity, for example lifting and carrying objects, using a keyboard", - field_123: "Learning or understanding or concentrating", - field_124: "Memory", - field_125: "Mental health", - field_126: "Stamina or breathing or fatigue", - field_127: "Socially or behaviourally, for example associated with autism spectral disorder (ASD) which includes Aspergers' or attention deficit hyperactivity disorder (ADHD)", - field_128: "Other", - field_129: "Is this letting a London Affordable Rent letting?", - field_130: "Which type of Intermediate Rent is this letting?", - field_131: "Which 'Other' type of Intermediate Rent is this letting?", - field_132: "Data Protection", - field_133: "Is this a joint tenancy?", - field_134: "Is this letting a renewal?", - }.freeze - attr_reader :bulk_upload, :path validate :validate_file_not_empty @@ -167,7 +30,7 @@ class BulkUpload::Lettings::Validator property_ref: row_parser.field_100, row:, cell: "#{cols[field_number_for_attribute(error.attribute) - col_offset + 1]}#{row}", - col: cols[field_number_for_attribute(error.attribute) - col_offset + 1], + col: csv_parser.column_for_field(error.attribute.to_s), category: error.options[:category], ) end @@ -210,7 +73,14 @@ private end def csv_parser - @csv_parser ||= BulkUpload::Lettings::CsvParser.new(path:) + @csv_parser ||= case bulk_upload.year + when 2022 + BulkUpload::Lettings::Year2022::CsvParser.new(path:) + when 2023 + BulkUpload::Lettings::Year2023::CsvParser.new(path:) + else + raise "csv parser not found" + end end def row_offset @@ -260,9 +130,9 @@ private def validate_max_columns return if halt_validations? - max_row_size = rows.map(&:size).max + column_count = rows.map(&:size).max - errors.add(:file, :max_row_size) if max_row_size > 136 + errors.add(:file, :column_count) if column_count > csv_parser.class::MAX_COLUMNS end def halt_validations! diff --git a/app/services/bulk_upload/lettings/csv_parser.rb b/app/services/bulk_upload/lettings/year2022/csv_parser.rb similarity index 76% rename from app/services/bulk_upload/lettings/csv_parser.rb rename to app/services/bulk_upload/lettings/year2022/csv_parser.rb index 6cead61ab..81977c3cb 100644 --- a/app/services/bulk_upload/lettings/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/csv_parser.rb @@ -1,6 +1,8 @@ require "csv" -class BulkUpload::Lettings::CsvParser +class BulkUpload::Lettings::Year2022::CsvParser + MAX_COLUMNS = 136 + attr_reader :path def initialize(path:) @@ -25,7 +27,7 @@ class BulkUpload::Lettings::CsvParser headers = ("field_1".."field_134").to_a hash = Hash[headers.zip(stripped_row)] - BulkUpload::Lettings::RowParser.new(hash) + BulkUpload::Lettings::Year2022::RowParser.new(hash) end end @@ -37,8 +39,16 @@ class BulkUpload::Lettings::CsvParser @rows ||= CSV.parse(normalised_string, row_sep:) end + def column_for_field(field) + cols[headers.find_index(field) + col_offset] + end + private + def headers + @headers ||= ("field_1".."field_134").to_a + end + def with_headers? rows[0][0]&.match?(/\D+/) end diff --git a/app/services/bulk_upload/lettings/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb similarity index 78% rename from app/services/bulk_upload/lettings/row_parser.rb rename to app/services/bulk_upload/lettings/year2022/row_parser.rb index ea1ca963a..69b7d9b4e 100644 --- a/app/services/bulk_upload/lettings/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -1,7 +1,144 @@ -class BulkUpload::Lettings::RowParser +class BulkUpload::Lettings::Year2022::RowParser include ActiveModel::Model include ActiveModel::Attributes + QUESTIONS = { + field_1: "What is the letting type?", + field_2: "This question has been removed", + field_3: "This question has been removed", + field_4: "Management group code", + field_5: "Scheme code", + field_6: "This question has been removed", + field_7: "What is the tenant code?", + field_8: "Is this a starter tenancy?", + field_9: "What is the tenancy type?", + field_10: "If 'Other', what is the tenancy type?", + field_11: "What is the length of the fixed-term tenancy to the nearest year?", + field_12: "Age of Person 1", + field_13: "Age of Person 2", + field_14: "Age of Person 3", + field_15: "Age of Person 4", + field_16: "Age of Person 5", + field_17: "Age of Person 6", + field_18: "Age of Person 7", + field_19: "Age of Person 8", + field_20: "Gender identity of Person 1", + field_21: "Gender identity of Person 2", + field_22: "Gender identity of Person 3", + field_23: "Gender identity of Person 4", + field_24: "Gender identity of Person 5", + field_25: "Gender identity of Person 6", + field_26: "Gender identity of Person 7", + field_27: "Gender identity of Person 8", + field_28: "Relationship to Person 1 for Person 2", + field_29: "Relationship to Person 1 for Person 3", + field_30: "Relationship to Person 1 for Person 4", + field_31: "Relationship to Person 1 for Person 5", + field_32: "Relationship to Person 1 for Person 6", + field_33: "Relationship to Person 1 for Person 7", + field_34: "Relationship to Person 1 for Person 8", + field_35: "Working situation of Person 1", + field_36: "Working situation of Person 2", + field_37: "Working situation of Person 3", + field_38: "Working situation of Person 4", + field_39: "Working situation of Person 5", + field_40: "Working situation of Person 6", + field_41: "Working situation of Person 7", + field_42: "Working situation of Person 8", + field_43: "What is the lead tenant's ethnic group?", + field_44: "What is the lead tenant's nationality?", + field_45: "Does anybody in the household have links to the UK armed forces?", + field_46: "Was the person seriously injured or ill as a result of serving in the UK armed forces?", + field_47: "Is anybody in the household pregnant?", + field_48: "Is the tenant likely to be receiving benefits related to housing?", + field_49: "How much of the household's income is from Universal Credit, state pensions or benefits?", + field_50: "How much income does the household have in total?", + field_51: "Do you know the household's income?", + field_52: "What is the tenant's main reason for the household leaving their last settled home?", + field_53: "If 'Other', what was the main reason for leaving their last settled home?", + field_54: "This question has been removed", + field_55: "Does anybody in the household have any disabled access needs?", + field_56: "Does anybody in the household have any disabled access needs?", + field_57: "Does anybody in the household have any disabled access needs?", + field_58: "Does anybody in the household have any disabled access needs?", + field_59: "Does anybody in the household have any disabled access needs?", + field_60: "Does anybody in the household have any disabled access needs?", + field_61: "Where was the household immediately before this letting?", + field_62: "What is the local authority of the household's last settled home?", + field_63: "Part 1 of postcode of last settled home", + field_64: "Part 2 of postcode of last settled home", + field_65: "Do you know the postcode of last settled home?", + field_66: "How long has the household continuously lived in the local authority area of the new letting?", + field_67: "How long has the household been on the waiting list for the new letting?", + field_68: "Was the tenant homeless directly before this tenancy?", + field_69: "Was the household given 'reasonable preference' by the local authority?", + field_70: "Reasonable preference. They were homeless or about to lose their home (within 56 days)", + field_71: "Reasonable preference. They were living in insanitary, overcrowded or unsatisfactory housing", + field_72: "Reasonable preference. They needed to move on medical and welfare grounds (including a disability)", + field_73: "Reasonable preference. They needed to move to avoid hardship to themselves or others", + field_74: "Reasonable preference. Don't know", + field_75: "Was the letting made under any of the following allocations systems?", + field_76: "Was the letting made under any of the following allocations systems?", + field_77: "Was the letting made under any of the following allocations systems?", + field_78: "What was the source of referral for this letting?", + field_79: "How often does the household pay rent and other charges?", + field_80: "What is the basic rent?", + field_81: "What is the service charge?", + field_82: "What is the personal service charge?", + field_83: "What is the support charge?", + field_84: "Total Charge", + field_85: "If this is a care home, how much does the household pay every [time period]?", + field_86: "Does the household pay rent or other charges for the accommodation?", + field_87: "After the household has received any housing-related benefits, will they still need to pay basic rent and other charges?", + field_88: "What do you expect the outstanding amount to be?", + field_89: "What is the void or renewal date?", + field_90: "What is the void or renewal date?", + field_91: "What is the void or renewal date?", + field_92: "What date were major repairs completed on?", + field_93: "What date were major repairs completed on?", + field_94: "What date were major repairs completed on?", + field_95: "This question has been removed", + field_96: "What date did the tenancy start?", + field_97: "What date did the tenancy start?", + field_98: "What date did the tenancy start?", + field_99: "Since becoming available, how many times has the property been previously offered?", + field_100: "What is the property reference?", + field_101: "How many bedrooms does the property have?", + field_102: "What type of unit is the property?", + field_103: "Which type of building is the property?", + field_104: "Is the property built or adapted to wheelchair-user standards?", + field_105: "What type was the property most recently let as?", + field_106: "What is the reason for the property being vacant?", + field_107: "What is the local authority of the property?", + field_108: "Part 1 of postcode of the property", + field_109: "Part 2 of postcode of the property", + field_110: "This question has been removed", + field_111: "Which organisation owns this property?", + field_112: "Username field", + field_113: "Which organisation manages this property?", + field_114: "Is the person still serving in the UK armed forces?", + field_115: "This question has been removed", + field_116: "How often does the household receive income?", + field_117: "Is this letting sheltered accommodation?", + field_118: "Does anybody in the household have a physical or mental health condition (or other illness) expected to last for 12 months or more?", + field_119: "Vision, for example blindness or partial sight", + field_120: "Hearing, for example deafness or partial hearing", + field_121: "Mobility, for example walking short distances or climbing stairs", + field_122: "Dexterity, for example lifting and carrying objects, using a keyboard", + field_123: "Learning or understanding or concentrating", + field_124: "Memory", + field_125: "Mental health", + field_126: "Stamina or breathing or fatigue", + field_127: "Socially or behaviourally, for example associated with autism spectral disorder (ASD) which includes Aspergers' or attention deficit hyperactivity disorder (ADHD)", + field_128: "Other", + field_129: "Is this letting a London Affordable Rent letting?", + field_130: "Which type of Intermediate Rent is this letting?", + field_131: "Which 'Other' type of Intermediate Rent is this letting?", + field_132: "Data Protection", + field_133: "Is this a joint tenancy?", + field_134: "Is this letting a renewal?", + }.freeze + attribute :bulk_upload attribute :block_log_creation, :boolean, default: -> { false } @@ -188,6 +325,10 @@ class BulkUpload::Lettings::RowParser validate :validate_location_exists validate :validate_location_data_given + def self.question_for_field(field) + QUESTIONS[field] + end + def valid? errors.clear @@ -211,7 +352,12 @@ class BulkUpload::Lettings::RowParser end def blank_row? - attribute_set.to_hash.reject { |k, _| %w[bulk_upload block_log_creation].include?(k) }.values.compact.empty? + attribute_set + .to_hash + .reject { |k, _| %w[bulk_upload block_log_creation].include?(k) } + .values + .compact + .empty? end def log diff --git a/app/services/bulk_upload/lettings/year2023/csv_parser.rb b/app/services/bulk_upload/lettings/year2023/csv_parser.rb new file mode 100644 index 000000000..550ef1825 --- /dev/null +++ b/app/services/bulk_upload/lettings/year2023/csv_parser.rb @@ -0,0 +1,80 @@ +require "csv" + +class BulkUpload::Lettings::Year2023::CsvParser + MAX_COLUMNS = 143 + + attr_reader :path + + def initialize(path:) + @path = path + end + + def row_offset + if with_headers? + rows.find_index { |row| row[0].match(/field number/i) } + 1 + else + 0 + end + end + + def col_offset + with_headers? ? 1 : 0 + end + + def cols + @cols ||= ("A".."EL").to_a + end + + def row_parsers + @row_parsers ||= body_rows.map do |row| + stripped_row = row[col_offset..] + hash = Hash[field_numbers.zip(stripped_row)] + + BulkUpload::Lettings::Year2023::RowParser.new(hash) + end + end + + def body_rows + rows[row_offset..] + end + + def rows + @rows ||= CSV.parse(normalised_string, row_sep:) + end + + def column_for_field(field) + cols[field_numbers.find_index(field) + col_offset] + end + +private + + def default_field_numbers + [5, nil, nil, 15, 16, nil, 13, 40, 41, 42, 43, 46, 52, 56, 60, 64, 68, 72, 76, 47, 53, 57, 61, 65, 69, 73, 77, 51, 55, 59, 63, 67, 71, 75, 50, 54, 58, 62, 66, 70, 74, 78, 48, 49, 79, 81, 82, 123, 124, 122, 120, 102, 103, nil, 83, 84, 85, 86, 87, 88, 104, 109, 107, 108, 106, 100, 101, 105, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 126, 128, 129, 130, 131, 132, 127, 125, 133, 134, 33, 34, 35, 36, 37, 38, nil, 7, 8, 9, 28, 14, 32, 29, 30, 31, 26, 27, 25, 23, 24, nil, 1, 3, 2, 80, nil, 121, 44, 89, 98, 92, 95, 90, 91, 93, 94, 97, 96, 99, 10, 11, 12, 45, 39, 6, 4, 17, 18, 19, 20, 21, 22].map { |h| h.present? ? "field_#{h}" : "field_blank" } + end + + def field_numbers + @field_numbers ||= if with_headers? + rows[row_offset - 1][col_offset..].map { |h| h.present? ? "field_#{h}" : "field_blank" } + else + default_field_numbers + end + end + + def with_headers? + rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) } + end + + def row_sep + "\n" + end + + def normalised_string + return @normalised_string if @normalised_string + + @normalised_string = File.read(path, encoding: "bom|utf-8") + @normalised_string.gsub!("\r\n", "\n") + @normalised_string.scrub!("") + + @normalised_string + end +end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb new file mode 100644 index 000000000..be20f6515 --- /dev/null +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -0,0 +1,1233 @@ +class BulkUpload::Lettings::Year2023::RowParser + include ActiveModel::Model + include ActiveModel::Attributes + + QUESTIONS = { + field_1: "Which organisation owns this property?", + field_2: "Which organisation manages this letting?", + field_3: "What is the CORE username of the account this letting log should be assigned to?", + field_4: "What is the needs type?", + field_5: "What is the letting type?", + field_6: "Is this letting a renewal?", + field_7: "What is the tenancy start date?", + field_8: "What is the tenancy start date?", + field_9: "What is the tenancy start date?", + field_10: "Is this a London Affordable Rent letting?", + field_11: "Which type of Intermediate Rent is this letting?", + field_12: "Which 'Other' type of Intermediate Rent is this letting?", + field_13: "What is the tenant code?", + field_14: "What is the property reference?", + field_15: "What management group does this letting belong to?", + field_16: "What scheme does this letting belong to?", + field_17: "Which location is this letting for?", + field_18: "If known, provide this property’s UPRN", + field_19: "Address line 1", + field_20: "Address line 2", + field_21: "Town or city", + field_22: "County", + field_23: "Part 1 of the property's postcode", + field_24: "Part 2 of the property's postcode", + field_25: "What is the property's local authority?", + field_26: "What type was the property most recently let as?", + field_27: "What is the reason for the property being vacant?", + field_28: "How many times was the property offered between becoming vacant and this letting?", + field_29: "What type of unit is the property?", + field_30: "Which type of building is the property?", + field_31: "Is the property built or adapted to wheelchair-user standards?", + field_32: "How many bedrooms does the property have?", + field_33: "What is the void date?", + field_34: "What is the void date?", + field_35: "What is the void date?", + field_36: "What date were any major repairs completed on?", + field_37: "What date were any major repairs completed on?", + field_38: "What date were any major repairs completed on?", + field_39: "Is this a joint tenancy?", + field_40: "Is this a starter tenancy?", + field_41: "What is the type of tenancy?", + field_42: "If 'Other', what is the type of tenancy?", + field_43: "What is the length of the fixed-term tenancy to the nearest year?", + field_44: "Is this letting sheltered accommodation?", + field_45: "Has tenant seen the DLUHC privacy notice?", + field_46: "What is the lead tenant's age?", + field_47: "Which of these best describes the lead tenant's gender identity?", + field_48: "Which of these best describes the lead tenant's ethnic background?", + field_49: "What is the lead tenant's nationality?", + field_50: "Which of these best describes the lead tenant's working situation?", + field_51: "What is person 2's relationship to the lead tenant?", + field_52: "What is person 2's age?", + field_53: "Which of these best describes person 2's gender identity?", + field_54: "Which of these best describes person 2's working situation?", + field_55: "What is person 3's relationship to the lead tenant?", + field_56: "What is person 3's age?", + field_57: "Which of these best describes person 3's gender identity?", + field_58: "Which of these best describes person 3's working situation?", + field_59: "What is person 4's relationship to the lead tenant?", + field_60: "What is person 4's age?", + field_61: "Which of these best describes person 4's gender identity?", + field_62: "Which of these best describes person 4's working situation?", + field_63: "What is person 5's relationship to the lead tenant?", + field_64: "What is person 5's age?", + field_65: "Which of these best describes person 5's gender identity?", + field_66: "Which of these best describes person 5's working situation?", + field_67: "What is person 6's relationship to the lead tenant?", + field_68: "What is person 6's age?", + field_69: "Which of these best describes person 6's gender identity?", + field_70: "Which of these best describes person 6's working situation?", + field_71: "What is person 7's relationship to the lead tenant?", + field_72: "What is person 7's age?", + field_73: "Which of these best describes person 7's gender identity?", + field_74: "Which of these best describes person 7's working situation?", + field_75: "What is person 8's relationship to the lead tenant?", + field_76: "What is person 8's age?", + field_77: "Which of these best describes person 8's gender identity?", + field_78: "Which of these best describes person 8's working situation?", + field_79: "Does anybody in the household have links to the UK armed forces?", + field_80: "Is this person still serving in the UK armed forces?", + field_81: "Was this person seriously injured or ill as a result of serving in the UK armed forces?", + field_82: "Is anybody in the household pregnant?", + field_83: "Does anybody in the household have any disabled access needs?", + field_84: "Does anybody in the household have any disabled access needs?", + field_85: "Does anybody in the household have any disabled access needs?", + field_86: "Does anybody in the household have any disabled access needs?", + field_87: "Does anybody in the household have any disabled access needs?", + field_88: "Does anybody in the household have any disabled access needs?", + field_89: "Does anybody in the household have a physical or mental health condition (or other illness) expected to last 12 months or more?", + field_90: "Does this person's condition affect their dexterity?", + field_91: "Does this person's condition affect their learning or understanding or concentrating?", + field_92: "Does this person's condition affect their hearing?", + field_93: "Does this person's condition affect their memory?", + field_94: "Does this person's condition affect their mental health?", + field_95: "Does this person's condition affect their mobility?", + field_96: "Does this person's condition affect them socially or behaviourally?", + field_97: "Does this person's condition affect their stamina or breathing or fatigue?", + field_98: "Does this person's condition affect their vision?", + field_99: "Does this person's condition affect them in another way?", + field_100: "How long has the household continuously lived in the local authority area of the new letting?", + field_101: "How long has the household been on the local authority waiting list for the new letting?", + field_102: "What is the tenant’s main reason for the household leaving their last settled home?", + field_103: "If 'Other', what was the main reason for leaving their last settled home?", + field_104: "Where was the household immediately before this letting?", + field_105: "Did the household experience homelessness immediately before this letting?", + field_106: "Do you know the postcode of the household's last settled home?", + field_107: "What is the postcode of the household's last settled home?", + field_108: "What is the postcode of the household's last settled home?", + field_109: "What is the local authority of the household's last settled home?", + field_110: "Was the household given 'reasonable preference' by the local authority?", + field_111: "Reasonable preference reason They were homeless or about to lose their home (within 56 days)", + field_112: "Reasonable preference reason They were living in insanitary, overcrowded or unsatisfactory housing", + field_113: "Reasonable preference reason They needed to move on medical and welfare reasons (including disability)", + field_114: "Reasonable preference reason They needed to move to avoid hardship to themselves or others", + field_115: "Reasonable preference reason Don't know", + field_116: "How was this letting allocated?", + field_117: "How was this letting allocated?", + field_118: "How was this letting allocated?", + field_119: "What was the source of referral for this letting?", + field_120: "Do you know the household's combined total income after tax?", + field_121: "How often does the household receive income?", + field_122: "How much income does the household have in total?", + field_123: "Is the tenant likely to be receiving any of these housing-related benefits?", + field_124: "How much of the household's income is from Universal Credit, state pensions or benefits?", + field_125: "Does the household pay rent or other charges for the accommodation?", + field_126: "How often does the household pay rent and other charges?", + field_127: "If this is a care home, how much does the household pay every [time period]?", + field_128: "What is the basic rent?", + field_129: "What is the service charge?", + field_130: "What is the personal service charge?", + field_131: "What is the support charge?", + field_132: "Total charge", + field_133: "After the household has received any housing-related benefits, will they still need to pay for rent and charges?", + field_134: "What do you expect the outstanding amount to be?", + }.freeze + + attribute :bulk_upload + attribute :block_log_creation, :boolean, default: -> { false } + + attribute :field_blank + + attribute :field_1, :string + attribute :field_2, :string + attribute :field_3, :string + attribute :field_4, :integer + attribute :field_5, :integer + attribute :field_6, :integer + attribute :field_7, :integer + attribute :field_8, :integer + attribute :field_9, :integer + attribute :field_10, :integer + attribute :field_11, :integer + attribute :field_12, :string + attribute :field_13, :string + attribute :field_14, :string + attribute :field_15, :string + attribute :field_16, :string + attribute :field_17, :string + attribute :field_18, :string + attribute :field_19, :string + attribute :field_20, :string + attribute :field_21, :string + attribute :field_22, :string + attribute :field_23, :string + attribute :field_24, :string + attribute :field_25, :string + attribute :field_26, :integer + attribute :field_27, :integer + attribute :field_28, :integer + attribute :field_29, :integer + attribute :field_30, :integer + attribute :field_31, :integer + attribute :field_32, :integer + attribute :field_33, :integer + attribute :field_34, :integer + attribute :field_35, :integer + attribute :field_36, :integer + attribute :field_37, :integer + attribute :field_38, :integer + attribute :field_39, :integer + attribute :field_40, :integer + attribute :field_41, :integer + attribute :field_42, :string + attribute :field_43, :integer + attribute :field_44, :integer + attribute :field_45, :integer + attribute :field_46, :string + attribute :field_47, :string + attribute :field_48, :integer + attribute :field_49, :integer + attribute :field_50, :integer + attribute :field_51, :string + attribute :field_52, :string + attribute :field_53, :string + attribute :field_54, :integer + attribute :field_55, :string + attribute :field_56, :string + attribute :field_57, :string + attribute :field_58, :integer + attribute :field_59, :string + attribute :field_60, :string + attribute :field_61, :string + attribute :field_62, :integer + attribute :field_63, :string + attribute :field_64, :string + attribute :field_65, :string + attribute :field_66, :integer + attribute :field_67, :string + attribute :field_68, :string + attribute :field_69, :string + attribute :field_70, :integer + attribute :field_71, :string + attribute :field_72, :string + attribute :field_73, :string + attribute :field_74, :integer + attribute :field_75, :string + attribute :field_76, :string + attribute :field_77, :string + attribute :field_78, :integer + attribute :field_79, :integer + attribute :field_80, :integer + attribute :field_81, :integer + attribute :field_82, :integer + attribute :field_83, :integer + attribute :field_84, :integer + attribute :field_85, :integer + attribute :field_86, :integer + attribute :field_87, :integer + attribute :field_88, :integer + attribute :field_89, :integer + attribute :field_90, :integer + attribute :field_91, :integer + attribute :field_92, :integer + attribute :field_93, :integer + attribute :field_94, :integer + attribute :field_95, :integer + attribute :field_96, :integer + attribute :field_97, :integer + attribute :field_98, :integer + attribute :field_99, :integer + attribute :field_100, :integer + attribute :field_101, :integer + attribute :field_102, :integer + attribute :field_103, :string + attribute :field_104, :integer + attribute :field_105, :integer + attribute :field_106, :integer + attribute :field_107, :string + attribute :field_108, :string + attribute :field_109, :string + attribute :field_110, :integer + attribute :field_111, :integer + attribute :field_112, :integer + attribute :field_113, :integer + attribute :field_114, :integer + attribute :field_115, :integer + attribute :field_116, :integer + attribute :field_117, :integer + attribute :field_118, :integer + attribute :field_119, :integer + attribute :field_120, :integer + attribute :field_121, :integer + attribute :field_122, :decimal + attribute :field_123, :integer + attribute :field_124, :integer + attribute :field_125, :integer + attribute :field_126, :integer + attribute :field_127, :decimal + attribute :field_128, :decimal + attribute :field_129, :decimal + attribute :field_130, :decimal + attribute :field_131, :decimal + attribute :field_132, :decimal + attribute :field_133, :integer + attribute :field_134, :decimal + + validates :field_5, presence: { message: I18n.t("validations.not_answered", question: "letting type") }, + inclusion: { in: (1..12).to_a, message: I18n.t("validations.invalid_option", question: "letting type") } + validates :field_16, presence: { if: proc { [2, 4, 6, 8, 10, 12].include?(field_5) } } + + validates :field_46, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 1 must be a number or the letter R" } + validates :field_52, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 2 must be a number or the letter R" } + validates :field_56, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 3 must be a number or the letter R" } + validates :field_60, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 4 must be a number or the letter R" } + validates :field_64, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 5 must be a number or the letter R" } + validates :field_68, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 6 must be a number or the letter R" } + validates :field_72, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 7 must be a number or the letter R" } + validates :field_76, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 8 must be a number or the letter R" } + + validates :field_7, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (day)") } + validates :field_8, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (month)") } + validates :field_9, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (year)") } + + validates :field_9, format: { with: /\A\d{2}\z/, message: I18n.t("validations.setup.startdate.year_not_two_digits") } + + validate :validate_needs_type_present + validate :validate_data_types + validate :validate_nulls + validate :validate_relevant_collection_window + validate :validate_la_with_local_housing_referral + validate :validate_cannot_be_la_referral_if_general_needs_and_la + validate :validate_leaving_reason_for_renewal + validate :validate_lettings_type_matches_bulk_upload + validate :validate_only_one_housing_needs_type + validate :validate_no_disabled_needs_conjunction + validate :validate_dont_know_disabled_needs_conjunction + validate :validate_no_and_dont_know_disabled_needs_conjunction + + validate :validate_owning_org_permitted + validate :validate_owning_org_owns_stock + validate :validate_owning_org_exists + validate :validate_owning_org_data_given + + validate :validate_managing_org_related + validate :validate_managing_org_exists + validate :validate_managing_org_data_given + + validate :validate_scheme_related + validate :validate_scheme_exists + validate :validate_scheme_data_given + + validate :validate_location_related + validate :validate_location_exists + validate :validate_location_data_given + + def self.question_for_field(field) + QUESTIONS[field] + end + + def valid? + errors.clear + + return true if blank_row? + + super + + log.valid? + + log.errors.each do |error| + fields = field_mapping_for_errors[error.attribute] || [] + + fields.each do |field| + unless errors.include?(field) + errors.add(field, error.type) + end + end + end + + errors.blank? + end + + def blank_row? + attribute_set + .to_hash + .reject { |k, _| %w[bulk_upload block_log_creation field_blank].include?(k) } + .values + .compact + .empty? + end + + def log + @log ||= LettingsLog.new(attributes_for_log) + end + + def block_log_creation! + self.block_log_creation = true + end + + def block_log_creation? + block_log_creation + end + +private + + def validate_needs_type_present + if field_4.blank? + errors.add(:field_4, I18n.t("validations.not_answered", question: "needs type")) + end + end + + def start_date + return if field_7.blank? || field_8.blank? || field_9.blank? + + Date.parse("20#{field_9.to_s.rjust(2, '0')}-#{field_8}-#{field_7}") + rescue StandardError + nil + end + + def validate_no_and_dont_know_disabled_needs_conjunction + if field_87 == 1 && field_88 == 1 + errors.add(:field_87, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) + errors.add(:field_88, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) + end + 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")) + 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")) + end + end + + def validate_only_one_housing_needs_type + if [field_83, field_84, field_85].compact.count.positive? + 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")) + end + end + + def validate_lettings_type_matches_bulk_upload + if [1, 3, 5, 7, 9, 11].include?(field_5) && !general_needs? + errors.add(:field_5, I18n.t("validations.setup.lettype.supported_housing_mismatch")) + end + + if [2, 4, 6, 8, 10, 12].include?(field_5) && !supported_housing? + errors.add(:field_5, I18n.t("validations.setup.lettype.general_needs_mismatch")) + end + end + + def validate_leaving_reason_for_renewal + if field_6 == 1 && ![40, 42].include?(field_102) + errors.add(:field_102, I18n.t("validations.household.reason.renewal_reason_needed")) + end + end + + def general_needs? + field_4 == 1 + end + + def supported_housing? + field_4 == 2 + end + + def validate_cannot_be_la_referral_if_general_needs_and_la + if field_119 == 4 && general_needs? && owning_organisation && owning_organisation.la? + errors.add :field_119, I18n.t("validations.household.referral.la_general_needs.prp_referred_by_la") + end + end + + def validate_la_with_local_housing_referral + if field_119 == 3 && owning_organisation && owning_organisation.la? + errors.add(:field_119, I18n.t("validations.household.referral.nominated_by_local_ha_but_la")) + end + end + + def validate_relevant_collection_window + return if start_date.blank? || bulk_upload.form.blank? + + unless bulk_upload.form.valid_start_date_for_form?(start_date) + errors.add(:field_7, I18n.t("validations.date.outside_collection_window")) + errors.add(:field_8, I18n.t("validations.date.outside_collection_window")) + errors.add(:field_9, I18n.t("validations.date.outside_collection_window")) + end + end + + def validate_data_types + unless attribute_set["field_5"].value_before_type_cast&.match?(/\A\d+\z/) + errors.add(:field_5, I18n.t("validations.invalid_number", question: "letting type")) + end + end + + def validate_nulls + field_mapping_for_errors.each do |error_key, fields| + question_id = error_key.to_s + question = questions.find { |q| q.id == question_id } + + next unless question + next if log.optional_fields.include?(question.id) + next if question.completed?(log) + + if setup_question?(question) + fields.each do |field| + if errors[field].present? + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase), category: :setup) + end + end + else + fields.each do |field| + unless errors.any? { |e| fields.include?(e.attribute) } + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase)) + end + end + end + end + end + + def validate_location_related + return if scheme.blank? || location.blank? + + unless location.scheme == scheme + block_log_creation! + errors.add(:field_17, "Scheme code must relate to a location that is owned by owning organisation or managing organisation") + end + end + + def validate_location_exists + if scheme && field_17.present? && location.nil? + errors.add(:field_17, "Location could be found with provided scheme code") + end + end + + def validate_location_data_given + if supported_housing? && field_17.blank? + errors.add(:field_17, "The scheme code must be present", category: "setup") + end + end + + def validate_scheme_related + return unless field_16.present? && scheme.present? + + owned_by_owning_org = owning_organisation && scheme.owning_organisation == owning_organisation + owned_by_managing_org = managing_organisation && scheme.owning_organisation == managing_organisation + + unless owned_by_owning_org || owned_by_managing_org + block_log_creation! + errors.add(:field_16, "This management group code does not belong to your organisation, or any of your stock owners / managing agents") + end + end + + def validate_scheme_exists + if field_16.present? && scheme.nil? + errors.add(:field_16, "The management group code is not correct") + end + end + + def validate_scheme_data_given + if supported_housing? && field_16.blank? + errors.add(:field_16, "The management group code is not correct", category: "setup") + end + end + + def validate_managing_org_related + if owning_organisation && managing_organisation && !owning_organisation.can_be_managed_by?(organisation: managing_organisation) + block_log_creation! + errors.add(:field_2, "This managing organisation does not have a relationship with the owning organisation") + end + end + + def validate_managing_org_exists + if managing_organisation.nil? + errors.delete(:field_2) + errors.add(:field_2, "The managing organisation code is incorrect") + end + end + + def validate_managing_org_data_given + if field_2.blank? + errors.add(:field_2, "The managing organisation code is incorrect", category: :setup) + end + end + + def validate_owning_org_owns_stock + if owning_organisation && !owning_organisation.holds_own_stock? + block_log_creation! + errors.delete(:field_1) + errors.add(:field_1, "The owning organisation code provided is for an organisation that does not own stock") + end + end + + def validate_owning_org_exists + if owning_organisation.nil? + errors.delete(:field_1) + errors.add(:field_1, "The owning organisation code is incorrect") + end + end + + def validate_owning_org_data_given + if field_1.blank? + errors.add(:field_1, "The owning organisation code is incorrect", category: :setup) + end + end + + def validate_owning_org_permitted + if owning_organisation && !bulk_upload.user.organisation.affiliated_stock_owners.include?(owning_organisation) + block_log_creation! + errors.delete(:field_1) + errors.add(:field_1, "You do not have permission to add logs for this owning organisation") + end + end + + def setup_question?(question) + log.form.setup_sections[0].subsections[0].questions.include?(question) + end + + def field_mapping_for_errors + { + lettype: [:field_5], + tenancycode: [:field_13], + postcode_known: %i[field_25 field_23 field_24], + postcode_full: %i[field_25 field_23 field_24], + la: %i[field_25], + owning_organisation: [:field_1], + managing_organisation: [:field_2], + owning_organisation_id: [:field_1], + managing_organisation_id: [:field_2], + renewal: [:field_6], + scheme: %i[field_16], + location: %i[field_17], + created_by: [], + needstype: [:field_4], + rent_type: %i[field_5 field_10 field_11], + startdate: %i[field_7 field_8 field_9], + unittype_gn: %i[field_29], + builtype: %i[field_30], + wchair: %i[field_31], + beds: %i[field_32], + joint: %i[field_39], + startertenancy: %i[field_40], + tenancy: %i[field_41], + tenancyother: %i[field_42], + tenancylength: %i[field_43], + declaration: %i[field_45], + + age1_known: %i[field_46], + age1: %i[field_46], + age2_known: %i[field_52], + age2: %i[field_52], + age3_known: %i[field_56], + age3: %i[field_56], + age4_known: %i[field_60], + age4: %i[field_60], + age5_known: %i[field_64], + age5: %i[field_64], + age6_known: %i[field_68], + age6: %i[field_68], + age7_known: %i[field_72], + age7: %i[field_72], + age8_known: %i[field_76], + age8: %i[field_76], + + sex1: %i[field_47], + sex2: %i[field_53], + sex3: %i[field_57], + sex4: %i[field_61], + sex5: %i[field_65], + sex6: %i[field_69], + sex7: %i[field_73], + sex8: %i[field_77], + + ethnic_group: %i[field_48], + ethnic: %i[field_48], + national: %i[field_49], + + relat2: %i[field_51], + relat3: %i[field_55], + relat4: %i[field_59], + relat5: %i[field_63], + relat6: %i[field_67], + relat7: %i[field_71], + relat8: %i[field_75], + + ecstat1: %i[field_50], + ecstat2: %i[field_54], + ecstat3: %i[field_58], + ecstat4: %i[field_62], + ecstat5: %i[field_66], + ecstat6: %i[field_70], + ecstat7: %i[field_74], + ecstat8: %i[field_78], + + armedforces: %i[field_79], + leftreg: %i[field_80], + reservist: %i[field_81], + preg_occ: %i[field_82], + housingneeds: %i[field_82], + + illness: %i[field_89], + + layear: %i[field_100], + waityear: %i[field_101], + reason: %i[field_102], + reasonother: %i[field_103], + prevten: %i[field_104], + homeless: %i[field_105], + + prevloc: %i[field_109], + previous_la_known: %i[field_109], + ppcodenk: %i[field_106], + ppostcode_full: %i[field_107 field_108], + + reasonpref: %i[field_110], + rp_homeless: %i[field_111], + rp_insan_unsat: %i[field_112], + rp_medwel: %i[field_113], + rp_hardship: %i[field_114], + rp_dontknow: %i[field_115], + + cbl: %i[field_116], + chr: %i[field_118], + cap: %i[field_117], + + referral: %i[field_119], + + net_income_known: %i[field_120], + earnings: %i[field_122], + incfreq: %i[field_121], + hb: %i[field_123], + benefits: %i[field_124], + + period: %i[field_126], + brent: %i[field_128], + scharge: %i[field_129], + pscharge: %i[field_130], + supcharg: %i[field_131], + tcharge: %i[field_132], + chcharge: %i[field_127], + household_charge: %i[field_125], + hbrentshortfall: %i[field_133], + tshortfall: %i[field_134], + + unitletas: %i[field_26], + rsnvac: %i[field_27], + sheltered: %i[field_44], + + illness_type_1: %i[field_98], + illness_type_2: %i[field_92], + illness_type_3: %i[field_95], + illness_type_4: %i[field_90], + illness_type_5: %i[field_91], + illness_type_6: %i[field_93], + illness_type_7: %i[field_94], + illness_type_8: %i[field_97], + illness_type_9: %i[field_96], + illness_type_10: %i[field_99], + + irproduct_other: %i[field_12], + + offered: %i[field_28], + propcode: %i[field_14], + + majorrepairs: %i[field_36 field_37 field_38], + mrcdate: %i[field_36 field_37 field_38], + + voiddate: %i[field_33 field_34 field_35], + } + end + + def attribute_set + @attribute_set ||= instance_variable_get(:@attributes) + end + + def questions + log.form.subsections.flat_map { |ss| ss.applicable_questions(log) } + end + + def attributes_for_log + attributes = {} + + attributes["lettype"] = field_5 + attributes["tenancycode"] = field_13 + attributes["la"] = field_25 + attributes["postcode_known"] = postcode_known + attributes["postcode_full"] = postcode_full + attributes["owning_organisation_id"] = owning_organisation_id + attributes["managing_organisation_id"] = managing_organisation_id + attributes["renewal"] = renewal + attributes["scheme"] = scheme + attributes["location"] = location + attributes["created_by"] = bulk_upload.user + attributes["needstype"] = field_4 + attributes["rent_type"] = rent_type + attributes["startdate"] = startdate + attributes["unittype_gn"] = field_29 + attributes["builtype"] = field_30 + attributes["wchair"] = field_31 + attributes["beds"] = field_32 + attributes["joint"] = field_39 + attributes["startertenancy"] = field_40 + attributes["tenancy"] = field_41 + attributes["tenancyother"] = field_42 + attributes["tenancylength"] = field_43 + attributes["declaration"] = field_45 + + attributes["age1_known"] = field_46 == "R" ? 1 : 0 + attributes["age1"] = field_46 if attributes["age1_known"].zero? && field_46&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age2_known"] = field_52 == "R" ? 1 : 0 + attributes["age2"] = field_52 if attributes["age2_known"].zero? && field_52&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age3_known"] = field_56 == "R" ? 1 : 0 + attributes["age3"] = field_56 if attributes["age3_known"].zero? && field_56&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age4_known"] = field_60 == "R" ? 1 : 0 + attributes["age4"] = field_60 if attributes["age4_known"].zero? && field_60&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age5_known"] = field_64 == "R" ? 1 : 0 + attributes["age5"] = field_64 if attributes["age5_known"].zero? && field_64&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age6_known"] = field_68 == "R" ? 1 : 0 + attributes["age6"] = field_68 if attributes["age6_known"].zero? && field_68&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age7_known"] = field_72 == "R" ? 1 : 0 + attributes["age7"] = field_72 if attributes["age7_known"].zero? && field_72&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age8_known"] = field_76 == "R" ? 1 : 0 + attributes["age8"] = field_76 if attributes["age8_known"].zero? && field_76&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["sex1"] = field_47 + attributes["sex2"] = field_53 + attributes["sex3"] = field_57 + attributes["sex4"] = field_61 + attributes["sex5"] = field_65 + attributes["sex6"] = field_69 + attributes["sex7"] = field_73 + attributes["sex8"] = field_77 + + attributes["ethnic_group"] = ethnic_group_from_ethnic + attributes["ethnic"] = field_48 + attributes["national"] = field_49 + + attributes["relat2"] = field_51 + attributes["relat3"] = field_55 + attributes["relat4"] = field_59 + attributes["relat5"] = field_63 + attributes["relat6"] = field_67 + attributes["relat7"] = field_71 + attributes["relat8"] = field_75 + + attributes["ecstat1"] = field_50 + attributes["ecstat2"] = field_54 + attributes["ecstat3"] = field_58 + attributes["ecstat4"] = field_62 + attributes["ecstat5"] = field_66 + attributes["ecstat6"] = field_70 + attributes["ecstat7"] = field_74 + attributes["ecstat8"] = field_78 + + attributes["details_known_2"] = details_known(2) + attributes["details_known_3"] = details_known(3) + attributes["details_known_4"] = details_known(4) + attributes["details_known_5"] = details_known(5) + attributes["details_known_6"] = details_known(6) + attributes["details_known_7"] = details_known(7) + attributes["details_known_8"] = details_known(8) + + attributes["armedforces"] = field_79 + attributes["leftreg"] = leftreg + attributes["reservist"] = field_81 + + attributes["preg_occ"] = field_82 + + attributes["housingneeds"] = housingneeds + attributes["housingneeds_type"] = housingneeds_type + attributes["housingneeds_other"] = housingneeds_other + + attributes["illness"] = field_89 + + attributes["layear"] = field_100 + attributes["waityear"] = field_101 + attributes["reason"] = field_102 + attributes["reasonother"] = field_103 + attributes["prevten"] = field_104 + attributes["homeless"] = homeless + + attributes["prevloc"] = prevloc + attributes["previous_la_known"] = previous_la_known + attributes["ppcodenk"] = ppcodenk + attributes["ppostcode_full"] = ppostcode_full + + attributes["reasonpref"] = field_110 + attributes["rp_homeless"] = field_111 + attributes["rp_insan_unsat"] = field_112 + attributes["rp_medwel"] = field_113 + attributes["rp_hardship"] = field_114 + attributes["rp_dontknow"] = field_115 + + attributes["cbl"] = cbl + attributes["chr"] = chr + attributes["cap"] = cap + attributes["letting_allocation_unknown"] = letting_allocation_unknown + + attributes["referral"] = field_119 + + attributes["net_income_known"] = net_income_known + attributes["earnings"] = earnings + attributes["incfreq"] = field_121 + attributes["hb"] = field_123 + attributes["benefits"] = field_124 + + attributes["period"] = field_126 + attributes["brent"] = field_128 + attributes["scharge"] = field_129 + attributes["pscharge"] = field_130 + attributes["supcharg"] = field_131 + attributes["tcharge"] = field_132 + attributes["chcharge"] = field_127 + attributes["household_charge"] = field_125 + attributes["hbrentshortfall"] = field_133 + attributes["tshortfall_known"] = tshortfall_known + attributes["tshortfall"] = field_134 + + attributes["hhmemb"] = hhmemb + + attributes["unitletas"] = field_26 + attributes["rsnvac"] = rsnvac + attributes["sheltered"] = field_44 + + attributes["illness_type_1"] = field_98 + attributes["illness_type_2"] = field_92 + attributes["illness_type_3"] = field_95 + attributes["illness_type_4"] = field_90 + attributes["illness_type_5"] = field_91 + attributes["illness_type_6"] = field_93 + attributes["illness_type_7"] = field_94 + attributes["illness_type_8"] = field_97 + attributes["illness_type_9"] = field_96 + attributes["illness_type_10"] = field_99 + + attributes["irproduct_other"] = field_12 + + attributes["offered"] = field_28 + + attributes["propcode"] = field_14 + + attributes["majorrepairs"] = majorrepairs + + attributes["mrcdate"] = mrcdate + + attributes["voiddate"] = voiddate + + attributes["first_time_property_let_as_social_housing"] = first_time_property_let_as_social_housing + + attributes + end + + def postcode_known + if postcode_full.present? + 1 + elsif field_25.present? + 0 + end + end + + def postcode_full + "#{field_23} #{field_24}" if field_23 && field_24 + end + + def owning_organisation + Organisation.find_by_id_on_mulitple_fields(field_1) + end + + def owning_organisation_id + owning_organisation&.id + end + + def managing_organisation + Organisation.find_by_id_on_mulitple_fields(field_2) + end + + def managing_organisation_id + managing_organisation&.id + end + + def renewal + case field_6 + when 1 + 1 + when 2 + 0 + when nil + rsnvac == 14 ? 1 : 0 + else + field_6 + end + end + + def rsnvac + field_27 + end + + def scheme + @scheme ||= Scheme.find_by_id_on_mulitple_fields(field_16) + end + + def location + return if scheme.nil? + + @location ||= scheme.locations.find_by_id_on_mulitple_fields(field_17) + end + + def renttype + case field_5 + when 1, 2, 3, 4 + :social + when 5, 6, 7, 8 + :affordable + when 9, 10, 11, 12 + :intermediate + end + end + + def rent_type + case renttype + when :social + Imports::LettingsLogsImportService::RENT_TYPE[:social_rent] + when :affordable + if field_10 == 1 + Imports::LettingsLogsImportService::RENT_TYPE[:london_affordable_rent] + else + Imports::LettingsLogsImportService::RENT_TYPE[:affordable_rent] + end + when :intermediate + case field_11 + when 1 + Imports::LettingsLogsImportService::RENT_TYPE[:rent_to_buy] + when 2 + Imports::LettingsLogsImportService::RENT_TYPE[:london_living_rent] + when 3 + Imports::LettingsLogsImportService::RENT_TYPE[:other_intermediate_rent_product] + end + end + end + + def startdate + Date.new(field_9 + 2000, field_8, field_7) if field_9.present? && field_8.present? && field_7.present? + rescue Date::Error + Date.new + end + + def ethnic_group_from_ethnic + return nil if field_48.blank? + + case field_48 + when 1, 2, 3, 18 + 0 + when 4, 5, 6, 7 + 1 + when 8, 9, 10, 11, 15 + 2 + when 12, 13, 14 + 3 + when 16, 19 + 4 + when 17 + 17 + end + end + + def details_known(person_n) + send("person_#{person_n}_present?") ? 0 : 1 + end + + def person_2_present? + field_51.present? && field_52.present? && field_53.present? + end + + def person_3_present? + field_55.present? && field_56.present? && field_57.present? + end + + def person_4_present? + field_59.present? && field_60.present? && field_61.present? + end + + def person_5_present? + field_63.present? && field_64.present? && field_65.present? + end + + def person_6_present? + field_67.present? && field_68.present? && field_69.present? + end + + def person_7_present? + field_71.present? && field_72.present? && field_73.present? + end + + def person_8_present? + field_75.present? && field_76.present? && field_77.present? + end + + def leftreg + case field_80 + when 3 + 3 + when 4 + 1 + when 5 + 2 + when 6 + 0 + end + end + + def housingneeds + if field_87 == 1 + 2 + elsif field_88 == 1 + 3 + else + 2 + end + end + + def housingneeds_type + if field_83 == 1 + 0 + elsif field_84 == 1 + 1 + elsif field_85 == 1 + 2 + end + end + + def housingneeds_other + return 1 if field_86 == 1 + end + + def homeless + case field_105 + when 1 + 1 + when 12 + 11 + end + end + + def prevloc + field_109 + end + + def previous_la_known + prevloc.present? ? 1 : 0 + end + + def ppcodenk + case field_106 + when 1 + 1 + when 2 + 0 + end + end + + def ppostcode_full + "#{field_107} #{field_108}".strip.gsub(/\s+/, " ") + end + + def cbl + case field_116 + when 2 + 0 + when 1 + 1 + end + end + + def chr + case field_118 + when 2 + 0 + when 1 + 1 + end + end + + def cap + case field_117 + when 2 + 0 + when 1 + 1 + end + end + + def letting_allocation_unknown + [cbl, chr, cap].all?(0) ? 1 : 0 + end + + def net_income_known + case field_120 + when 1 + 0 + when 2 + 1 + when 3 + 1 + when 4 + 2 + end + end + + def earnings + field_122.round if field_122.present? + end + + def tshortfall_known + field_133 == 1 ? 0 : 1 + end + + def hhmemb + [ + person_2_present?, + person_3_present?, + person_4_present?, + person_5_present?, + person_6_present?, + person_7_present?, + person_8_present?, + ].count(true) + 1 + end + + def majorrepairs + mrcdate.present? ? 1 : 0 + end + + def mrcdate + Date.new(field_38 + 2000, field_37, field_36) if field_38.present? && field_37.present? && field_36.present? + end + + def voiddate + Date.new(field_35 + 2000, field_34, field_33) if field_35.present? && field_34.present? && field_33.present? + end + + def first_time_property_let_as_social_housing + case rsnvac + when 15, 16, 17 + 1 + else + 0 + end + end +end diff --git a/app/services/bulk_upload/sales/row_parser.rb b/app/services/bulk_upload/sales/row_parser.rb deleted file mode 100644 index f7b74dfe1..000000000 --- a/app/services/bulk_upload/sales/row_parser.rb +++ /dev/null @@ -1,176 +0,0 @@ -class BulkUpload::Sales::RowParser - include ActiveModel::Model - include ActiveModel::Attributes - - attribute :field_1, :string - attribute :field_2, :integer - attribute :field_3, :integer - attribute :field_4, :integer - attribute :field_5 - attribute :field_6, :integer - attribute :field_7, :integer - attribute :field_8, :integer - attribute :field_9, :integer - attribute :field_10, :integer - attribute :field_11, :integer - attribute :field_12, :integer - attribute :field_13, :string - attribute :field_14, :string - attribute :field_15, :string - attribute :field_16, :string - attribute :field_17, :string - attribute :field_18, :string - attribute :field_19, :string - attribute :field_20, :integer - attribute :field_21, :integer - attribute :field_22, :integer - attribute :field_23, :integer - attribute :field_24, :integer - attribute :field_25, :integer - attribute :field_26, :integer - attribute :field_27, :integer - attribute :field_28, :integer - attribute :field_29, :integer - attribute :field_30, :integer - attribute :field_31, :integer - attribute :field_32, :integer - attribute :field_33, :integer - attribute :field_34, :integer - attribute :field_35, :integer - attribute :field_36, :integer - attribute :field_37, :integer - attribute :field_38 - attribute :field_39, :integer - attribute :field_40, :string - attribute :field_41, :string - attribute :field_42, :string - attribute :field_43, :integer - attribute :field_44, :integer - attribute :field_45, :integer - attribute :field_46, :integer - attribute :field_47, :integer - attribute :field_48, :integer - attribute :field_49, :integer - attribute :field_50, :integer - attribute :field_51, :integer - attribute :field_52, :integer - attribute :field_53, :string - attribute :field_54, :string - attribute :field_55, :string - attribute :field_56, :integer - attribute :field_57, :integer - attribute :field_58, :integer - attribute :field_59, :integer - attribute :field_60, :integer - attribute :field_61, :integer - attribute :field_62, :integer - attribute :field_63, :integer - attribute :field_64, :integer - attribute :field_65, :integer - attribute :field_66, :integer - attribute :field_67, :integer - attribute :field_68, :integer - attribute :field_69, :integer - attribute :field_70, :integer - attribute :field_71, :integer - attribute :field_72, :integer - attribute :field_73, :integer - attribute :field_74, :decimal - attribute :field_75, :decimal - attribute :field_76, :integer - attribute :field_77, :integer - attribute :field_78, :integer - attribute :field_79, :integer - attribute :field_80, :integer - attribute :field_81, :integer - attribute :field_82, :integer - attribute :field_83, :integer - attribute :field_84, :integer - attribute :field_85, :string - attribute :field_86 - attribute :field_87, :integer - attribute :field_88, :integer - attribute :field_89, :integer - attribute :field_90, :integer - attribute :field_91, :integer - attribute :field_92, :integer - attribute :field_93, :string - attribute :field_94 - attribute :field_95, :integer - attribute :field_96 - attribute :field_97, :integer - attribute :field_98, :integer - attribute :field_99, :string - attribute :field_100, :integer - attribute :field_101, :string - attribute :field_102, :integer - attribute :field_103, :string - attribute :field_104, :integer - attribute :field_105, :integer - attribute :field_106, :integer - attribute :field_107, :integer - attribute :field_108, :integer - attribute :field_109, :integer - attribute :field_110, :integer - attribute :field_111, :integer - attribute :field_112, :integer - attribute :field_113, :integer - attribute :field_114, :integer - attribute :field_115, :integer - attribute :field_116, :integer - attribute :field_117, :integer - attribute :field_118, :integer - attribute :field_119, :integer - attribute :field_120, :integer - attribute :field_121, :integer - attribute :field_122, :integer - attribute :field_123, :integer - attribute :field_124, :integer - attribute :field_125, :integer - - # validates :field_1, presence: true, numericality: { in: (1..12) } - # validates :field_4, numericality: { in: (1..999), allow_blank: true } - # validates :field_4, presence: true, if: :field_4_presence_check - - validate :validate_possible_answers - -# delegate :valid?, to: :native_object -# delegate :errors, to: :native_object - -private - - def native_object - @native_object ||= SalesLog.new(attributes_for_log) - end - - def field_mapping - { - field_117: :buy1livein, - } - end - - def validate_possible_answers - field_mapping.each do |field, attribute| - possible_answers = FormHandler.instance.current_sales_form.questions.find { |q| q.id == attribute.to_s }.answer_options.keys - - unless possible_answers.include?(public_send(field)) - errors.add(field, "Value supplied is not one of the permitted values") - end - end - end - - def attributes_for_log - hash = field_mapping.invert - attributes = {} - - hash.map do |k, v| - attributes[k] = public_send(v) - end - - attributes - end - - # def field_4_presence_check - # [1, 3, 5, 7, 9, 11].include?(field_1) - # end -end diff --git a/app/services/bulk_upload/sales/validator.rb b/app/services/bulk_upload/sales/validator.rb index 43b8c001d..bddbfd13f 100644 --- a/app/services/bulk_upload/sales/validator.rb +++ b/app/services/bulk_upload/sales/validator.rb @@ -1,138 +1,6 @@ class BulkUpload::Sales::Validator include ActiveModel::Validations - QUESTIONS = { - field_1: "What is the purchaser code?", - field_2: "What is the day of the sale completion date? - DD", - field_3: "What is the month of the sale completion date? - MM", - field_4: "What is the year of the sale completion date? - YY", - field_5: "This question has been removed", - field_6: "Was the buyer interviewed for any of the answers you will provide on this log?", - field_7: "Age of Buyer 1", - field_8: "Age of Person 2", - field_9: "Age of Person 3", - field_10: "Age of Person 4", - field_11: "Age of Person 5", - field_12: "Age of Person 6", - field_13: "Gender identity of Buyer 1", - field_14: "Gender identity of Person 2", - field_15: "Gender identity of Person 3", - field_16: "Gender identity of Person 4", - field_17: "Gender identity of Person 5", - field_18: "Gender identity of Person 6", - field_19: "Relationship to Buyer 1 for Person 2", - field_20: "Relationship to Buyer 1 for Person 3", - field_21: "Relationship to Buyer 1 for Person 4", - field_22: "Relationship to Buyer 1 for Person 5", - field_23: "Relationship to Buyer 1 for Person 6", - field_24: "Working situation of Buyer 1", - field_25: "Working situation of Person 2", - field_26: "Working situation of Person 3", - field_27: "Working situation of Person 4", - field_28: "Working situation of Person 5", - field_29: "Working situation of Person 6", - field_30: "What is buyer 1's ethnic group?", - field_31: "What is buyer 1's nationality?", - field_32: "What is buyer 1's gross annual income?", - field_33: "What is buyer 2's gross annual income?", - field_34: "Was buyer 1's income used for a mortgage application?", - field_35: "Was buyer 2's income used for a mortgage application?", - field_36: "What is the total amount the buyers had in savings before they paid any deposit for the property?", - field_37: "Have any of the purchasers previously owned a property?", - field_38: "This question has been removed", - field_39: "What was buyer 1's previous tenure?", - field_40: "What is the local authority of buyer 1's last settled home?", - field_41: "Part 1 of postcode of buyer 1's last settled home", - field_42: "Part 2 of postcode of buyer 1's last settled home", - field_43: "Do you know the postcode of buyer 1's last settled home?", - field_44: "Was the buyer registered with their PRP (HA)?", - field_45: "Was the buyer registered with the local authority?", - field_46: "Was the buyer registered with a Help to Buy agent?", - field_47: "Was the buyer registered with another PRP (HA)?", - field_48: "Does anyone in the household consider themselves to have a disability?", - field_49: "Does anyone in the household use a wheelchair?", - field_50: "How many bedrooms does the property have?", - field_51: "What type of unit is the property?", - field_52: "Which type of bulding is the property?", - field_53: "What is the local authority of the property?", - field_54: "Part 1 of postcode of property", - field_55: "Part 2 of postcode of property", - field_56: "Is the property built or adapted to wheelchair user standards?", - field_57: "What is the type of shared ownership sale?", - field_58: "Is this a resale?", - field_59: "What is the day of the practical completion or handover date?", - field_60: "What is the month of the practical completion or handover date?", - field_61: "What is the day of the exchange of contracts date?", - field_62: "What is the day of the practical completion or handover date?", - field_63: "What is the month of the practical completion or handover date?", - field_64: "What is the year of the practical completion or handover date?", - field_65: "Was the household re-housed under a local authority nominations agreement?", - field_66: "How many bedrooms did the buyer's previous property have?", - field_67: "What was the type of the buyer's previous property?", - field_68: "What was the full purchase price?", - field_69: "What was the initial percentage equity stake purchased?", - field_70: "What is the mortgage amount?", - field_71: "Does this include any extra borrowing?", - field_72: "How much was the cash deposit paid on the property?", - field_73: "How much cash discount was given through Social Homebuy?", - field_74: "What is the basic monthly rent?", - field_75: "What are the total monthly leasehold charges for the property?", - field_76: "What is the type of discounted ownership sale?", - field_77: "What was the full purchase price?", - field_78: "What was the amount of any loan, grant, discount or subsidy given?", - field_79: "What was the percentage discount?", - field_80: "What is the mortgage amount?", - field_81: "Does this include any extra borrowing?", - field_82: "How much was the cash deposit paid on the property?", - field_83: "What are the total monthly leasehold charges for the property?", - field_84: "What is the type of outright sale?", - field_85: "If 'other', what is the 'other' type?", - field_86: "This question has been removed", - field_87: "What is the full purchase price?", - field_88: "What is the mortgage amount?", - field_89: "Does this include any extra borrowing?", - field_90: "How much was the cash deposit paid on the property?", - field_91: "What are the total monthly leasehold charges for the property?", - field_92: "Which organisation owned this property before the sale?", - field_93: "Username", - field_94: "This question has been removed", - field_95: "Has the buyer ever served in the UK Armed Forces and for how long?", - field_96: "This question has been removed", - field_97: "Are any of the buyers a spouse or civil partner of a UK Armed Forces regular who died in service within the last 2 years?", - field_98: "What is the name of the mortgage lender? - Shared ownership", - field_99: "If 'other', what is the name of the mortgage lender?", - field_100: "What is the name of the mortgage lender? - Discounted ownership", - field_101: "If 'other', what is the name of the mortgage lender?", - field_102: "What is the name of the mortgage lender? - Outright sale", - field_103: "If 'other', what is the name of the mortgage lender?", - field_104: "Were the buyers receiving any of these housing-related benefits immediately before buying this property?", - field_105: "What is the length of the mortgage in years? - Shared ownership", - field_106: "What is the length of the mortgage in years? - Discounted ownership", - field_107: "What is the length of the mortgage in years? - Outright sale", - field_108: "How long have the buyers been living in the property before the purchase? - Discounted ownership", - field_109: "Are there more than two joint purchasers of this property?", - field_110: "How long have the buyers been living in the property before the purchase? - Shared ownership", - field_111: "Is this a staircasing transaction?", - field_112: "Data Protection question", - field_113: "Was this purchase made through an ownership scheme?", - field_114: "Is the buyer a company?", - field_115: "Will the buyers live in the property?", - field_116: "Is this a joint purchase?", - field_117: "Will buyer 1 live in the property?", - field_118: "Will buyer 2 live in the property?", - field_119: "Besides the buyers, how many people will live in the property?", - field_120: "What percentage of the property has been bought in this staircasing transaction?", - field_121: "What percentage of the property does the buyer now own in total?", - field_122: "What was the rent type of the buyer's previous property?", - field_123: "Was a mortgage used for the purchase of this property? - Shared ownership", - field_124: "Was a mortgage used for the purchase of this property? - Discounted ownership", - field_125: "Was a mortgage used for the purchase of this property? - Outright sale", - }.freeze - - def self.question_for_field(field) - QUESTIONS[field] - end - attr_reader :bulk_upload, :path validate :validate_file_not_empty @@ -193,7 +61,7 @@ private headers = ("field_1".."field_125").to_a hash = Hash[headers.zip(stripped_row)] - BulkUpload::Sales::RowParser.new(hash) + BulkUpload::Sales::Year2022::RowParser.new(hash) end end diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb new file mode 100644 index 000000000..965c3547a --- /dev/null +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -0,0 +1,308 @@ +class BulkUpload::Sales::Year2022::RowParser + include ActiveModel::Model + include ActiveModel::Attributes + + QUESTIONS = { + field_1: "What is the purchaser code?", + field_2: "What is the day of the sale completion date? - DD", + field_3: "What is the month of the sale completion date? - MM", + field_4: "What is the year of the sale completion date? - YY", + field_5: "This question has been removed", + field_6: "Was the buyer interviewed for any of the answers you will provide on this log?", + field_7: "Age of Buyer 1", + field_8: "Age of Person 2", + field_9: "Age of Person 3", + field_10: "Age of Person 4", + field_11: "Age of Person 5", + field_12: "Age of Person 6", + field_13: "Gender identity of Buyer 1", + field_14: "Gender identity of Person 2", + field_15: "Gender identity of Person 3", + field_16: "Gender identity of Person 4", + field_17: "Gender identity of Person 5", + field_18: "Gender identity of Person 6", + field_19: "Relationship to Buyer 1 for Person 2", + field_20: "Relationship to Buyer 1 for Person 3", + field_21: "Relationship to Buyer 1 for Person 4", + field_22: "Relationship to Buyer 1 for Person 5", + field_23: "Relationship to Buyer 1 for Person 6", + field_24: "Working situation of Buyer 1", + field_25: "Working situation of Person 2", + field_26: "Working situation of Person 3", + field_27: "Working situation of Person 4", + field_28: "Working situation of Person 5", + field_29: "Working situation of Person 6", + field_30: "What is buyer 1's ethnic group?", + field_31: "What is buyer 1's nationality?", + field_32: "What is buyer 1's gross annual income?", + field_33: "What is buyer 2's gross annual income?", + field_34: "Was buyer 1's income used for a mortgage application?", + field_35: "Was buyer 2's income used for a mortgage application?", + field_36: "What is the total amount the buyers had in savings before they paid any deposit for the property?", + field_37: "Have any of the purchasers previously owned a property?", + field_38: "This question has been removed", + field_39: "What was buyer 1's previous tenure?", + field_40: "What is the local authority of buyer 1's last settled home?", + field_41: "Part 1 of postcode of buyer 1's last settled home", + field_42: "Part 2 of postcode of buyer 1's last settled home", + field_43: "Do you know the postcode of buyer 1's last settled home?", + field_44: "Was the buyer registered with their PRP (HA)?", + field_45: "Was the buyer registered with the local authority?", + field_46: "Was the buyer registered with a Help to Buy agent?", + field_47: "Was the buyer registered with another PRP (HA)?", + field_48: "Does anyone in the household consider themselves to have a disability?", + field_49: "Does anyone in the household use a wheelchair?", + field_50: "How many bedrooms does the property have?", + field_51: "What type of unit is the property?", + field_52: "Which type of bulding is the property?", + field_53: "What is the local authority of the property?", + field_54: "Part 1 of postcode of property", + field_55: "Part 2 of postcode of property", + field_56: "Is the property built or adapted to wheelchair user standards?", + field_57: "What is the type of shared ownership sale?", + field_58: "Is this a resale?", + field_59: "What is the day of the practical completion or handover date?", + field_60: "What is the month of the practical completion or handover date?", + field_61: "What is the day of the exchange of contracts date?", + field_62: "What is the day of the practical completion or handover date?", + field_63: "What is the month of the practical completion or handover date?", + field_64: "What is the year of the practical completion or handover date?", + field_65: "Was the household re-housed under a local authority nominations agreement?", + field_66: "How many bedrooms did the buyer's previous property have?", + field_67: "What was the type of the buyer's previous property?", + field_68: "What was the full purchase price?", + field_69: "What was the initial percentage equity stake purchased?", + field_70: "What is the mortgage amount?", + field_71: "Does this include any extra borrowing?", + field_72: "How much was the cash deposit paid on the property?", + field_73: "How much cash discount was given through Social Homebuy?", + field_74: "What is the basic monthly rent?", + field_75: "What are the total monthly leasehold charges for the property?", + field_76: "What is the type of discounted ownership sale?", + field_77: "What was the full purchase price?", + field_78: "What was the amount of any loan, grant, discount or subsidy given?", + field_79: "What was the percentage discount?", + field_80: "What is the mortgage amount?", + field_81: "Does this include any extra borrowing?", + field_82: "How much was the cash deposit paid on the property?", + field_83: "What are the total monthly leasehold charges for the property?", + field_84: "What is the type of outright sale?", + field_85: "If 'other', what is the 'other' type?", + field_86: "This question has been removed", + field_87: "What is the full purchase price?", + field_88: "What is the mortgage amount?", + field_89: "Does this include any extra borrowing?", + field_90: "How much was the cash deposit paid on the property?", + field_91: "What are the total monthly leasehold charges for the property?", + field_92: "Which organisation owned this property before the sale?", + field_93: "Username", + field_94: "This question has been removed", + field_95: "Has the buyer ever served in the UK Armed Forces and for how long?", + field_96: "This question has been removed", + field_97: "Are any of the buyers a spouse or civil partner of a UK Armed Forces regular who died in service within the last 2 years?", + field_98: "What is the name of the mortgage lender? - Shared ownership", + field_99: "If 'other', what is the name of the mortgage lender?", + field_100: "What is the name of the mortgage lender? - Discounted ownership", + field_101: "If 'other', what is the name of the mortgage lender?", + field_102: "What is the name of the mortgage lender? - Outright sale", + field_103: "If 'other', what is the name of the mortgage lender?", + field_104: "Were the buyers receiving any of these housing-related benefits immediately before buying this property?", + field_105: "What is the length of the mortgage in years? - Shared ownership", + field_106: "What is the length of the mortgage in years? - Discounted ownership", + field_107: "What is the length of the mortgage in years? - Outright sale", + field_108: "How long have the buyers been living in the property before the purchase? - Discounted ownership", + field_109: "Are there more than two joint purchasers of this property?", + field_110: "How long have the buyers been living in the property before the purchase? - Shared ownership", + field_111: "Is this a staircasing transaction?", + field_112: "Data Protection question", + field_113: "Was this purchase made through an ownership scheme?", + field_114: "Is the buyer a company?", + field_115: "Will the buyers live in the property?", + field_116: "Is this a joint purchase?", + field_117: "Will buyer 1 live in the property?", + field_118: "Will buyer 2 live in the property?", + field_119: "Besides the buyers, how many people will live in the property?", + field_120: "What percentage of the property has been bought in this staircasing transaction?", + field_121: "What percentage of the property does the buyer now own in total?", + field_122: "What was the rent type of the buyer's previous property?", + field_123: "Was a mortgage used for the purchase of this property? - Shared ownership", + field_124: "Was a mortgage used for the purchase of this property? - Discounted ownership", + field_125: "Was a mortgage used for the purchase of this property? - Outright sale", + }.freeze + + attribute :field_1, :string + attribute :field_2, :integer + attribute :field_3, :integer + attribute :field_4, :integer + attribute :field_5 + attribute :field_6, :integer + attribute :field_7, :integer + attribute :field_8, :integer + attribute :field_9, :integer + attribute :field_10, :integer + attribute :field_11, :integer + attribute :field_12, :integer + attribute :field_13, :string + attribute :field_14, :string + attribute :field_15, :string + attribute :field_16, :string + attribute :field_17, :string + attribute :field_18, :string + attribute :field_19, :string + attribute :field_20, :integer + attribute :field_21, :integer + attribute :field_22, :integer + attribute :field_23, :integer + attribute :field_24, :integer + attribute :field_25, :integer + attribute :field_26, :integer + attribute :field_27, :integer + attribute :field_28, :integer + attribute :field_29, :integer + attribute :field_30, :integer + attribute :field_31, :integer + attribute :field_32, :integer + attribute :field_33, :integer + attribute :field_34, :integer + attribute :field_35, :integer + attribute :field_36, :integer + attribute :field_37, :integer + attribute :field_38 + attribute :field_39, :integer + attribute :field_40, :string + attribute :field_41, :string + attribute :field_42, :string + attribute :field_43, :integer + attribute :field_44, :integer + attribute :field_45, :integer + attribute :field_46, :integer + attribute :field_47, :integer + attribute :field_48, :integer + attribute :field_49, :integer + attribute :field_50, :integer + attribute :field_51, :integer + attribute :field_52, :integer + attribute :field_53, :string + attribute :field_54, :string + attribute :field_55, :string + attribute :field_56, :integer + attribute :field_57, :integer + attribute :field_58, :integer + attribute :field_59, :integer + attribute :field_60, :integer + attribute :field_61, :integer + attribute :field_62, :integer + attribute :field_63, :integer + attribute :field_64, :integer + attribute :field_65, :integer + attribute :field_66, :integer + attribute :field_67, :integer + attribute :field_68, :integer + attribute :field_69, :integer + attribute :field_70, :integer + attribute :field_71, :integer + attribute :field_72, :integer + attribute :field_73, :integer + attribute :field_74, :decimal + attribute :field_75, :decimal + attribute :field_76, :integer + attribute :field_77, :integer + attribute :field_78, :integer + attribute :field_79, :integer + attribute :field_80, :integer + attribute :field_81, :integer + attribute :field_82, :integer + attribute :field_83, :integer + attribute :field_84, :integer + attribute :field_85, :string + attribute :field_86 + attribute :field_87, :integer + attribute :field_88, :integer + attribute :field_89, :integer + attribute :field_90, :integer + attribute :field_91, :integer + attribute :field_92, :integer + attribute :field_93, :string + attribute :field_94 + attribute :field_95, :integer + attribute :field_96 + attribute :field_97, :integer + attribute :field_98, :integer + attribute :field_99, :string + attribute :field_100, :integer + attribute :field_101, :string + attribute :field_102, :integer + attribute :field_103, :string + attribute :field_104, :integer + attribute :field_105, :integer + attribute :field_106, :integer + attribute :field_107, :integer + attribute :field_108, :integer + attribute :field_109, :integer + attribute :field_110, :integer + attribute :field_111, :integer + attribute :field_112, :integer + attribute :field_113, :integer + attribute :field_114, :integer + attribute :field_115, :integer + attribute :field_116, :integer + attribute :field_117, :integer + attribute :field_118, :integer + attribute :field_119, :integer + attribute :field_120, :integer + attribute :field_121, :integer + attribute :field_122, :integer + attribute :field_123, :integer + attribute :field_124, :integer + attribute :field_125, :integer + + # validates :field_1, presence: true, numericality: { in: (1..12) } + # validates :field_4, numericality: { in: (1..999), allow_blank: true } + # validates :field_4, presence: true, if: :field_4_presence_check + + validate :validate_possible_answers + + # delegate :valid?, to: :native_object + # delegate :errors, to: :native_object + + def self.question_for_field(field) + QUESTIONS[field] + end + +private + + def native_object + @native_object ||= SalesLog.new(attributes_for_log) + end + + def field_mapping + { + field_117: :buy1livein, + } + end + + def validate_possible_answers + field_mapping.each do |field, attribute| + possible_answers = FormHandler.instance.current_sales_form.questions.find { |q| q.id == attribute.to_s }.answer_options.keys + + unless possible_answers.include?(public_send(field)) + errors.add(field, "Value supplied is not one of the permitted values") + end + end + end + + def attributes_for_log + hash = field_mapping.invert + attributes = {} + + hash.map do |k, v| + attributes[k] = public_send(v) + end + + attributes + end + + # def field_4_presence_check + # [1, 3, 5, 7, 9, 11].include?(field_1) + # end +end diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb index 4fc402212..0253bcc41 100644 --- a/config/initializers/feature_toggle.rb +++ b/config/initializers/feature_toggle.rb @@ -40,6 +40,12 @@ class FeatureToggle !Rails.env.development? end + def self.force_crossover? + return false if Rails.env.test? + + !Rails.env.production? + end + def self.validate_valid_radio_options? !(Rails.env.production? || Rails.env.staging?) end diff --git a/spec/components/bulk_upload_error_summary_table_component_spec.rb b/spec/components/bulk_upload_error_summary_table_component_spec.rb index a7468d70a..7fb7b65ed 100644 --- a/spec/components/bulk_upload_error_summary_table_component_spec.rb +++ b/spec/components/bulk_upload_error_summary_table_component_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe BulkUploadErrorSummaryTableComponent, type: :component do subject(:component) { described_class.new(bulk_upload:) } - let(:bulk_upload) { create(:bulk_upload) } + let(:bulk_upload) { create(:bulk_upload, :lettings) } before do stub_const("BulkUploadErrorSummaryTableComponent::DISPLAY_THRESHOLD", 0) @@ -52,7 +52,7 @@ RSpec.describe BulkUploadErrorSummaryTableComponent, type: :component do expect(row_1).to eql([ "A", "1", - BulkUpload::Lettings::Validator.question_for_field(error_1.field.to_sym), + bulk_upload.prefix_namespace::RowParser.question_for_field(error_1.field.to_sym), error_1.error, error_1.field, ]) @@ -62,7 +62,7 @@ RSpec.describe BulkUploadErrorSummaryTableComponent, type: :component do expect(row_2).to eql([ "B", "1", - BulkUpload::Lettings::Validator.question_for_field(error_2.field.to_sym), + bulk_upload.prefix_namespace::RowParser.question_for_field(error_2.field.to_sym), error_2.error, error_2.field, ]) @@ -89,7 +89,7 @@ RSpec.describe BulkUploadErrorSummaryTableComponent, type: :component do expect(row_1).to eql([ "A", "2", - BulkUpload::Lettings::Validator.question_for_field(error_1.field.to_sym), + bulk_upload.prefix_namespace::RowParser.question_for_field(error_1.field.to_sym), error_1.error, error_1.field, ]) diff --git a/spec/models/forms/bulk_upload_lettings/year_spec.rb b/spec/models/forms/bulk_upload_lettings/year_spec.rb index 0b0babb30..ebb65e517 100644 --- a/spec/models/forms/bulk_upload_lettings/year_spec.rb +++ b/spec/models/forms/bulk_upload_lettings/year_spec.rb @@ -5,8 +5,8 @@ RSpec.describe Forms::BulkUploadLettings::Year do describe "#options" do it "returns correct years" do - expect(form.options.map(&:id)).to eql([2022, 2021]) - expect(form.options.map(&:name)).to eql(%w[2022/2023 2021/2022]) + expect(form.options.map(&:id)).to eql([2023, 2022]) + expect(form.options.map(&:name)).to eql(%w[2023/2024 2022/2023]) end end end diff --git a/spec/services/bulk_upload/lettings/log_creator_spec.rb b/spec/services/bulk_upload/lettings/log_creator_spec.rb index 39dfd00fe..8b320c35f 100644 --- a/spec/services/bulk_upload/lettings/log_creator_spec.rb +++ b/spec/services/bulk_upload/lettings/log_creator_spec.rb @@ -30,9 +30,9 @@ RSpec.describe BulkUpload::Lettings::LogCreator do let(:log) { LettingsLog.new } before do - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) file.rewind end @@ -61,7 +61,7 @@ RSpec.describe BulkUpload::Lettings::LogCreator do end before do - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) file.rewind end diff --git a/spec/services/bulk_upload/lettings/validator_spec.rb b/spec/services/bulk_upload/lettings/validator_spec.rb index ba48bd9e7..dda7722c6 100644 --- a/spec/services/bulk_upload/lettings/validator_spec.rb +++ b/spec/services/bulk_upload/lettings/validator_spec.rb @@ -82,7 +82,7 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:path) { file.path } before do - file.write(BulkUpload::LogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -122,8 +122,8 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_2) { build(:lettings_log, :completed, created_by: user) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0, overrides: { illness: 100 }).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0, overrides: { illness: 100 }).to_2022_csv_row) file.close end @@ -138,8 +138,8 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_2) { build(:lettings_log, :completed, renttype: 1, created_by: user) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -155,7 +155,7 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_1) { build(:lettings_log, :completed, renttype: 1, created_by: user, owning_organisation: unaffiliated_org) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -169,7 +169,7 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log) { build(:lettings_log, :in_progress, created_by: user, startdate: Time.zone.local(2022, 5, 1)) } before do - file.write(BulkUpload::LogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -188,11 +188,11 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_5) { build(:lettings_log, renttype: 2, created_by: user, builtype: nil, startdate: Time.zone.local(2022, 5, 1)) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -210,11 +210,11 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_5) { build(:lettings_log, renttype: 2, created_by: user, builtype: nil, startdate: Time.zone.local(2022, 5, 1)) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -238,11 +238,11 @@ RSpec.describe BulkUpload::Lettings::Validator do let(:log_5) { build(:lettings_log, renttype: 2, created_by: user, builtype: nil, startdate: Time.zone.local(2022, 5, 1)) } before do - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0).to_2022_csv_row) file.close end @@ -262,11 +262,11 @@ RSpec.describe BulkUpload::Lettings::Validator do before do overrides = { age1: 50, age2: "R", age3: "R", age4: "4", age5: "R", age6: "R", age7: "R", age8: "R" } - file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0, overrides:).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0, overrides:).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0, overrides:).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0, overrides:).to_csv_row) - file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0, overrides:).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0, overrides:).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_2, line_ending: "\r\n", col_offset: 0, overrides:).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_3, line_ending: "\r\n", col_offset: 0, overrides:).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_4, line_ending: "\r\n", col_offset: 0, overrides:).to_2022_csv_row) + file.write(BulkUpload::LogToCsv.new(log: log_5, line_ending: "\r\n", col_offset: 0, overrides:).to_2022_csv_row) file.close end diff --git a/spec/services/bulk_upload/lettings/csv_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb similarity index 67% rename from spec/services/bulk_upload/lettings/csv_parser_spec.rb rename to spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb index 80c8e37dc..d396ad78e 100644 --- a/spec/services/bulk_upload/lettings/csv_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe BulkUpload::Lettings::CsvParser do +RSpec.describe BulkUpload::Lettings::Year2022::CsvParser do subject(:service) { described_class.new(path:) } let(:path) { file_fixture("2022_23_lettings_bulk_upload.csv") } @@ -22,7 +22,7 @@ RSpec.describe BulkUpload::Lettings::CsvParser do let(:log) { build(:lettings_log, :completed) } before do - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) file.rewind end @@ -44,7 +44,7 @@ RSpec.describe BulkUpload::Lettings::CsvParser do before do file.write(bom) - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) file.close end @@ -61,7 +61,7 @@ RSpec.describe BulkUpload::Lettings::CsvParser do before do file.write(invalid_sequence) - file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) file.close end @@ -69,4 +69,29 @@ RSpec.describe BulkUpload::Lettings::CsvParser do expect(service.row_parsers[0].field_12.to_i).to eql(log.age1) end end + + describe "#column_for_field", aggregate_failures: true do + context "when headers present" do + it "returns correct column" do + expect(service.column_for_field("field_1")).to eql("B") + expect(service.column_for_field("field_134")).to eql("EE") + end + end + + context "when no headers" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:lettings_log, :completed) } + + before do + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_1")).to eql("A") + expect(service.column_for_field("field_134")).to eql("ED") + end + end + end end diff --git a/spec/services/bulk_upload/lettings/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb similarity index 99% rename from spec/services/bulk_upload/lettings/row_parser_spec.rb rename to spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index 68990c59b..312a63a3b 100644 --- a/spec/services/bulk_upload/lettings/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe BulkUpload::Lettings::RowParser do +RSpec.describe BulkUpload::Lettings::Year2022::RowParser do subject(:parser) { described_class.new(attributes) } let(:now) { Time.zone.today } diff --git a/spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb new file mode 100644 index 000000000..a8cf227f4 --- /dev/null +++ b/spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb @@ -0,0 +1,181 @@ +require "rails_helper" + +RSpec.describe BulkUpload::Lettings::Year2023::CsvParser do + subject(:service) { described_class.new(path:) } + + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:lettings_log, :completed) } + + context "when parsing csv with headers" do + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::LogToCsv.new(log:).default_2023_field_numbers_row) + file.write(BulkUpload::LogToCsv.new(log:).to_2023_csv_row) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(7) + expect(service.col_offset).to eq(1) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + context "when parsing csv with headers with extra rows" do + before do + file.write("Section\n") + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::LogToCsv.new(log:).default_2023_field_numbers_row) + file.write(BulkUpload::LogToCsv.new(log:).to_2023_csv_row) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(8) + expect(service.col_offset).to eq(1) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + context "when parsing csv with headers in arbitrary order" do + let(:seed) { rand } + + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::LogToCsv.new(log:).default_2023_field_numbers_row(seed:)) + file.write(BulkUpload::LogToCsv.new(log:).to_2023_csv_row(seed:)) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(7) + expect(service.col_offset).to eq(1) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + context "when parsing csv without headers" do + before do + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(0) + expect(service.col_offset).to eq(0) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + context "when parsing with BOM aka byte order mark" do + let(:bom) { "\uFEFF" } + + before do + file.write(bom) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + context "when an invalid byte sequence" do + let(:invalid_sequence) { "\x81" } + + before do + file.write(invalid_sequence) + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_13).to eql(log.tenancycode) + end + end + + describe "#column_for_field", aggregate_failures: true do + context "when with headers using default ordering" do + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::LogToCsv.new(log:).default_2023_field_numbers_row) + file.write(BulkUpload::LogToCsv.new(log:).to_2023_csv_row) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_5")).to eql("B") + expect(service.column_for_field("field_22")).to eql("EL") + end + end + + context "when without headers using default ordering" do + before do + file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_5")).to eql("A") + expect(service.column_for_field("field_22")).to eql("EK") + end + end + + context "when with headers using custom ordering" do + let(:seed) { 123 } + + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::LogToCsv.new(log:).default_2023_field_numbers_row(seed:)) + file.write(BulkUpload::LogToCsv.new(log:).to_2023_csv_row(seed:)) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_5")).to eql("N") + expect(service.column_for_field("field_22")).to eql("O") + expect(service.column_for_field("field_26")).to eql("B") + expect(service.column_for_field("field_25")).to eql("EF") + end + end + end +end diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb new file mode 100644 index 000000000..f658f7b04 --- /dev/null +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -0,0 +1,1282 @@ +require "rails_helper" + +RSpec.describe BulkUpload::Lettings::Year2023::RowParser do + subject(:parser) { described_class.new(attributes) } + + let(:now) { Time.zone.today } + + let(:attributes) { { bulk_upload: } } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: nil) } + let(:user) { create(:user, organisation: owning_org) } + + let(:owning_org) { create(:organisation, :with_old_visible_id) } + let(:managing_org) { create(:organisation, :with_old_visible_id) } + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:location) { create(:location, :with_old_visible_id, scheme:) } + + let(:setup_section_params) do + { + bulk_upload:, + field_1: owning_org.old_visible_id, + field_2: managing_org.old_visible_id, + field_4: "1", + field_5: "2", + field_6: "2", + field_7: now.day.to_s, + field_8: now.month.to_s, + field_9: now.strftime("%g"), + } + end + + before do + create(:organisation_relationship, parent_organisation: owning_org, child_organisation: managing_org) + end + + around do |example| + FormHandler.instance.use_real_forms! + + example.run + + FormHandler.instance.use_fake_forms! + end + + describe "#blank_row?" do + context "when a new object" do + it "returns true" do + expect(parser).to be_blank_row + end + end + + context "when any field is populated" do + before do + parser.field_1 = "1" + end + + it "returns false" do + expect(parser).not_to be_blank_row + end + end + end + + describe "validations" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\", \"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) + + parser.valid? + end + + describe "#valid?" do + context "when the row is blank" do + let(:attributes) { { bulk_upload: } } + + it "returns true" do + expect(parser).to be_valid + end + end + + context "when calling the method multiple times" do + let(:attributes) { { bulk_upload:, field_134: 2 } } + + it "does not add keep adding errors to the pile" do + expect { parser.valid? }.not_to change(parser.errors, :count) + end + end + + context "when valid row" do + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_13: "123", + field_7: now.day.to_s, + field_8: now.month.to_s, + field_9: now.strftime("%g"), + field_23: "EC1N", + field_24: "2TD", + field_1: owning_org.old_visible_id, + field_2: managing_org.old_visible_id, + field_11: "1", + field_6: "2", + field_29: "2", + field_30: "1", + field_31: "1", + field_32: "1", + field_39: "2", + field_40: "1", + field_41: "2", + field_45: "1", + + field_46: "42", + field_52: "41", + field_56: "20", + field_60: "18", + field_64: "16", + field_68: "14", + field_72: "12", + field_76: "20", + + field_47: "F", + field_53: "M", + field_57: "F", + field_61: "M", + field_65: "F", + field_69: "M", + field_73: "F", + field_77: "M", + + field_48: "17", + field_49: "18", + + field_51: "P", + field_55: "C", + field_59: "X", + field_63: "R", + field_67: "C", + field_71: "C", + field_75: "X", + + field_50: "1", + field_54: "2", + field_58: "6", + field_62: "7", + field_66: "8", + field_70: "9", + field_74: "0", + field_78: "10", + + field_79: "1", + field_80: "4", + field_81: "1", + + field_82: "1", + + field_89: "2", + + field_100: "5", + field_101: "2", + field_102: "31", + field_104: "3", + field_105: "12", + + field_106: "1", + field_107: "EC1N", + field_108: "2TD", + + field_110: "1", + field_111: "1", + field_112: "", + field_113: "1", + field_114: "", + field_115: "", + + field_116: "1", + field_117: "2", + field_118: "2", + + field_119: "2", + + field_120: "1", + field_122: "2000", + field_121: "2", + field_123: "1", + field_124: "1", + + field_126: "4", + field_128: "1234.56", + field_129: "43.32", + field_130: "13.14", + field_131: "101.11", + field_132: "1500.19", + field_133: "1", + field_134: "234.56", + + field_27: "15", + field_28: "0", + field_33: now.day.to_s, + field_34: now.month.to_s, + field_35: now.strftime("%g"), + + field_4: "1", + } + end + + it "returns true" do + expect(parser).to be_valid + end + + it "instantiates a log with everything completed", aggregate_failures: true do + questions = parser.send(:questions).reject do |q| + parser.send(:log).optional_fields.include?(q.id) || q.completed?(parser.send(:log)) + end + + expect(questions.map(&:id).size).to eq(0) + expect(questions.map(&:id)).to eql([]) + end + end + end + + context "when setup section not complete" do + let(:attributes) { { bulk_upload:, field_13: "123" } } + + it "has errors on setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute) + + expect(errors).to eql(%i[field_4 field_5 field_7 field_8 field_9 field_1 field_2]) + end + end + + describe "#field_5" do + context "when null" do + let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } } + + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end + + context "when incorrect data type" do + let(:attributes) { { bulk_upload:, field_5: "foo" } } + + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end + + context "when unpermitted value" do + let(:attributes) { { bulk_upload:, field_5: "101" } } + + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end + + context "when valid" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "1" } } + + it "does not return any errors" do + expect(parser.errors[:field_5]).to be_blank + end + end + + context "when bulk upload is for general needs" do + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "1" } } + + it "is permitted" do + expect(parser.errors[:field_5]).to be_blank + end + end + + context "when supported housing option selected" do + let(:attributes) { { bulk_upload:, field_5: "2", field_4: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_5]).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:) } + + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "2" } } + + it "is not permitted" do + expect(parser.errors[:field_5]).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_5: "2", field_4: "2" } } + + it "is permitted" do + expect(parser.errors[:field_5]).to be_blank + end + end + end + end + + describe "#field_16" do + context "when nullable not permitted" do + let(:attributes) { { bulk_upload:, field_5: "2", field_16: nil } } + + it "cannot be nulled" do + expect(parser.errors[:field_16]).to be_present + end + end + + context "when nullable permitted" do + let(:attributes) { { bulk_upload:, field_5: "1", field_16: nil } } + + it "can be nulled" do + expect(parser.errors[:field_15]).to be_blank + end + end + + context "when matching scheme cannot be found" do + let(:attributes) { { bulk_upload:, field_5: "1", field_16: "123" } } + + it "returns an error" do + expect(parser.errors[:field_16]).to be_present + end + end + + context "when scheme belongs to someone else" do + let(:other_scheme) { create(:scheme, :with_old_visible_id) } + let(:attributes) { { bulk_upload:, field_5: "1", field_16: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } } + + it "returns an error" do + expect(parser.errors[:field_16]).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents") + end + end + + context "when scheme belongs to owning org" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_1: owning_org.old_visible_id } } + + it "does not return an error" do + expect(parser.errors[:field_16]).to be_blank + end + end + + context "when scheme belongs to managing org" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) } + let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_2: managing_org.old_visible_id } } + + it "does not return an error" do + expect(parser.errors[:field_16]).to be_blank + end + end + end + + describe "#field_17" do + context "when location does not exist" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_16: scheme.old_visible_id, + field_17: "dontexist", + field_1: owning_org.old_visible_id, + } + end + + it "returns an error" do + expect(parser.errors[:field_17]).to be_present + end + end + + context "when location exists" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_16: scheme.old_visible_id, + field_17: location.old_visible_id, + field_1: owning_org.old_visible_id, + } + end + + it "does not return an error" do + expect(parser.errors[:field_17]).to be_blank + end + end + + context "when location exists but not related" do + let(:location) { create(:scheme, :with_old_visible_id) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_16: scheme.old_visible_id, + field_17: location.old_visible_id, + field_1: owning_org.old_visible_id, + } + end + + it "returns an error" do + expect(parser.errors[:field_17]).to be_present + end + end + end + + describe "#field_102" do # leaving reason + context "when field_6 is 1 meaning it is a renewal" do + context "when field_102 is 40" do + let(:attributes) { { bulk_upload:, field_102: "40", field_6: "1" } } + + it "is permitted" do + expect(parser.errors[:field_102]).to be_blank + end + end + + context "when field_102 is 42" do + let(:attributes) { { bulk_upload:, field_102: "42", field_6: "1" } } + + it "is permitted" do + expect(parser.errors[:field_102]).to be_blank + end + end + + context "when field_102 is not 40 or 42" do + let(:attributes) { { bulk_upload:, field_102: "1", field_6: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_102]).to be_present + end + end + end + end + + describe "#field_83, #field_84, #field_85" do + context "when more than one item selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_84: "1" } } + + 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 + + describe "#field_87" do + context "when 1 and another disability field selected" do + let(:attributes) { { bulk_upload:, field_87: "1", field_86: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_87]).to be_present + end + end + end + + describe "#field_88" do + context "when 1 and another disability field selected" do + let(:attributes) { { bulk_upload:, field_88: "1", field_86: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_88]).to be_present + end + end + end + + describe "#field_87, #field_88" do + context "when both 1" do + let(:attributes) { { bulk_upload:, field_87: "1", field_88: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_87]).to be_present + expect(parser.errors[:field_88]).to be_present + end + end + end + + describe "#field_119" do # referral + context "when 3 ie PRP nominated by LA and owning org is LA" do + let(:attributes) { { bulk_upload:, field_119: "3", field_1: owning_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_119]).to be_present + end + end + + context "when 4 ie referred by LA and is general needs and owning org is LA" do + let(:attributes) { { bulk_upload:, field_119: "4", field_1: owning_org.old_visible_id.to_s, field_4: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_119]).to be_present + end + end + + context "when 4 ie referred by LA and is general needs and owning org is PRP" do + let(:owning_org) { create(:organisation, :prp, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_119: "4", field_1: owning_org.old_visible_id.to_s } } + + it "is permitted" do + expect(parser.errors[:field_119]).to be_blank + end + end + + context "when 4 ie referred by LA and is not general needs" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } + let(:attributes) { { bulk_upload:, field_119: "4", field_4: "2" } } + + it "is permitted" do + expect(parser.errors[:field_119]).to be_blank + end + end + end + + describe "fields 7, 8, 9 => startdate" do + context "when any one of these fields is blank" do + let(:attributes) { { bulk_upload:, field_5: "1", field_7: nil, field_8: nil, field_9: nil } } + + it "returns an error" do + parser.valid? + + expect(parser.errors[:field_7]).to be_present + expect(parser.errors[:field_8]).to be_present + expect(parser.errors[:field_9]).to be_present + end + end + + context "when field_9 is 4 digits instead of 2" do + let(:attributes) { { bulk_upload:, field_9: "2022" } } + + it "returns an error" do + parser.valid? + + expect(parser.errors[:field_9]).to include("Tenancy start year must be 2 digits") + end + end + + context "when invalid date given" do + let(:attributes) { { bulk_upload:, field_5: "1", field_7: "a", field_8: "12", field_9: "22" } } + + it "does not raise an error" do + expect { parser.valid? }.not_to raise_error + end + end + + context "when inside of collection year" do + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "10", field_9: "22" } } + + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } + + it "does not return errors" do + parser.valid? + + expect(parser.errors[:field_7]).not_to be_present + expect(parser.errors[:field_8]).not_to be_present + expect(parser.errors[:field_9]).not_to be_present + end + end + + context "when outside of collection year" do + around do |example| + Timecop.freeze(Date.new(2022, 4, 2)) do + example.run + end + end + + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "1", field_9: "22" } } + + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } + + it "returns errors" do + parser.valid? + + expect(parser.errors[:field_7]).to be_present + expect(parser.errors[:field_8]).to be_present + expect(parser.errors[:field_9]).to be_present + end + end + end + + describe "#field_1" do # owning org + context "when cannot find owning org" do + let(:attributes) { { bulk_upload:, field_1: "donotexist" } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["The owning organisation code is incorrect"]) + end + end + + context "when org is not stock owning" do + let(:owning_org) { create(:organisation, :with_old_visible_id, :does_not_own_stock) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["The owning organisation code provided is for an organisation that does not own stock"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + + context "when not affiliated with owning org" do + let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_1: unaffiliated_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["You do not have permission to add logs for this owning organisation"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + end + + describe "#field_2" do # managing org + context "when cannot find managing org" do + let(:attributes) { { bulk_upload:, field_2: "donotexist" } } + + it "is not permitted" do + expect(parser.errors[:field_2]).to eql(["The managing organisation code is incorrect"]) + end + end + + context "when not affiliated with managing org" do + let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_2: unaffiliated_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_2]).to eql(["This managing organisation does not have a relationship with the owning organisation"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + end + + describe "#field_4" do + context "when blank" do + let(:attributes) { { bulk_upload:, field_4: nil, field_13: "123" } } + + it "is reported as a setup error" do + errors = parser.errors.select { |e| e.options[:category] == :setup } + error = errors.find { |e| e.attribute == :field_4 } + + expect(error).to be_present + expect(error.type).to eql("You must answer needs type") + end + end + end + + describe "#field_6" do + context "when an unpermitted value" do + let(:attributes) { { bulk_upload:, field_6: "3" } } + + it "has errors on the field" do + expect(parser.errors[:field_6]).to be_present + end + end + end + + describe "#field_30" do + context "when null" do + let(:attributes) { setup_section_params.merge({ field_30: nil }) } + + it "returns an error" do + expect(parser.errors[:field_30]).to be_present + end + + it "populates with correct error message" do + expect(parser.errors[:field_30]).to eql(["You must answer type of building"]) + end + end + + context "when unpermitted values" do + let(:attributes) { setup_section_params.merge({ field_30: "4" }) } + + it "returns an error" do + expect(parser.errors[:field_30]).to be_present + end + end + end + end + + describe "#log" do + [ + %w[age1_known age1 field_46], + %w[age2_known age2 field_52], + %w[age3_known age3 field_56], + %w[age4_known age4 field_60], + %w[age5_known age5 field_64], + %w[age6_known age6 field_68], + %w[age7_known age7 field_72], + %w[age8_known age8 field_76], + ].each do |known, age, field| + describe "##{known} and ##{age}" do + context "when #{field} is R" do + let(:attributes) { { bulk_upload:, field.to_s => "R" } } + + it "sets ##{known} 1" do + expect(parser.log.public_send(known)).to be(1) + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end + end + + context "when #{field} is a number" do + let(:attributes) { { bulk_upload:, field.to_s => "50" } } + + it "sets ##{known} to 0" do + expect(parser.log.public_send(known)).to be(0) + end + + it "sets ##{age} to given age" do + expect(parser.log.public_send(age)).to be(50) + end + end + end + end + + describe "#location" do + context "when lookup is via new core id" do + let(:attributes) { { bulk_upload:, field_16: scheme.old_visible_id, field_17: location.id, field_1: owning_org } } + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end + end + + describe "#scheme" do + context "when lookup is via id prefixed with S" do + let(:attributes) { { bulk_upload:, field_16: "S#{scheme.id}", field_1: owning_org } } + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end + end + + describe "#owning_organisation" do + context "when lookup is via id prefixed with ORG" do + let(:attributes) { { bulk_upload:, field_1: "ORG#{owning_org.id}" } } + + it "assigns the correct org" do + expect(parser.log.owning_organisation).to eql(owning_org) + end + end + end + + describe "#managing_organisation" do + context "when lookup is via id prefixed with ORG" do + let(:attributes) { { bulk_upload:, field_2: "ORG#{managing_org.id}" } } + + it "assigns the correct org" do + expect(parser.log.managing_organisation).to eql(managing_org) + end + end + end + + describe "#cbl" do + context "when field_116 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_116: 1 } } + + it "sets value to 1" do + expect(parser.log.cbl).to be(1) + end + end + + context "when field_116 is no ie 2" do + let(:attributes) { { bulk_upload:, field_116: 2 } } + + it "sets value to 0" do + expect(parser.log.cbl).to be(0) + end + end + end + + describe "#chr" do + context "when field_118 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_118: 1 } } + + it "sets value to 1" do + expect(parser.log.chr).to be(1) + end + end + + context "when field_118 is no ie 2" do + let(:attributes) { { bulk_upload:, field_118: 2 } } + + it "sets value to 0" do + expect(parser.log.chr).to be(0) + end + end + end + + describe "#cap" do + context "when field_117 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_117: 1 } } + + it "sets value to 1" do + expect(parser.log.cap).to be(1) + end + end + + context "when field_117 is no ie 2" do + let(:attributes) { { bulk_upload:, field_117: 2 } } + + it "sets value to 0" do + expect(parser.log.cap).to be(0) + end + end + end + + describe "#letting_allocation_unknown" do + context "when field_116, 117, 118 are no ie 2" do + let(:attributes) { { bulk_upload:, field_116: 2, field_117: 2, field_118: 2 } } + + it "sets value to 1" do + expect(parser.log.letting_allocation_unknown).to be(1) + end + end + + context "when any one of field_116, 117, 118 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_116: 1 } } + + it "sets value to 0" do + expect(parser.log.letting_allocation_unknown).to be(0) + end + end + end + + describe "#renewal" do + context "when field_6 is no ie 2" do + let(:attributes) { { bulk_upload:, field_6: 2 } } + + it "sets value to 0" do + expect(parser.log.renewal).to eq(0) + end + end + + context "when field_6 is null but rsnvac/field_27 is 14" do + let(:attributes) { { bulk_upload:, field_6: "", field_27: "14" } } + + it "sets renewal to 1" do + expect(parser.log.renewal).to eq(1) + end + end + end + + describe "#sexN fields" do + let(:attributes) do + { + bulk_upload:, + field_47: "F", + field_53: "M", + field_57: "X", + field_61: "R", + field_65: "F", + field_69: "M", + field_73: "X", + field_77: "R", + } + end + + it "sets value from correct mapping" do + expect(parser.log.sex1).to eql("F") + expect(parser.log.sex2).to eql("M") + expect(parser.log.sex3).to eql("X") + expect(parser.log.sex4).to eql("R") + expect(parser.log.sex5).to eql("F") + expect(parser.log.sex6).to eql("M") + expect(parser.log.sex7).to eql("X") + expect(parser.log.sex8).to eql("R") + end + end + + describe "#ecstatN fields" do + let(:attributes) do + { + bulk_upload:, + field_50: "1", + field_54: "2", + field_58: "6", + field_62: "7", + field_66: "8", + field_70: "9", + field_74: "0", + field_78: "10", + } + end + + it "sets value from correct mapping", aggregate_failures: true do + expect(parser.log.ecstat1).to eq(1) + expect(parser.log.ecstat2).to eq(2) + expect(parser.log.ecstat3).to eq(6) + expect(parser.log.ecstat4).to eq(7) + expect(parser.log.ecstat5).to eq(8) + expect(parser.log.ecstat6).to eq(9) + expect(parser.log.ecstat7).to eq(0) + expect(parser.log.ecstat8).to eq(10) + end + end + + describe "#relatN fields" do + let(:attributes) do + { + bulk_upload:, + field_51: "P", + field_55: "C", + field_59: "X", + field_63: "R", + field_67: "P", + field_71: "C", + field_75: "X", + } + end + + it "sets value from correct mapping", aggregate_failures: true do + expect(parser.log.relat2).to eq("P") + expect(parser.log.relat3).to eq("C") + expect(parser.log.relat4).to eq("X") + expect(parser.log.relat5).to eq("R") + expect(parser.log.relat6).to eq("P") + expect(parser.log.relat7).to eq("C") + expect(parser.log.relat8).to eq("X") + end + end + + describe "#net_income_known" do + let(:attributes) { { bulk_upload:, field_120: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.net_income_known).to eq(0) + end + end + + describe "#unitletas" do + let(:attributes) { { bulk_upload:, field_26: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.unitletas).to eq(1) + end + end + + describe "#rsnvac" do + let(:attributes) { { bulk_upload:, field_27: "5" } } + + it "sets value from correct mapping" do + expect(parser.log.rsnvac).to eq(5) + end + end + + describe "#sheltered" do + let(:attributes) { { bulk_upload:, field_44: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.sheltered).to eq(1) + end + end + + describe "illness fields" do + mapping = [ + { attribute: :illness_type_1, field: :field_98 }, + { attribute: :illness_type_2, field: :field_92 }, + { attribute: :illness_type_3, field: :field_95 }, + { attribute: :illness_type_4, field: :field_90 }, + { attribute: :illness_type_5, field: :field_91 }, + { attribute: :illness_type_6, field: :field_93 }, + { attribute: :illness_type_7, field: :field_94 }, + { attribute: :illness_type_8, field: :field_97 }, + { attribute: :illness_type_9, field: :field_96 }, + { attribute: :illness_type_10, field: :field_99 }, + ] + + mapping.each do |hash| + describe "##{hash[:attribute]}" do + context "when yes" do + let(:attributes) { { bulk_upload:, hash[:field] => "1" } } + + it "sets value from correct mapping" do + expect(parser.log.public_send(hash[:attribute])).to eq(1) + end + end + + context "when no" do + let(:attributes) { { bulk_upload:, hash[:field] => "" } } + + it "sets value from correct mapping" do + expect(parser.log.public_send(hash[:attribute])).to be_nil + end + end + end + end + end + + describe "#irproduct_other" do + let(:attributes) { { bulk_upload:, field_12: "some other product" } } + + it "sets value to given free text string" do + expect(parser.log.irproduct_other).to eql("some other product") + end + end + + describe "#tenancyother" do + let(:attributes) { { bulk_upload:, field_42: "some other tenancy" } } + + it "sets value to given free text string" do + expect(parser.log.tenancyother).to eql("some other tenancy") + end + end + + describe "#tenancylength" do + let(:attributes) { { bulk_upload:, field_43: "2" } } + + it "sets value to given free text string" do + expect(parser.log.tenancylength).to eq(2) + end + end + + describe "#earnings" do + let(:attributes) { { bulk_upload:, field_122: "104.50" } } + + it "rounds to the nearest whole pound" do + expect(parser.log.earnings).to eq(105) + end + end + + describe "#reasonother" do + let(:attributes) { { bulk_upload:, field_103: "some other reason" } } + + it "sets value to given free text string" do + expect(parser.log.reasonother).to eql("some other reason") + end + end + + describe "#ppcodenk" do + let(:attributes) { { bulk_upload:, field_106: "2" } } + + it "sets correct value from mapping" do + expect(parser.log.ppcodenk).to eq(0) + end + end + + describe "#household_charge" do + let(:attributes) { { bulk_upload:, field_125: "1" } } + + it "sets correct value from mapping" do + expect(parser.log.household_charge).to eq(1) + end + end + + describe "#chcharge" do + let(:attributes) { { bulk_upload:, field_127: "123.45" } } + + it "sets value given" do + expect(parser.log.chcharge).to eq(123.45) + end + end + + describe "#tcharge" do + let(:attributes) { { bulk_upload:, field_132: "123.45" } } + + it "sets value given" do + expect(parser.log.tcharge).to eq(123.45) + end + end + + describe "#supcharg" do + let(:attributes) { { bulk_upload:, field_131: "123.45" } } + + it "sets value given" do + expect(parser.log.supcharg).to eq(123.45) + end + end + + describe "#pscharge" do + let(:attributes) { { bulk_upload:, field_130: "123.45" } } + + it "sets value given" do + expect(parser.log.pscharge).to eq(123.45) + end + end + + describe "#scharge" do + let(:attributes) { { bulk_upload:, field_129: "123.45" } } + + it "sets value given" do + expect(parser.log.scharge).to eq(123.45) + end + end + + describe "#offered" do + let(:attributes) { { bulk_upload:, field_28: "3" } } + + it "sets value given" do + expect(parser.log.offered).to eq(3) + end + end + + describe "#propcode" do + let(:attributes) { { bulk_upload:, field_14: "abc123" } } + + it "sets value given" do + expect(parser.log.propcode).to eq("abc123") + end + end + + describe "#mrcdate" do + let(:attributes) { { bulk_upload:, field_36: "13", field_37: "12", field_38: "22" } } + + it "sets value given" do + expect(parser.log.mrcdate).to eq(Date.new(2022, 12, 13)) + end + end + + describe "#majorrepairs" do + context "when mrcdate given" do + let(:attributes) { { bulk_upload:, field_36: "13", field_37: "12", field_38: "22" } } + + it "sets #majorrepairs to 1" do + expect(parser.log.majorrepairs).to eq(1) + end + end + + context "when mrcdate not given" do + let(:attributes) { { bulk_upload:, field_36: "", field_37: "", field_38: "" } } + + it "sets #majorrepairs to 0" do + expect(parser.log.majorrepairs).to eq(0) + end + end + end + + describe "#voiddate" do + let(:attributes) { { bulk_upload:, field_33: "13", field_34: "12", field_35: "22" } } + + it "sets value given" do + expect(parser.log.voiddate).to eq(Date.new(2022, 12, 13)) + end + end + + describe "#startdate" do + let(:attributes) { { bulk_upload:, field_7: now.day.to_s, field_8: now.month.to_s, field_9: now.strftime("%g") } } + + it "sets value given" do + expect(parser.log.startdate).to eq(now) + end + end + + describe "#postcode_full" do + let(:attributes) { { bulk_upload:, field_23: " EC1N ", field_24: " 2TD " } } + + it "strips whitespace" do + expect(parser.log.postcode_full).to eql("EC1N 2TD") + end + end + + describe "#la" do + let(:attributes) { { bulk_upload:, field_25: "E07000223" } } + + it "sets to given value" do + expect(parser.log.la).to eql("E07000223") + end + end + + describe "#prevloc" do + let(:attributes) { { bulk_upload:, field_109: "E07000223" } } + + it "sets to given value" do + expect(parser.log.prevloc).to eql("E07000223") + end + end + + describe "#previous_la_known" do + context "when known" do + let(:attributes) { { bulk_upload:, field_109: "E07000223" } } + + it "sets to 1" do + expect(parser.log.previous_la_known).to eq(1) + end + end + + context "when not known" do + let(:attributes) { { bulk_upload:, field_109: "" } } + + it "sets to 0" do + expect(parser.log.previous_la_known).to eq(0) + end + end + end + + describe "#first_time_property_let_as_social_housing" do + context "when field_27 is 15, 16, or 17" do + let(:attributes) { { bulk_upload:, field_27: %w[15 16 17].sample } } + + it "sets to 1" do + expect(parser.log.first_time_property_let_as_social_housing).to eq(1) + end + end + + context "when field_27 is not 15, 16, or 17" do + let(:attributes) { { bulk_upload:, field_27: "1" } } + + it "sets to 0" do + expect(parser.log.first_time_property_let_as_social_housing).to eq(0) + end + end + end + + describe "#housingneeds" do + context "when no disabled needs" do + let(:attributes) { { bulk_upload:, field_87: "1" } } + + it "sets to 2" do + expect(parser.log.housingneeds).to eq(2) + end + end + + context "when dont know about disabled needs" do + let(:attributes) { { bulk_upload:, field_88: "1" } } + + it "sets to 3" do + expect(parser.log.housingneeds).to eq(3) + end + end + end + + describe "#housingneeds_type" do + context "when field_83 is 1" do + let(:attributes) { { bulk_upload:, field_83: "1" } } + + it "set to 0" do + expect(parser.log.housingneeds_type).to eq(0) + end + end + + context "when field_84 is 1" do + let(:attributes) { { bulk_upload:, field_84: "1" } } + + it "set to 1" do + expect(parser.log.housingneeds_type).to eq(1) + end + end + + context "when field_85 is 1" do + let(:attributes) { { bulk_upload:, field_85: "1" } } + + it "set to 2" do + expect(parser.log.housingneeds_type).to eq(2) + end + end + end + + describe "#housingneeds_other" do + context "when field_58 is 1" do + let(:attributes) { { bulk_upload:, field_86: "1" } } + + it "sets to 1" do + expect(parser.log.housingneeds_other).to eq(1) + end + end + end + end + + describe "#start_date" do + context "when year of 9 is passed to represent 2009" do + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "1", field_9: "9" } } + + it "uses the year 2009" do + expect(parser.send(:start_date)).to eql(Date.new(2009, 1, 1)) + end + end + end +end diff --git a/spec/services/bulk_upload/sales/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb similarity index 87% rename from spec/services/bulk_upload/sales/row_parser_spec.rb rename to spec/services/bulk_upload/sales/year2022/row_parser_spec.rb index 38f02fabb..e27355dc4 100644 --- a/spec/services/bulk_upload/sales/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe BulkUpload::Sales::RowParser do +RSpec.describe BulkUpload::Sales::Year2022::RowParser do subject(:parser) { described_class.new(attributes) } describe "validations" do diff --git a/spec/support/bulk_upload/log_to_csv.rb b/spec/support/bulk_upload/log_to_csv.rb index e15c9173b..86167376d 100644 --- a/spec/support/bulk_upload/log_to_csv.rb +++ b/spec/support/bulk_upload/log_to_csv.rb @@ -8,9 +8,49 @@ class BulkUpload::LogToCsv @overrides = overrides end - def to_csv_row + def row_prefix + [nil] * col_offset + end + + def to_2022_csv_row + (row_prefix + to_2022_row).flatten.join(",") + line_ending + end + + def to_2023_csv_row(seed: nil) + if seed + row = to_2023_row.shuffle(random: Random.new(seed)) + (row_prefix + row).flatten.join(",") + line_ending + else + (row_prefix + to_2023_row).flatten.join(",") + line_ending + end + end + + def to_2023_row + to_2022_row + [ + log.needstype, + log.location&.id, + nil, # uprn, + nil, # address_line_1, + nil, # address_line_2, + nil, # town_or_city, + nil, # county, + ] + end + + def default_2023_field_numbers_row(seed: nil) + if seed + ["Bulk upload field number"] + default_2023_field_numbers.shuffle(random: Random.new(seed)) + else + ["Bulk upload field number"] + default_2023_field_numbers + end.flatten.join(",") + line_ending + end + + def default_2023_field_numbers + [5, nil, nil, 15, 16, nil, 13, 40, 41, 42, 43, 46, 52, 56, 60, 64, 68, 72, 76, 47, 53, 57, 61, 65, 69, 73, 77, 51, 55, 59, 63, 67, 71, 75, 50, 54, 58, 62, 66, 70, 74, 78, 48, 49, 79, 81, 82, 123, 124, 122, 120, 102, 103, nil, 83, 84, 85, 86, 87, 88, 104, 109, 107, 108, 106, 100, 101, 105, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 126, 128, 129, 130, 131, 132, 127, 125, 133, 134, 33, 34, 35, 36, 37, 38, nil, 7, 8, 9, 28, 14, 32, 29, 30, 31, 26, 27, 25, 23, 24, nil, 1, 3, 2, 80, nil, 121, 44, 89, 98, 92, 95, 90, 91, 93, 94, 97, 96, 99, 10, 11, 12, 45, 39, 6, 4, 17, 18, 19, 20, 21, 22] + end + + def to_2022_row [ - [nil] * col_offset, # 0 log.renttype, # 1 nil, nil, @@ -155,10 +195,11 @@ class BulkUpload::LogToCsv log.declaration, log.joint, renewal, - line_ending, - ].flatten.join(",") + ] end +private + def renewal checkbox_value(log.renewal) end