Browse Source

feat: accept best "good" match in bulk upload

pull/2278/head
natdeanlewissoftwire 2 years ago
parent
commit
6141c7db1d
  1. 6
      app/models/form/lettings/pages/address_fallback.rb
  2. 2
      app/models/lettings_log.rb
  3. 24
      app/models/log.rb
  4. 4
      app/services/address_data_presenter.rb
  5. 17
      app/services/bulk_upload/lettings/year2024/row_parser.rb

6
app/models/form/lettings/pages/address_fallback.rb

@ -4,9 +4,9 @@ class Form::Lettings::Pages::AddressFallback < ::Form::Page
@id = "address"
@header = "Q12 - What is the property's address?"
@depends_on = [
{ "is_supported_housing?" => false, "uprn_known" => nil, "address_selection" => -1 },
{ "is_supported_housing?" => false, "uprn_known" => 0, "address_selection" => -1 },
{ "is_supported_housing?" => false, "uprn_confirmed" => 0, "address_selection" => -1 },
{ "is_supported_housing?" => false, "uprn_known" => nil, "address_selection" => 100 },
{ "is_supported_housing?" => false, "uprn_known" => 0, "address_selection" => 100 },
{ "is_supported_housing?" => false, "uprn_confirmed" => 0, "address_selection" => 100 },
{ "is_supported_housing?" => false, "uprn_known" => nil, "address_options_present?" => false },
{ "is_supported_housing?" => false, "uprn_known" => 0, "address_options_present?" => false },
{ "is_supported_housing?" => false, "uprn_confirmed" => 0, "address_options_present?" => false },

2
app/models/lettings_log.rb

@ -850,6 +850,6 @@ private
end
def should_process_address_change?
address_selection && startdate && (address_selection_changed? || startdate_changed?) && form.start_year_after_2024?
(address_selection || select_best_address_match) && startdate && ((address_selection_changed? || select_best_address_match) || startdate_changed?) && form.start_year_after_2024?
end
end

24
app/models/log.rb

@ -53,7 +53,7 @@ class Log < ApplicationRecord
scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) }
scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) }
attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :skip_update_address_selection, :skip_dpo_validation
attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :select_best_address_match, :skip_dpo_validation
delegate :present?, to: :address_options, prefix: true
@ -68,7 +68,7 @@ class Log < ApplicationRecord
self.uprn_known = 1
self.uprn_confirmed = nil unless skip_update_uprn_confirmed
self.address_selection = nil # unless skip_update_address_confirmed
self.address_selection = nil
self.address_line1 = presenter.address_line1
self.address_line2 = presenter.address_line2
self.town_or_city = presenter.town_or_city
@ -79,27 +79,37 @@ class Log < ApplicationRecord
end
def process_address_change!
if [address_selection, address_line1_input, postcode_full_input].all?(&:present?)
if [address_line1_input, postcode_full_input].all?(&:present?) && (address_selection.present? || select_best_address_match.present?)
address_string = "#{address_line1_input}, , , #{postcode_full_input}"
service = AddressClient.new(address_string)
service.call
return errors.add(:address_selection, :address_error, message: service.error) if service.error.present?
if select_best_address_match
presenter = AddressDataPresenter.new(service.result.first)
os_good_match_threshold = 0.8
if presenter.match >= os_good_match_threshold
self.address_selection = 0
else
return nil
end
end
if address_selection.between?(0, 9)
presenter = AddressDataPresenter.new(service.result[address_selection])
self.uprn_known = 1
self.uprn_confirmed = 1 # unless skip_update_uprn_confirmed
self.address_selection = nil # unless skip_update_address_confirmed
self.uprn = presenter.uprn # skip process uprn change?
self.uprn_confirmed = 1
self.address_selection = nil
self.uprn = presenter.uprn
self.address_line1 = presenter.address_line1
self.address_line2 = presenter.address_line2
self.town_or_city = presenter.town_or_city
self.postcode_full = presenter.postcode
self.county = nil
process_postcode_changes!
elsif address_selection == -1
elsif address_selection == 100
self.uprn_known = 0
self.uprn_confirmed = nil
self.uprn = nil

4
app/services/address_data_presenter.rb

@ -46,4 +46,8 @@ class AddressDataPresenter
def address
data["ADDRESS"]
end
def match
data["MATCH"]
end
end

17
app/services/bulk_upload/lettings/year2024/row_parser.rb

@ -376,6 +376,7 @@ class BulkUpload::Lettings::Year2024::RowParser
validate :validate_created_by_exists, on: :after_log
validate :validate_created_by_related, on: :after_log
validate :validate_address_option_found, on: :after_log
validate :validate_nulls, on: :after_log
@ -540,6 +541,14 @@ private
end
end
def validate_address_option_found
if log.address_selection.nil?
%i[field_17 field_18 field_19 field_21 field_22].each do |field|
errors.add(field, "We could not find this address. Edit the address data or fix this error on the CORE site")
end
end
end
def validate_incomplete_soft_validations
routed_to_soft_validation_questions = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) }.compact
routed_to_soft_validation_questions.each do |question|
@ -1021,6 +1030,7 @@ private
address_line2: [:field_18],
town_or_city: [:field_19],
county: [:field_20],
address_selection: [:field_17],
}.compact
end
@ -1217,10 +1227,17 @@ private
attributes["address_line2"] = field_18
attributes["town_or_city"] = field_19
attributes["county"] = field_20
attributes["address_line1_input"] = address_line1_input
attributes["postcode_full_input"] = postcode_full
attributes["select_best_address_match"] = true
attributes
end
def address_line1_input
[field_17, field_18, field_19, field_20].compact.join(", ")
end
def postcode_known
if postcode_full.present?
1

Loading…
Cancel
Save