feat(commune): chose commune by postal code

This commit is contained in:
Paul Chavard 2023-03-21 14:44:03 +01:00
parent a4e96d5256
commit dc605015b3
7 changed files with 121 additions and 25 deletions

View file

@ -1,2 +1,17 @@
class EditableChamp::CommunesComponent < EditableChamp::ComboSearchComponent class EditableChamp::CommunesComponent < EditableChamp::EditableChampBaseComponent
include ApplicationHelper
private
def commune_options
@champ.communes.map { ["#{_1[:name]} (#{_1[:postal_code]})", _1[:code]] }
end
def code_postal_input_id
"#{@champ.input_id}-code_postal"
end
def commune_select_options
{ selected: @champ.selected }.merge(@champ.mandatory? ? { prompt: '' } : { include_blank: '' })
end
end end

View file

@ -1,12 +1,13 @@
- render_parent %label.notice{ for: code_postal_input_id } Code postal de la commune
= @form.hidden_field :value = @form.text_field :code_postal, required: @champ.required?, id: code_postal_input_id, class: "width-33-desktop width-100-mobile small-margin"
= @form.hidden_field :external_id - if @champ.code_postal_with_fallback?
= @form.hidden_field :departement - if commune_options.empty?
= @form.hidden_field :code_departement .fr-error-text.mb-4= "Aucune commune trouvée pour le code postal #{@champ.code_postal_with_fallback}"
= react_component("ComboCommunesSearch", - elsif commune_options.size <= 3
required: @champ.required?, %fieldset.radios
id: @champ.input_id, - commune_options.each.with_index do |(option, value), index|
classNameDepartement: "width-33-desktop width-100-mobile", %label
className: "width-66-desktop width-100-mobile", = @form.radio_button :value, value, checked: @champ.selected == value, id: index == 0 ? @champ.input_id : nil
describedby: @champ.describedby_id, = option
**react_combo_props) - else
= @form.select :value, commune_options, commune_select_options, required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop width-100-mobile"

View file

@ -326,8 +326,8 @@ module Instructeurs
def champs_private_params def champs_private_params
champs_params = params.require(:dossier).permit(champs_private_attributes: [ champs_params = params.require(:dossier).permit(champs_private_attributes: [
:id, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :departement, :code_departement, :value, value: [], :id, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :code_departement, :value, value: [],
champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :departement, :code_departement, value: []] champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :code_departement, value: []]
]) ])
champs_params[:champs_private_all_attributes] = champs_params.delete(:champs_private_attributes) || {} champs_params[:champs_private_all_attributes] = champs_params.delete(:champs_private_attributes) || {}
champs_params champs_params

View file

@ -387,7 +387,7 @@ module Users
def champs_public_params def champs_public_params
champs_params = params.require(:dossier).permit(champs_public_attributes: [ champs_params = params.require(:dossier).permit(champs_public_attributes: [
:id, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :identifiant, :numero_fiscal, :reference_avis, :ine, :piece_justificative_file, :departement, :code_departement, value: [], :id, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :identifiant, :numero_fiscal, :reference_avis, :ine, :piece_justificative_file, :code_departement, value: [],
champs_attributes: [:id, :_destroy, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :identifiant, :numero_fiscal, :reference_avis, :ine, :piece_justificative_file, :departement, :code_departement, value: []] champs_attributes: [:id, :_destroy, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :identifiant, :numero_fiscal, :reference_avis, :ine, :piece_justificative_file, :departement, :code_departement, value: []]
]) ])
champs_params[:champs_public_all_attributes] = champs_params.delete(:champs_public_attributes) || {} champs_params[:champs_public_all_attributes] = champs_params.delete(:champs_public_attributes) || {}

View file

@ -21,30 +21,110 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::CommuneChamp < Champs::TextChamp class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :departement, :code_departement store_accessor :value_json, :code_departement, :code_postal
before_validation :on_code_postal_change
def for_export def for_export
[value, external_id, departement? ? departement_code_and_name : ''] [name, code, departement? ? departement_code_and_name : '']
end end
def name_departement def departement_name
# FIXME we originaly saved already formatted departement with the code in the name APIGeoService.departement_name(code_departement)
departement&.gsub(/^(.[0-9])\s-\s/, '')
end end
def departement_code_and_name def departement_code_and_name
"#{code_departement} - #{name_departement}" "#{code_departement} #{departement_name}"
end
def departement
{ code: code_departement, name: departement_name }
end end
def departement? def departement?
departement.present? code_departement.present?
end end
def code? def code?
code.present? code.present?
end end
def code_postal?
code_postal.present?
end
def name
if code?
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal_with_fallback})"
else
value
end
end
def to_s
name
end
def code def code
external_id external_id
end end
def selected
code
end
def communes
if code_postal_with_fallback?
APIGeoService.communes_by_postal_code(code_postal_with_fallback)
else
[]
end
end
def value=(code)
if code.blank? || !code_postal_with_fallback?
self.code_departement = nil
self.external_id = nil
super(nil)
else
commune = communes.find { _1[:code] == code }
if commune.present?
self.code_departement = commune[:departement_code]
self.external_id = commune[:code]
super(commune[:name])
else
self.code_departement = nil
self.external_id = nil
super(nil)
end
end
end
def code_postal_with_fallback?
code_postal_with_fallback.present?
end
# We try to extract the postal code from the value, which is the name of the commune and the
# postal code in brackets. This is temporary until we do a full data migration.
def code_postal_with_fallback
if code_postal?
code_postal
elsif value.present?
match = value.match(/[^(]\(([^\)]*)\)$/)
match[1] if match.present?
else
nil
end
end
private
def on_code_postal_change
if code_postal_changed?
if communes.one?
self.value = communes.first[:code]
else
self.value = nil
end
end
end
end end

View file

@ -514,7 +514,7 @@ class TypeDeChamp < ApplicationRecord
def self.refresh_after_update?(type_champ) def self.refresh_after_update?(type_champ)
case type_champ case type_champ
when type_champs.fetch(:epci) when type_champs.fetch(:epci), type_champs.fetch(:communes)
true true
else else
false false

View file

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