From db65c9178e1b276e14c8b24e09fcb36c5d706ff5 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 16 Apr 2024 14:54:53 +0200 Subject: [PATCH] =?UTF-8?q?fix(address):=20BAN=20does=20not=20return=20pos?= =?UTF-8?q?tcode=20for=20some=20TOM=20like=20Nouvelle-Cal=C3=A9donie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit But we have to fallback to an empty string because API graphql / serializer expects a non-null attribute. --- app/models/champs/address_champ.rb | 10 +++++++--- app/services/api_geo_service.rb | 2 +- spec/models/champs/address_champ_spec.rb | 20 ++++++++++++++++++++ spec/services/api_geo_service_spec.rb | 9 +++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/models/champs/address_champ.rb b/app/models/champs/address_champ.rb index 169180bcf..751b9844a 100644 --- a/app/models/champs/address_champ.rb +++ b/app/models/champs/address_champ.rb @@ -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 diff --git a/app/services/api_geo_service.rb b/app/services/api_geo_service.rb index f2e372bfe..f1ed8c228 100644 --- a/app/services/api_geo_service.rb +++ b/app/services/api_geo_service.rb @@ -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'] diff --git a/spec/models/champs/address_champ_spec.rb b/spec/models/champs/address_champ_spec.rb index ed86d7d6f..21b8c458c 100644 --- a/spec/models/champs/address_champ_spec.rb +++ b/spec/models/champs/address_champ_spec.rb @@ -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 diff --git a/spec/services/api_geo_service_spec.rb b/spec/services/api_geo_service_spec.rb index 00e7a1068..382cd5070 100644 --- a/spec/services/api_geo_service_spec.rb +++ b/spec/services/api_geo_service_spec.rb @@ -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