From 3afe3002b5108ba9929e5b98fca8a286691a5fd3 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 10 May 2023 15:15:13 +0100 Subject: [PATCH] Confirm the soft validations --- .../bulk_upload/lettings/log_creator.rb | 14 ++++- .../bulk_upload/lettings/validator.rb | 2 +- app/services/bulk_upload/processor.rb | 5 +- app/services/bulk_upload/sales/log_creator.rb | 3 +- .../bulk_upload/lettings/log_creator_spec.rb | 61 +++++++++++++++++++ spec/services/bulk_upload/processor_spec.rb | 20 ++++++ 6 files changed, 100 insertions(+), 5 deletions(-) diff --git a/app/services/bulk_upload/lettings/log_creator.rb b/app/services/bulk_upload/lettings/log_creator.rb index 8d3a6cbd7..b227167ad 100644 --- a/app/services/bulk_upload/lettings/log_creator.rb +++ b/app/services/bulk_upload/lettings/log_creator.rb @@ -1,9 +1,10 @@ class BulkUpload::Lettings::LogCreator attr_reader :bulk_upload, :path - def initialize(bulk_upload:, path:) + def initialize(bulk_upload:, path:, confirm_soft_validations: false) @bulk_upload = bulk_upload @path = path + @confirm_soft_validations = confirm_soft_validations end def call @@ -18,6 +19,17 @@ class BulkUpload::Lettings::LogCreator row_parser.log.status = "pending" row_parser.log.status_cache = row_parser.log.calculate_status + # confirm soft validations + if @confirm_soft_validations + row_parser.log.retirement_value_check = 0 + row_parser.log.pregnancy_value_check = 0 + row_parser.log.major_repairs_date_value_check = 0 + row_parser.log.void_date_value_check = 0 + row_parser.log.rent_value_check = 0 + row_parser.log.net_income_value_check = 0 + row_parser.log.carehome_charges_value_check = 0 + end + begin row_parser.log.save! rescue StandardError => e diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 3a2bf073e..68cb5be08 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -65,7 +65,7 @@ class BulkUpload::Lettings::Validator def soft_validation_errors_only? errors = bulk_upload.bulk_upload_errors - errors.count == errors.where(category: "soft_validation").count + errors.count == errors.where(category: "soft_validation").count && errors.count.positive? end def over_column_error_threshold? diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index 649984c1c..a77a68de9 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -17,7 +17,7 @@ class BulkUpload::Processor elsif validator.create_logs? create_logs - + if validator.soft_validation_errors_only? send_check_soft_validations_mail elsif created_logs_but_incompleted? @@ -90,10 +90,11 @@ private bulk_upload.user end - def create_logs + def create_logs(confirm_soft_validations: false) log_creator_class.new( bulk_upload:, path: downloader.path, + confirm_soft_validations:, ).call end diff --git a/app/services/bulk_upload/sales/log_creator.rb b/app/services/bulk_upload/sales/log_creator.rb index 69ef637fa..1267a20ee 100644 --- a/app/services/bulk_upload/sales/log_creator.rb +++ b/app/services/bulk_upload/sales/log_creator.rb @@ -1,9 +1,10 @@ class BulkUpload::Sales::LogCreator attr_reader :bulk_upload, :path - def initialize(bulk_upload:, path:) + def initialize(bulk_upload:, path:, confirm_soft_validations: false) @bulk_upload = bulk_upload @path = path + @confirm_soft_validations = confirm_soft_validations end def call diff --git a/spec/services/bulk_upload/lettings/log_creator_spec.rb b/spec/services/bulk_upload/lettings/log_creator_spec.rb index 9c28cc81e..12e2c26e5 100644 --- a/spec/services/bulk_upload/lettings/log_creator_spec.rb +++ b/spec/services/bulk_upload/lettings/log_creator_spec.rb @@ -129,6 +129,67 @@ RSpec.describe BulkUpload::Lettings::LogCreator do end end + context "with a valid csv and soft validations" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) do + build( + :lettings_log, + :completed, + renttype: 3, + age1: 22, + age1_known: 0, + ecstat1: 5, + owning_organisation: owning_org, + managing_organisation: owning_org, + created_by: user, + national: 18, + waityear: 9, + joint: 2, + tenancy: 9, + ppcodenk: 0, + ) + end + + before do + file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.rewind + end + + it "creates a new log" do + expect { service.call }.to change(LettingsLog, :count) + end + + it "creates a log with pending status" do + service.call + expect(LettingsLog.last.status).to eql("pending") + end + + context "when confirming soft validations" do + subject(:service) { described_class.new(bulk_upload:, path:, confirm_soft_validations: true) } + + it "sets unanswered soft validations to yes" do + service.call + + log = LettingsLog.last + expect(log.age1).to be(22) + expect(log.ecstat1).to be(5) + expect(log.retirement_value_check).to be(0) + end + end + + context "when not confirming soft validations" do + it "does not set unanswered soft validations" do + service.call + + log = LettingsLog.last + expect(log.age1).to be(22) + expect(log.ecstat1).to be(5) + expect(log.retirement_value_check).to be(nil) + end + end + end + context "when valid csv with existing log" do xit "what should happen?" end diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index 3fe22c439..2adbd9616 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -234,6 +234,16 @@ RSpec.describe BulkUpload::Processor do expect(BulkUploadMailer).to have_received(:send_how_fix_upload_mail) expect(mail_double).to have_received(:deliver_later) end + + it "calls log creator without the confirm_soft_validations option" do + log_creator_double = instance_double(BulkUpload::Lettings::LogCreator, call: nil) + + allow(BulkUpload::Lettings::LogCreator).to receive(:new).and_return(log_creator_double) + + processor.call + + expect(BulkUpload::Lettings::LogCreator).to have_received(:new).with(bulk_upload:, path:, confirm_soft_validations: false) + end end context "when a bulk upload has logs with only soft validations triggered" do @@ -302,6 +312,16 @@ RSpec.describe BulkUpload::Processor do expect(BulkUploadMailer).not_to have_received(:send_how_fix_upload_mail) expect(mail_double).to have_received(:deliver_later) end + + it "calls log creator with the confirm_soft_validations option" do + log_creator_double = instance_double(BulkUpload::Lettings::LogCreator, call: nil) + + allow(BulkUpload::Lettings::LogCreator).to receive(:new).and_return(log_creator_double) + + processor.call + + expect(BulkUpload::Lettings::LogCreator).to have_received(:new).with(bulk_upload:, path:, confirm_soft_validations: true) + end end context "when upload has no setup errors something blocks log creation" do