diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index be0a71e05..19780ef87 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -43,8 +43,25 @@ class BulkUploadMailer < NotifyMailer end end - def send_correct_and_upload_again_mail(bulk_upload:) - error_description = "We noticed that you have a lot of similar errors in column #{columns_with_errors(bulk_upload:)}. Please correct your data export and upload again." + def send_correct_and_upload_again_mail( + any_setup_sections_incomplete, + over_column_error_threshold, + any_logs_already_exist, + any_logs_invalid, + bulk_upload: + ) + + any_setup_sections_incomplete_error_description = "Placeholder text for setup sections incomplete case. " + over_column_error_threshold_error_description = "We noticed that you have a lot of similar errors in column #{columns_with_errors(bulk_upload:)}. " + any_logs_already_exist_error_description = "We noticed that one of the logs you are trying has been created previously. " + any_logs_invalid_error_description = "Placeholder text for invalid logs case. " + + error_description = "" + error_description << any_setup_sections_incomplete_error_description if any_setup_sections_incomplete + error_description << over_column_error_threshold_error_description if over_column_error_threshold + error_description << any_logs_already_exist_error_description if any_logs_already_exist + error_description << any_logs_invalid_error_description if any_logs_invalid + error_description << "Please correct your data export and upload again." send_email( bulk_upload.user.email, diff --git a/app/services/bulk_upload/lettings/row_parser.rb b/app/services/bulk_upload/lettings/row_parser.rb index 7106a9530..ef969581f 100644 --- a/app/services/bulk_upload/lettings/row_parser.rb +++ b/app/services/bulk_upload/lettings/row_parser.rb @@ -181,6 +181,24 @@ class BulkUpload::Lettings::RowParser @log ||= LettingsLog.new(attributes_for_log) end + def log_already_exists? + fields_for_duplicity_check = %w( + startdate + postcode_full + brent + scharge + pscharge + supcharg + tenancycode + age1 + sex1 + ecstat1 + ethnic + ) + + LettingsLog.exists?(Hash[fields_for_duplicity_check.collect { |field| [field, log[field]] }]) + end + private def validate_no_and_dont_know_disabled_needs_conjunction @@ -314,24 +332,6 @@ private end end - def log_already_exists? - fields_for_duplicity_check = %w( - startdate - postcode_full - brent - scharge - pscharge - supcharg - tenancycode - age1 - sex1 - ecstat1 - ethnic - ) - - LettingsLog.exists?(Hash[fields_for_duplicity_check.collect { |field| [field, log[field]] }]) - end - def field_mapping_for_errors { lettype: [:field_1], diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 96937f687..741c1b026 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -177,16 +177,15 @@ class BulkUpload::Lettings::Validator return false if any_setup_sections_incomplete? return false if over_column_error_threshold? return false if any_logs_already_exist? + return false if any_logs_invalid? - row_parsers.all? { |row_parser| row_parser.log.valid? } + true end def self.question_for_field(field) QUESTIONS[field] end -private - def any_setup_sections_incomplete? row_parsers.any? { |row_parser| row_parser.log.form.setup_sections[0].subsections[0].is_incomplete?(row_parser.log) } end @@ -208,6 +207,12 @@ private row_parsers.any? { |row_parser| row_parser.log_already_exists? } end + def any_logs_invalid? + row_parsers.any? { |row_parser| row_parser.log.invalid? } + end + + private + def csv_parser @csv_parser ||= BulkUpload::Lettings::CsvParser.new(path:) end diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index bbf7e6cd1..63c9bf318 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -8,15 +8,24 @@ class BulkUpload::Processor def call download + # binding.pry return send_failure_mail if validator.invalid? validator.call - create_logs if validator.create_logs? - send_correct_and_upload_again_mail unless validator.create_logs? + if validator.create_logs? + create_logs + send_fix_errors_mail if created_logs_but_incompleted? + send_success_mail if created_logs_and_all_completed? + else + send_correct_and_upload_again_mail( + validator.any_setup_sections_incomplete?, + validator.over_column_error_threshold?, + validator.any_logs_already_exist?, + validator.any_logs_invalid? + ) + end - send_fix_errors_mail if created_logs_but_incompleted? - send_success_mail if created_logs_and_all_completed? rescue StandardError => e Sentry.capture_exception(e) send_failure_mail @@ -26,8 +35,19 @@ class BulkUpload::Processor private - def send_correct_and_upload_again_mail - BulkUploadMailer.send_correct_and_upload_again_mail(bulk_upload:).deliver_later + def send_correct_and_upload_again_mail( + any_setup_sections_incomplete, + over_column_error_threshold, + any_logs_already_exist, + any_logs_invalid + ) + BulkUploadMailer.send_correct_and_upload_again_mail( + any_setup_sections_incomplete, + over_column_error_threshold, + any_logs_already_exist, + any_logs_invalid, + bulk_upload: + ).deliver_later end def send_fix_errors_mail