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 class Champs::EpciChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_region store_accessor :value_json, :code_departement, :code_region
before_validation :on_departement_change before_validation :on_departement_change
before_validation :on_epci_name_changes
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? } validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.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) errors.add(:value, :not_in_departement_epci_names)
end 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 end

View file

@ -110,10 +110,10 @@ describe Champs::EpciChamp, type: :model do
it { is_expected.to be_valid } it { is_expected.to be_valid }
end end
context 'when value is empty' do context 'when value is in departement epci names' do
let(:value) { '' } let(:value) { 'CA Haut - Bugey Agglomération' }
it { is_expected.not_to be_valid } it { is_expected.to be_valid }
end end
context 'when value is in departement epci names' do context 'when value is in departement epci names' do
@ -122,10 +122,22 @@ describe Champs::EpciChamp, type: :model do
it { is_expected.to be_valid } it { is_expected.to be_valid }
end end
context 'when value is not in departement epci names' do context 'when epci name had been renamed' do
let(:value) { 'totoro' } let(:value) { 'totoro' }
it { is_expected.not_to be_valid } it 'is valid and updates its own value' do
expect(subject).to be_valid
expect(subject.value).to eq('CA Haut - Bugey Agglomération')
end
end
context 'when value is not in departement epci names nor in departement epci codes' do
let(:value) { 'totoro' }
it 'is invalid' do
allow(APIGeoService).to receive(:epcis).with(champ.code_departement).and_return([])
expect(subject).not_to be_valid
end
end end
end end
end end