feat(graphql): expose commune and departement information on address

This commit is contained in:
Paul Chavard 2023-04-05 18:02:42 +02:00
parent 0b95a912c6
commit aaae3f461d
10 changed files with 153 additions and 21 deletions

View file

@ -493,21 +493,33 @@ class API::V2::StoredQuery
address {
...AddressFragment
}
}
... on CommuneChamp {
commune {
...CommuneFragment
}
departement {
...DepartementFragment
}
}
... on EpciChamp {
epci {
name
code
}
departement {
name
code
...DepartementFragment
}
}
... on CommuneChamp {
commune {
...CommuneFragment
}
departement {
...DepartementFragment
}
}
... on DepartementChamp {
departement {
name
code
...DepartementFragment
}
}
... on RegionChamp {
@ -590,6 +602,17 @@ class API::V2::StoredQuery
regionCode
}
fragment DepartementFragment on Departement {
name
code
}
fragment CommuneFragment on Commune {
name
code
postalCode
}
fragment PageInfoFragment on PageInfo {
hasPreviousPage
hasNextPage

View file

@ -67,6 +67,8 @@ type Address {
type AddressChamp implements Champ {
address: Address
commune: Commune
departement: Departement
id: ID!
"""

View file

@ -3,5 +3,7 @@ module Types::Champs
implements Types::ChampType
field :address, Types::AddressType, null: true
field :commune, Types::Champs::CommuneChampType::CommuneType, null: true
field :departement, Types::Champs::DepartementChampType::DepartementType, null: true
end
end

View file

@ -5,7 +5,7 @@ module Types::Champs
class CommuneType < Types::BaseObject
field :name, String, "Le nom de la commune", null: false
field :code, String, "Le code INSEE", null: false
field :postal_code, String, "Le code postal", null: true, method: :code_postal
field :postal_code, String, "Le code postal", null: true
end
field :commune, CommuneType, null: true

View file

@ -64,4 +64,24 @@ class Champs::AddressChamp < Champs::TextChamp
def fetch_external_data
APIAddress::AddressAdapter.new(external_id).to_params
end
def departement_name
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 }
end
end
def commune
if full_address?
{ code: address.fetch('city_code'), name: commune_name, postal_code: address.fetch('postal_code') }
end
end
end

View file

@ -25,7 +25,7 @@ class Champs::CommuneChamp < Champs::TextChamp
before_validation :on_code_postal_change
def for_export
[name, code? ? code : '', departement? ? departement_code_and_name : '']
[to_s, code? ? code : '', departement? ? departement_code_and_name : '']
end
def departement_name
@ -52,16 +52,22 @@ class Champs::CommuneChamp < Champs::TextChamp
code_postal.present?
end
alias postal_code code_postal
def name
if code?
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal_with_fallback})"
APIGeoService.commune_name(code_departement, code)
else
value.present? ? value.to_s : ''
end
end
def to_s
name
if code?
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal_with_fallback})"
else
value.present? ? value.to_s : ''
end
end
def code

View file

@ -1,4 +1,4 @@
= champ.name
= champ.to_s
- if champ.code?
%p.text-sm
Code INSEE :

File diff suppressed because one or more lines are too long

View file

@ -48,7 +48,11 @@ RSpec.describe Types::DossierType, type: :graphql do
}
end
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
dossier.champs_public.second.update(data: address)
end
@ -56,6 +60,9 @@ RSpec.describe Types::DossierType, type: :graphql do
expect(data[:dossier][:champs][0][:__typename]).to eq "CommuneChamp"
expect(data[:dossier][:champs][1][:__typename]).to eq "AddressChamp"
expect(data[:dossier][:champs][2][:__typename]).to eq "SiretChamp"
expect(data[:dossier][:champs][1][:commune][:code]).to eq('75119')
expect(data[:dossier][:champs][1][:commune][:postalCode]).to eq('75019')
expect(data[:dossier][:champs][1][:departement][:code]).to eq('75')
expect(data[:dossier][:champs][2][:etablissement][:siret]).to eq dossier.champs_public[2].etablissement.siret
expect(data[:dossier][:champs][0][:id]).to eq(data[:dossier][:revision][:champDescriptors][0][:id])
end
@ -288,6 +295,13 @@ RSpec.describe Types::DossierType, type: :graphql do
address {
...AddressFragment
}
commune {
...CommuneFragment
}
departement {
name
code
}
}
... on SiretChamp {
etablissement {
@ -302,9 +316,15 @@ RSpec.describe Types::DossierType, type: :graphql do
commune {
...CommuneFragment
}
departement {
name
code
}
}
fragment CommuneFragment on Commune {
name
code
postalCode
}
fragment AddressFragment on Address {
type

View file

@ -15,7 +15,8 @@ describe Champs::CommuneChamp do
it 'with code_postal' do
champ.update(value: code_insee)
expect(champ.name).to eq('Châteldon (63290)')
expect(champ.to_s).to eq('Châteldon (63290)')
expect(champ.name).to eq('Châteldon')
expect(champ.external_id).to eq(code_insee)
expect(champ.code).to eq(code_insee)
expect(champ.code_departement).to eq(code_departement)
@ -29,7 +30,8 @@ describe Champs::CommuneChamp do
it 'with value' do
champ.update_column(:value, 'Châteldon (63290)')
expect(champ.name).to eq('Châteldon (63290)')
expect(champ.to_s).to eq('Châteldon (63290)')
expect(champ.name).to eq('Châteldon')
expect(champ.external_id).to eq(code_insee)
expect(champ.code).to eq(code_insee)
expect(champ.code_departement).to eq(code_departement)