From 159d57d56e8aec2905807111acd590dcca5b41a8 Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Mon, 6 Feb 2023 12:02:16 +0000 Subject: [PATCH] feat: first pass at bulk upload duplicate log detection --- app/services/bulk_upload/lettings/validator.rb | 12 ++++++++++++ .../bulk_upload/lettings/validator_spec.rb | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 6f37c0f3a..d2a75725a 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -176,6 +176,7 @@ class BulkUpload::Lettings::Validator def create_logs? return false if any_setup_sections_incomplete? return false if over_column_error_threshold? + return false if duplicate_log_already_exists? row_parsers.all? { |row_parser| row_parser.log.valid? } end @@ -203,6 +204,17 @@ private end end + def duplicate_log_already_exists? + fields = ["lettype"] + + fields.any? do |field| + # binding.pry + count = row_parsers.count { |row_parser| LettingsLog.where("lettype": 7).present? } + + count > 0 + end + end + def csv_parser @csv_parser ||= BulkUpload::Lettings::CsvParser.new(path:) end diff --git a/spec/services/bulk_upload/lettings/validator_spec.rb b/spec/services/bulk_upload/lettings/validator_spec.rb index 263c83163..1a7da8803 100644 --- a/spec/services/bulk_upload/lettings/validator_spec.rb +++ b/spec/services/bulk_upload/lettings/validator_spec.rb @@ -255,5 +255,19 @@ RSpec.describe BulkUpload::Lettings::Validator do end end end + + context "when the file contains a log which already exists in the db" do + let(:log_1) { create(:lettings_log, :completed, lettype: 7, created_by: user) } + + before do + file.write(BulkUpload::LogToCsv.new(log: log_1, line_ending: "\r\n", col_offset: 0).to_csv_row) + file.close + end + + it "returns true" do + validator.call + expect(validator).not_to be_create_logs + end + end end end