Merge pull request #10060 from tchak/fix-improuve-municipalities-handling-by-ban-api

fix(adress): show postal code on municipalities
This commit is contained in:
Paul Chavard 2024-03-04 11:11:38 +00:00 committed by GitHub
commit 124466a6a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 197 additions and 184 deletions

View file

@ -18,11 +18,20 @@ class DataSources::AdresseController < ApplicationController
private
def fetch_results
Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 })
Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 }, timeout: 3)
end
def format_results(results)
results[:features].map do
results[:features].flat_map do |feature|
if feature[:properties][:type] == 'municipality'
departement_code = feature[:properties][:context].split(',').first
APIGeoService.commune_postal_codes(departement_code, feature[:properties][:citycode]).map do |postcode|
feature.deep_merge(properties: { postcode:, label: "#{feature[:properties][:label]} (#{postcode})" })
end
else
feature
end
end.map do
{
label: _1[:properties][:label],
value: _1[:properties][:label],

View file

@ -31,7 +31,7 @@ class DataSources::CommuneController < ApplicationController
nom: name,
boost: 'population',
limit: 100
})
}, timeout: 3)
end
def fetch_by_postal_code(postal_code)
@ -39,8 +39,8 @@ class DataSources::CommuneController < ApplicationController
type: 'commune-actuelle,arrondissement-municipal',
codePostal: postal_code,
boost: 'population',
limit: 20
})
limit: 50
}, timeout: 3)
end
def postal_code?(string)

View file

@ -81,6 +81,10 @@ class APIGeoService
communes(departement_code).find { _1[:name] == name }&.dig(:code)
end
def commune_postal_codes(departement_code, code)
communes(departement_code).filter { _1[:code] == code }.map { _1[:postal_code] }
end
def parse_ban_address(feature)
return unless ban_address_schema.valid?(feature)

File diff suppressed because one or more lines are too long