diff --git a/app/models/log.rb b/app/models/log.rb index 1b7d2a826..7b4e5cf35 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -59,8 +59,16 @@ class Log < ApplicationRecord def process_uprn_change! if uprn.present? - presenter = uprn_presenter - return if presenter.nil? + service = UprnClient.new(uprn) + service.call + + if service.error.present? + errors.add(:uprn, :uprn_error, message: service.error) + errors.add(:uprn_selection, :uprn_error, message: service.error) + return + end + + presenter = UprnDataPresenter.new(service.result) self.uprn_known = 1 self.uprn_confirmed = nil unless skip_update_uprn_confirmed @@ -74,28 +82,14 @@ class Log < ApplicationRecord end end - def uprn_presenter - @uprn_presenter ||= begin - service = UprnClient.new(uprn) - service.call - return if service.result.blank? - - if service.error.present? - errors.add(:uprn, :uprn_error, message: service.error) - errors.add(:uprn_selection, :uprn_error, message: service.error) - return - end - - UprnDataPresenter.new(service.result) - end - end - def process_address_change! if uprn_selection.present? || select_best_address_match.present? if select_best_address_match - presenter = address_presenter - return nil if presenter.nil? + service = AddressClient.new(address_string) + service.call + return nil if service.result.blank? || service.error.present? + presenter = AddressDataPresenter.new(service.result.first) os_match_threshold_for_bulk_upload = 0.7 if presenter.match >= os_match_threshold_for_bulk_upload self.uprn_selection = presenter.uprn @@ -122,21 +116,6 @@ class Log < ApplicationRecord end end - def address_presenter - @address_presenter ||= begin - service = AddressClient.new(address_string) - service.call - return nil if service.result.blank? - - if service.error.present? - errors.add(:address_line1_input, :address_error, message: service.error) - errors.add(:postcode_full_input, :address_error, message: service.error) - return - end - AddressDataPresenter.new(service.result.first) - end - end - def address_string "#{address_line1_input}, #{postcode_full_input}" end @@ -298,7 +277,7 @@ class Log < ApplicationRecord nationality_all_group&.zero? || nationality_all_group == 826 end -private + private # Handle logs that are older than previous collection start date def older_than_previous_collection_year?