fix(address): BAN does not return postcode for some TOM like Nouvelle-Calédonie

But we have to fallback to an empty string because API graphql / serializer
expects a non-null attribute.
This commit is contained in:
Colin Darie 2024-04-16 14:54:53 +02:00
parent 7a80574afc
commit db65c9178e
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
4 changed files with 37 additions and 4 deletions

View file

@ -97,9 +97,13 @@ class Champs::AddressChamp < Champs::TextChamp
end
def commune_name
if full_address?
"#{APIGeoService.commune_name(code_departement, address['city_code'])} (#{address['postal_code']})"
end
return if !full_address?
commune = APIGeoService.commune_name(code_departement, address['city_code'])
return commune if address['postal_code'].blank?
"#{commune} (#{address['postal_code']})"
end
def commune

View file

@ -115,7 +115,7 @@ class APIGeoService
label: properties.fetch('label'),
type: properties.fetch('type'),
street_address: properties.fetch('name'),
postal_code: properties.fetch('postcode'),
postal_code: properties.fetch('postcode') { '' }, # API graphql / serializer requires non-null data
street_number: properties['housenumber'],
street_name: properties['street'],
geometry: feature['geometry']

View file

@ -53,4 +53,24 @@ describe Champs::AddressChamp do
it { expect(champ.full_address?).to be_truthy }
it { expect(champ.commune).to eq({ name: 'Les Trois Lacs', code: '27676', postal_code: '27700' }) }
end
context "with empty code postal" do
let(:value) { '15 rue Baudelaire Nouméa' }
let(:data) do
{
"type" => "housenumber",
"label" => "15 Rue BAUDELAIRE Nouméa",
"city_code" => "98818",
"city_name" => "Nouméa",
"postal_code" => "",
"department_code" => "988",
"department_name" => "Nouvelle-Calédonie"
}
end
it do
expect(champ.commune).to eq({ name: 'Nouméa', code: '98818', postal_code: '' })
expect(champ.commune_name).to eq("Nouméa")
end
end
end

View file

@ -106,6 +106,15 @@ describe APIGeoService do
it { expect(subject[:city_name]).to eq('Paris') }
end
context 'without postcode (nouméa…)' do
let(:feature) do
features.first.tap { _1["properties"].delete("postcode") }
end
it { expect(subject[:postal_code]).to eq('') }
it { expect(subject[:city_name]).to eq('Paris') }
end
end
describe 'safely_normalize_city_name' do