feat(commune): prefill by postal code and insee

This commit is contained in:
Paul Chavard 2023-03-21 14:47:14 +01:00
parent 666db74b09
commit 76da68c3fb
6 changed files with 48 additions and 55 deletions

View file

@ -1,47 +1,38 @@
class TypesDeChamp::PrefillCommuneTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def all_possible_values
departements.map do |departement|
"#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/communes?codeDepartement=#{departement[:code]}"
end
[]
end
def example_value
departement_code = departements.pick(:code)
commune_code = APIGeoService.communes(departement_code).pick(:code)
[departement_code, commune_code]
APIGeoService.communes(departement_code).pick(:postal_code, :code)
end
def to_assignable_attributes(champ, value)
return if value.blank? || !value.is_a?(Array)
return if (departement_code = value.first).blank?
return if (departement_name = APIGeoService.departement_name(departement_code)).blank?
return if (postal_code = value.first).blank?
return if APIGeoService.communes_by_postal_code(postal_code).empty?
return if !value.one? && (commune_code = value.second).blank?
return if !value.one? && (commune_name = APIGeoService.commune_name(departement_code, commune_code)).blank?
return if !value.one? && !APIGeoService.communes_by_postal_code(postal_code).any? { _1[:code] == commune_code }
if value.one?
departement_attributes(champ, departement_code, departement_name)
code_postal_attributes(champ, postal_code)
else
departement_and_commune_attributes(champ, departement_code, departement_name, commune_code, commune_name)
code_postal_and_commune_attributes(champ, postal_code, commune_code)
end
end
private
def departement_attributes(champ, departement_code, departement_name)
def code_postal_attributes(champ, postal_code)
{
id: champ.id,
code_departement: departement_code,
departement: departement_name
code_postal: postal_code
}
end
def departement_and_commune_attributes(champ, departement_code, departement_name, commune_code, commune_name)
postal_code = APIGeoService.commune_postal_codes(departement_code, commune_code).first
departement_attributes(champ, departement_code, departement_name).merge(
external_id: commune_code,
value: "#{commune_name} (#{postal_code})"
)
def code_postal_and_commune_attributes(champ, postal_code, commune_code)
code_postal_attributes(champ, postal_code).merge(value: commune_code)
end
def departements

View file

@ -15,14 +15,14 @@ RSpec.describe PrefillParams do
VCR.insert_cassette('api_geo_regions')
VCR.insert_cassette('api_geo_departements')
VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_communes_01')
VCR.insert_cassette('api_geo_epcis')
end
after do
VCR.eject_cassette('api_geo_regions')
VCR.eject_cassette('api_geo_departements')
VCR.eject_cassette('api_geo_communes')
VCR.eject_cassette('api_geo_communes_01')
VCR.eject_cassette('api_geo_epcis')
end
@ -139,7 +139,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is authorized", :checkbox, "false"
it_behaves_like "a champ public value that is authorized", :drop_down_list, "value"
it_behaves_like "a champ public value that is authorized", :departements, "03"
it_behaves_like "a champ public value that is authorized", :communes, ['01', '01457']
it_behaves_like "a champ public value that is authorized", :communes, ['01540', '01457']
it_behaves_like "a champ public value that is authorized", :address, "20 avenue de Ségur 75007 Paris"
it_behaves_like "a champ public value that is authorized", :annuaire_education, "0050009H"
it_behaves_like "a champ public value that is authorized", :multiple_drop_down_list, ["val1", "val2"]
@ -183,7 +183,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ private value that is authorized", :rna, "value"
it_behaves_like "a champ private value that is authorized", :siret, "13002526500013"
it_behaves_like "a champ private value that is authorized", :departements, "03"
it_behaves_like "a champ private value that is authorized", :communes, ['01', '01457']
it_behaves_like "a champ private value that is authorized", :communes, ['01540', '01457']
it_behaves_like "a champ private value that is authorized", :address, "20 avenue de Ségur 75007 Paris"
it_behaves_like "a champ private value that is authorized", :annuaire_education, "0050009H"
it_behaves_like "a champ private value that is authorized", :multiple_drop_down_list, ["val1", "val2"]

View file

@ -12,12 +12,12 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
before do
VCR.insert_cassette('api_geo_departements')
VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_communes_01')
end
after do
VCR.eject_cassette('api_geo_departements')
VCR.eject_cassette('api_geo_communes')
VCR.eject_cassette('api_geo_communes_01')
end
describe 'ancestors' do
@ -26,21 +26,21 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
end
describe '#all_possible_values' do
let(:expected_values) do
departements.map { |departement| "#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/communes?codeDepartement=#{departement[:code]}" }
end
subject(:all_possible_values) { described_class.new(type_de_champ, procedure.active_revision).all_possible_values }
# describe '#all_possible_values' do
# let(:expected_values) do
# departements.map { |departement| "#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/communes?codeDepartement=#{departement[:code]}" }
# end
# subject(:all_possible_values) { described_class.new(type_de_champ, procedure.active_revision).all_possible_values }
it { expect(all_possible_values).to match(expected_values) }
end
# it { expect(all_possible_values).to match(expected_values) }
# end
describe '#example_value' do
let(:departement_code) { departements.pick(:code) }
let(:commune_code) { APIGeoService.communes(departement_code).pick(:code) }
let(:value) { APIGeoService.communes(departement_code).pick(:postal_code, :code) }
subject(:example_value) { described_class.new(type_de_champ, procedure.active_revision).example_value }
it { is_expected.to eq([departement_code, commune_code]) }
it { is_expected.to eq(value) }
end
describe '#to_assignable_attributes' do
@ -65,22 +65,22 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
end
context 'when the value is an array of one element' do
context 'when the first element is a valid departement code' do
let(:value) { ['01'] }
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain' }) }
context 'when the first element is a valid postal code' do
let(:value) { ['01540'] }
it { is_expected.to match({ id: champ.id, code_postal: '01540' }) }
end
context 'when the first element is not a valid departement code' do
context 'when the first element is not a valid postal code' do
let(:value) { ['totoro'] }
it { is_expected.to match(nil) }
end
end
context 'when the value is an array of two elements' do
context 'when the first element is a valid departement code' do
context 'when the first element is a valid postal code' do
context 'when the second element is a valid insee code' do
let(:value) { ['01', '01457'] }
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }) }
let(:value) { ['01540', '01457'] }
it { is_expected.to match({ id: champ.id, code_postal: '01540', value: '01457' }) }
end
context 'when the second element is not a valid insee code' do
@ -89,26 +89,26 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
end
end
context 'when the first element is not a valid departement code' do
context 'when the first element is not a valid postal code' do
let(:value) { ['totoro', '01457'] }
it { is_expected.to match(nil) }
end
end
context 'when the value is an array of three or more elements' do
context 'when the first element is a valid departement code' do
context 'when the first element is a valid postal code' do
context 'when the second element is a valid insee code' do
let(:value) { ['01', '01457', 'hello'] }
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }) }
let(:value) { ['01540', '01457', 'hello'] }
it { is_expected.to match({ id: champ.id, code_postal: '01540', value: '01457' }) }
end
context 'when the second element is not a valid insee code' do
let(:value) { ['01', 'totoro', 'hello'] }
let(:value) { ['01540', 'totoro', 'hello'] }
it { is_expected.to match(nil) }
end
end
context 'when the first element is not a valid departement code' do
context 'when the first element is not a valid postal code' do
let(:value) { ['totoro', '01457', 'hello'] }
it { is_expected.to match(nil) }
end

View file

@ -25,7 +25,7 @@ shared_examples "the user has got a prefilled dossier, owned by themselves" do
expect(page).to have_content(multiple_drop_down_list_values.last)
expect(page).to have_field(type_de_champ_epci.libelle, with: epci_value.last)
expect(page).to have_field(type_de_champ_dossier_link.libelle, with: dossier_link_value)
expect(page).to have_selector("input[value='Vonnas (01540)']")
expect(page).to have_field(commune_libelle, with: '01457')
expect(page).to have_content(annuaire_education_value.last)
expect(page).to have_content(address_value.last)
end

View file

@ -32,7 +32,8 @@ describe 'Prefilling a dossier (with a GET request):', js: true do
}
let(:epci_value) { ['01', '200029999'] }
let(:dossier_link_value) { '42' }
let(:commune_value) { ['01', '01457'] } # Vonnas (01540)
let(:commune_value) { ['01540', '01457'] } # Vonnas (01540)
let(:commune_libelle) { 'Vonnas (01540)' }
let(:address_value) { "20 Avenue de Ségur 75007 Paris" }
let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) }
let(:text_repetition_libelle) { sub_type_de_champs_repetition.first.libelle }
@ -78,13 +79,13 @@ describe 'Prefilling a dossier (with a GET request):', js: true do
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/associations.json'))
VCR.insert_cassette('api_geo_departements')
VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_communes_01')
VCR.insert_cassette('api_geo_epcis')
end
after do
VCR.eject_cassette('api_geo_departements')
VCR.eject_cassette('api_geo_communes')
VCR.eject_cassette('api_geo_communes_01')
VCR.eject_cassette('api_geo_epcis')
end

View file

@ -31,7 +31,8 @@ describe 'Prefilling a dossier (with a POST request):', js: true do
]
}
let(:epci_value) { ['01', '200029999'] }
let(:commune_value) { ['01', '01457'] } # Vonnas (01540)
let(:commune_value) { ['01540', '01457'] } # Vonnas (01540)
let(:commune_libelle) { 'Vonnas (01540)' }
let(:address_value) { "20 Avenue de Ségur 75007 Paris" }
let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) }
let(:text_repetition_libelle) { sub_type_de_champs_repetition.first.libelle }
@ -55,13 +56,13 @@ describe 'Prefilling a dossier (with a POST request):', js: true do
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/associations.json'))
VCR.insert_cassette('api_geo_departements')
VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_communes_01')
VCR.insert_cassette('api_geo_epcis')
end
after do
VCR.eject_cassette('api_geo_departements')
VCR.eject_cassette('api_geo_communes')
VCR.eject_cassette('api_geo_communes_01')
VCR.eject_cassette('api_geo_epcis')
end