From 9f7cd311c11f2faa1b12df7bee7a345466c117cd Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 19 Apr 2023 16:28:13 +0100 Subject: [PATCH] Add validations --- .../bulk_upload/sales/year2022/row_parser.rb | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 5a9a86b90..85d0f97fc 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -286,8 +286,37 @@ class BulkUpload::Sales::Year2022::RowParser @log ||= SalesLog.new(attributes_for_log) end + def valid? + errors.clear + + return true if blank_row? + + log.valid? + + super + + log.errors.each do |error| + fields = field_mapping_for_errors[error.attribute] || [] + + fields.each do |field| + unless errors.include?(field) + errors.add(field, error.message) + end + end + end + + errors.blank? + end + private + def field_mapping_for_errors + { + age1_known: %i[field_7], + age1: %i[field_7], + } + end + def attributes_for_log attributes = {} attributes["purchid"] = field_1 @@ -368,7 +397,7 @@ private attributes["builtype"] = field_52 attributes["la_known"] = field_53.present? ? 1 : 0 attributes["la"] = field_53 - attributes["pcodenk"] = 0 if postcode_full.present? || field_43 == 1 + attributes["pcodenk"] = 0 if postcode_full.present? attributes["postcode_full"] = postcode_full attributes["wchair"] = field_56