From f6a26d2747902f7005222d98eb01469977ae1551 Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Thu, 30 Mar 2023 18:10:53 +0100 Subject: [PATCH] feat: add duplicate log check for 23/24 --- .../lettings/year2023/row_parser.rb | 45 +++++++++++++++++++ .../lettings/year2023/row_parser_spec.rb | 33 ++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 24b42a608..12a2b3de9 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -311,6 +311,7 @@ class BulkUpload::Lettings::Year2023::RowParser validate :validate_no_disabled_needs_conjunction, on: :after_log validate :validate_dont_know_disabled_needs_conjunction, on: :after_log validate :validate_no_and_dont_know_disabled_needs_conjunction, on: :after_log + validate :validate_if_log_already_exists, on: :after_log validate :validate_owning_org_data_given, on: :after_log validate :validate_owning_org_exists, on: :after_log @@ -397,6 +398,10 @@ class BulkUpload::Lettings::Year2023::RowParser field_14 end + def log_already_exists? + LettingsLog.exists?(duplicity_check_fields.index_with { |field| log.public_send(field) }) + end + private def validate_declaration_acceptance @@ -447,6 +452,26 @@ private end end + def duplicity_check_fields + %w[ + startdate + age1 + sex1 + ecstat1 + owning_organisation + tcharge + propcode + postcode_full + location + ] + end + + 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? @@ -678,6 +703,26 @@ private log.form.setup_sections[0].subsections[0].questions.include?(question) end + def validate_if_log_already_exists + if log_already_exists? + error_message = "This is a duplicate log" + + errors.add(:field_1, error_message) # owning_organisation + errors.add(:field_7, error_message) # startdate + errors.add(:field_8, error_message) # startdate + errors.add(:field_9, error_message) # startdate + errors.add(:field_14, error_message) # propcode + errors.add(:field_17, error_message) # location + errors.add(:field_23, error_message) # postcode_full + errors.add(:field_24, error_message) # postcode_full + errors.add(:field_25, error_message) # postcode_full + errors.add(:field_46, error_message) # age1 + errors.add(:field_47, error_message) # sex1 + errors.add(:field_50, error_message) # ecstat1 + errors.add(:field_132, error_message) # tcharge + end + end + def field_mapping_for_errors { lettype: [:field_5], diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index 89abce90e..9414939f4 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -236,6 +236,39 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do expect(questions.map(&:id).size).to eq(0) expect(questions.map(&:id)).to eql([]) end + + context "when the log already exists in the db" do + before do + parser.log.save! + end + + it "is not a valid row" do + expect(parser).not_to be_valid + end + + it "adds an error to all (and only) the fields used to determine duplicity" do + parser.valid? + + error_message = "This is a duplicate log" + + expected_errors = { + field_1: [error_message], # owning_organisation + field_7: [error_message], # startdate + field_8: [error_message], # startdate + field_9: [error_message], # startdate + field_14: [error_message], # propcode + field_17: [error_message], # location + field_23: [error_message], # postcode_full + field_24: [error_message], # postcode_full + field_25: [error_message], # postcode_full + field_46: [error_message], # age1 + field_47: [error_message], # sex1 + field_50: [error_message], # ecstat1 + field_132: [error_message], # tcharge + } + expect(parser.errors.as_json).to eq(expected_errors) + end + end end describe "#validate_nulls" do