fix(commune): add fallbacks to commune name resolution
This commit is contained in:
parent
f5bd129218
commit
02dcead04a
5 changed files with 81 additions and 17 deletions
|
@ -5,5 +5,11 @@ module Types::Champs
|
|||
field :address, Types::AddressType, null: true
|
||||
field :commune, Types::Champs::CommuneChampType::CommuneType, null: true
|
||||
field :departement, Types::Champs::DepartementChampType::DepartementType, null: true
|
||||
|
||||
def address
|
||||
if object.full_address?
|
||||
object.address.merge('city_code' => object.commune.fetch(:code))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -69,10 +69,6 @@ class Champs::AddressChamp < Champs::TextChamp
|
|||
APIGeoService.departement_name(address.fetch('department_code'))
|
||||
end
|
||||
|
||||
def commune_name
|
||||
APIGeoService.commune_name(address.fetch('department_code'), address.fetch('city_code'))
|
||||
end
|
||||
|
||||
def departement
|
||||
if full_address?
|
||||
{ code: address.fetch('department_code'), name: departement_name }
|
||||
|
@ -81,7 +77,30 @@ class Champs::AddressChamp < Champs::TextChamp
|
|||
|
||||
def commune
|
||||
if full_address?
|
||||
{ code: address.fetch('city_code'), name: commune_name, postal_code: address.fetch('postal_code') }
|
||||
department_code = address.fetch('department_code')
|
||||
city_code = address.fetch('city_code')
|
||||
city_name = address.fetch('city_name')
|
||||
postal_code = address.fetch('postal_code')
|
||||
|
||||
commune_name = APIGeoService.commune_name(department_code, city_code)
|
||||
commune_code = APIGeoService.commune_code(department_code, city_name)
|
||||
|
||||
if commune_name.present?
|
||||
{
|
||||
code: city_code,
|
||||
name: commune_name
|
||||
}
|
||||
elsif commune_code.present?
|
||||
{
|
||||
code: commune_code,
|
||||
name: city_name
|
||||
}
|
||||
else
|
||||
{
|
||||
code: city_code,
|
||||
name: city_name
|
||||
}
|
||||
end.merge(postal_code:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -60,17 +60,18 @@ class Champs::CommuneChamp < Champs::TextChamp
|
|||
|
||||
def name
|
||||
if code?
|
||||
APIGeoService.commune_name(code_departement, code)
|
||||
APIGeoService.commune_name(code_departement, code).presence || safe_to_s
|
||||
else
|
||||
value.present? ? value.to_s : ''
|
||||
safe_to_s
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
if code?
|
||||
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal})"
|
||||
name = APIGeoService.commune_name(code_departement, code)
|
||||
name.present? ? "#{name} (#{code_postal})" : safe_to_s
|
||||
else
|
||||
value.present? ? value.to_s : ''
|
||||
safe_to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -111,6 +112,10 @@ class Champs::CommuneChamp < Champs::TextChamp
|
|||
|
||||
private
|
||||
|
||||
def safe_to_s
|
||||
value.present? ? value.to_s : ''
|
||||
end
|
||||
|
||||
def on_code_postal_change
|
||||
if code_postal_changed?
|
||||
if communes.one?
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
- if champ.to_s.present?
|
||||
%p= format_text_value(champ.to_s)
|
||||
- if champ.data.present?
|
||||
%p
|
||||
Code INSEE :
|
||||
= champ.data['city_code']
|
||||
= format_text_value(champ.to_s)
|
||||
- if champ.full_address?
|
||||
Code INSEE :
|
||||
= champ.commune&.fetch(:code)
|
||||
|
|
|
@ -11,10 +11,45 @@ describe Champs::AddressChamp do
|
|||
end
|
||||
|
||||
context "with value and data" do
|
||||
let(:value) { 'Paris' }
|
||||
let(:data) { { label: 'Paris' } }
|
||||
let(:value) { '33 Rue Rébeval 75019 Paris' }
|
||||
let(:data) do
|
||||
{
|
||||
"type" => "housenumber",
|
||||
"label" => "33 Rue Rébeval 75019 Paris",
|
||||
"city_code" => "75119",
|
||||
"city_name" => "Paris",
|
||||
"postal_code" => "75019",
|
||||
"region_code" => "11",
|
||||
"region_name" => "Île-de-France",
|
||||
"street_name" => "Rue Rébeval",
|
||||
"street_number" => "33",
|
||||
"street_address" => "33 Rue Rébeval",
|
||||
"department_code" => "75",
|
||||
"department_name" => "Paris"
|
||||
}
|
||||
end
|
||||
|
||||
it { expect(champ.address_label).to eq('Paris') }
|
||||
it { expect(champ.address_label).to eq('33 Rue Rébeval 75019 Paris') }
|
||||
it { expect(champ.full_address?).to be_truthy }
|
||||
it { expect(champ.commune).to eq({ name: 'Paris 19e Arrondissement', code: '75119', postal_code: '75019' }) }
|
||||
end
|
||||
|
||||
context "with wrong code INSEE" do
|
||||
let(:value) { 'Rue du Bois Charles 27700 Les Trois Lacs' }
|
||||
let(:data) do
|
||||
{
|
||||
"type" => "housenumber",
|
||||
"label" => "Rue du Bois Charles 27700 Les Trois Lacs",
|
||||
"city_code" => "27058",
|
||||
"city_name" => "Les Trois Lacs",
|
||||
"postal_code" => "27700",
|
||||
"department_code" => "27",
|
||||
"department_name" => "Eure"
|
||||
}
|
||||
end
|
||||
|
||||
it { expect(champ.address_label).to eq('Rue du Bois Charles 27700 Les Trois Lacs') }
|
||||
it { expect(champ.full_address?).to be_truthy }
|
||||
it { expect(champ.commune).to eq({ name: 'Les Trois Lacs', code: '27676', postal_code: '27700' }) }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue