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 { address {
...AddressFragment ...AddressFragment
} }
}
... on CommuneChamp {
commune { commune {
...CommuneFragment
}
departement {
...DepartementFragment
}
}
... on EpciChamp {
epci {
name name
code code
} }
departement { departement {
name ...DepartementFragment
code }
}
... on CommuneChamp {
commune {
...CommuneFragment
}
departement {
...DepartementFragment
} }
} }
... on DepartementChamp { ... on DepartementChamp {
departement { departement {
name ...DepartementFragment
code
} }
} }
... on RegionChamp { ... on RegionChamp {
@ -590,6 +602,17 @@ class API::V2::StoredQuery
regionCode regionCode
} }
fragment DepartementFragment on Departement {
name
code
}
fragment CommuneFragment on Commune {
name
code
postalCode
}
fragment PageInfoFragment on PageInfo { fragment PageInfoFragment on PageInfo {
hasPreviousPage hasPreviousPage
hasNextPage hasNextPage

View file

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

View file

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

View file

@ -5,7 +5,7 @@ module Types::Champs
class CommuneType < Types::BaseObject class CommuneType < Types::BaseObject
field :name, String, "Le nom de la commune", null: false field :name, String, "Le nom de la commune", null: false
field :code, String, "Le code INSEE", 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 end
field :commune, CommuneType, null: true field :commune, CommuneType, null: true

View file

@ -64,4 +64,24 @@ class Champs::AddressChamp < Champs::TextChamp
def fetch_external_data def fetch_external_data
APIAddress::AddressAdapter.new(external_id).to_params APIAddress::AddressAdapter.new(external_id).to_params
end 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 end

View file

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

View file

@ -1,4 +1,4 @@
= champ.name = champ.to_s
- if champ.code? - if champ.code?
%p.text-sm %p.text-sm
Code INSEE : 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 end
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
dossier.champs_public.second.update(data: address) dossier.champs_public.second.update(data: address)
end 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][0][:__typename]).to eq "CommuneChamp"
expect(data[:dossier][:champs][1][:__typename]).to eq "AddressChamp" expect(data[:dossier][:champs][1][:__typename]).to eq "AddressChamp"
expect(data[:dossier][:champs][2][:__typename]).to eq "SiretChamp" 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][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]) expect(data[:dossier][:champs][0][:id]).to eq(data[:dossier][:revision][:champDescriptors][0][:id])
end end
@ -288,6 +295,13 @@ RSpec.describe Types::DossierType, type: :graphql do
address { address {
...AddressFragment ...AddressFragment
} }
commune {
...CommuneFragment
}
departement {
name
code
}
} }
... on SiretChamp { ... on SiretChamp {
etablissement { etablissement {
@ -302,9 +316,15 @@ RSpec.describe Types::DossierType, type: :graphql do
commune { commune {
...CommuneFragment ...CommuneFragment
} }
departement {
name
code
}
} }
fragment CommuneFragment on Commune { fragment CommuneFragment on Commune {
name
code code
postalCode
} }
fragment AddressFragment on Address { fragment AddressFragment on Address {
type type

View file

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