fallback to known city/state on nx usps facility xlsx entry

This commit is contained in:
24c02 2025-06-01 19:54:35 -04:00
parent d40051675f
commit 4f5111a00c
3 changed files with 24 additions and 6 deletions

View file

@ -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
{

View file

@ -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 = []

View file

@ -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