fix(champs.epci): epci names change over time. remap when it occurs

This commit is contained in:
Martin 2023-12-26 16:36:53 +01:00
parent e52bd9d6bf
commit fa07c21ec3
2 changed files with 27 additions and 5 deletions

View file

@ -1,6 +1,7 @@
class Champs::EpciChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_region
before_validation :on_departement_change
before_validation :on_epci_name_changes
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? }
@ -99,4 +100,13 @@ class Champs::EpciChamp < Champs::TextChamp
errors.add(:value, :not_in_departement_epci_names)
end
def on_epci_name_changes
return if external_id.nil? || code_departement.nil?
return if value.in?(APIGeoService.epcis(code_departement).pluck(:name))
if external_id.in?(APIGeoService.epcis(code_departement).pluck(:code))
self.value = (external_id)
end
end
end