From 4f5111a00c90443241aeb9cb4e39de76e0d6548f Mon Sep 17 00:00:00 2001 From: 24c02 <163450896+24c02@users.noreply.github.com> Date: Sun, 1 Jun 2025 19:54:35 -0400 Subject: [PATCH] fallback to known city/state on nx usps facility xlsx entry --- app/jobs/public/update_map_data_job.rb | 6 ++--- app/services/geocoding_service.rb | 2 ++ .../geocoding_service/usps_facilities.rb | 22 ++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/jobs/public/update_map_data_job.rb b/app/jobs/public/update_map_data_job.rb index d9d22d6..99a84d4 100644 --- a/app/jobs/public/update_map_data_job.rb +++ b/app/jobs/public/update_map_data_job.rb @@ -60,7 +60,7 @@ class Public::UpdateMapDataJob < ApplicationJob hydrated = event.hydrated locale_key = hydrated.scan_locale_key if locale_key.present? - coords = geocode_usps_facility(locale_key) + coords = geocode_usps_facility(locale_key, event) coordinates << coords if coords end rescue => e @@ -105,8 +105,8 @@ class Public::UpdateMapDataJob < ApplicationJob } end - def geocode_usps_facility(locale_key) - result = GeocodingService::USPSFacilities.coords_for_locale_key(locale_key) + def geocode_usps_facility(locale_key, event) + result = GeocodingService::USPSFacilities.coords_for_locale_key(locale_key, event) return nil unless result { diff --git a/app/services/geocoding_service.rb b/app/services/geocoding_service.rb index ff30df9..405cc85 100644 --- a/app/services/geocoding_service.rb +++ b/app/services/geocoding_service.rb @@ -48,6 +48,8 @@ module GeocodingService end def hackclub_geocode(params) + return nil if params.nil? + Rails.logger.info "Hack Club Geocoding: #{params}" address_components = [] diff --git a/app/services/geocoding_service/usps_facilities.rb b/app/services/geocoding_service/usps_facilities.rb index b060718..0013a86 100644 --- a/app/services/geocoding_service/usps_facilities.rb +++ b/app/services/geocoding_service/usps_facilities.rb @@ -8,11 +8,27 @@ module GeocodingService }, } - def coords_for_locale_key(locale_key) + def coords_for_locale_key(locale_key, event = nil) SPECIAL_CASES[locale_key] || Rails.cache.fetch("geocode_usps_facility_#{locale_key}", expires_in: 1.day) do - GeocodingService.first_hit(address_for_locale_key(locale_key)) || - GeocodingService.first_hit(address_for_locale_key(locale_key).slice(:city, :state, :postalcode, :country)) + address = address_for_locale_key(locale_key) + if address + GeocodingService.first_hit(address) || + GeocodingService.first_hit(address.slice(:city, :state, :postalcode, :country)) + elsif event + # Fallback to event scan facility data when USPS facility lookup fails + facility = event.scan_facility + fallback_address = { + city: facility[:city], + state: facility[:state], + postalcode: facility[:zip], + country: "US", + } + Rails.logger.warn "USPS facility not found for locale_key #{locale_key}, falling back to scan facility data: #{fallback_address}" + GeocodingService.first_hit(fallback_address) + else + nil + end end end