fix(champs.rnf): some foundations was removed from the registry. lucky us [or good archi ? :D], we kept api data

This commit is contained in:
mfo 2024-09-16 11:29:12 +02:00
parent d8a0adc6ed
commit e2ec5118f4
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 47 additions and 20 deletions

View file

@ -9,7 +9,7 @@ module Maintenance
include Dry::Monads[:result]
def collection
Champs::RNFChamp.where(value_json: nil)
Champs::RNFChamp.where("external_id != null and data != null") # had been found
# Collection to be iterated over
# Must be Active Record Relation or Array
end
@ -23,8 +23,9 @@ module Maintenance
rescue ActiveRecord::RecordInvalid
# some champ might have dossier nil
end
else
# not found
else # fondation was removed, but we kept API data in data:, use it to restore stuff
champ.update_with_external_data!(data: champ.data.with_indifferent_access)
end
end

View file

@ -46,25 +46,51 @@ module Maintenance
subject(:process) { described_class.process(element) }
before do
allow_any_instance_of(Champs::RNFChamp).to receive(:fetch_external_data).and_return(Success(data))
context 'when api respond ok' do
before do
allow_any_instance_of(Champs::RNFChamp).to receive(:fetch_external_data).and_return(Success(data))
end
it 'updates value_json' do
expect { subject }.to change { element.reload.value_json }
.from(nil)
.to({
"street_number" => "16",
"street_name" => "Rue du Général de Boissieu",
"street_address" => "16 Rue du Général de Boissieu",
"postal_code" => "75015",
"city_name" => "Paris 15e Arrondissement",
"city_code" => "75115",
"departement_code" => "75",
"departement_name" => "Paris",
"region_code" => "11",
"region_name" => "Île-de-France"
})
end
end
it 'updates value_json' do
expect { subject }.to change { element.reload.value_json }
.from(nil)
.to({
"street_number" => "16",
"street_name" => "Rue du Général de Boissieu",
"street_address" => "16 Rue du Général de Boissieu",
"postal_code" => "75015",
"city_name" => "Paris 15e Arrondissement",
"city_code" => "75115",
"departement_code" => "75",
"departement_name" => "Paris",
"region_code" => "11",
"region_name" => "Île-de-France"
})
context 'when api respond KO' do
before do
element.update(data:)
allow_any_instance_of(Champs::RNFChamp).to receive(:fetch_external_data).and_return(Failure(code: 404, reason: :removed))
end
it 'updates value_json' do
expect { subject }.to change { element.reload.value_json }
.from(nil)
.to({
"street_number" => "16",
"street_name" => "Rue du Général de Boissieu",
"street_address" => "16 Rue du Général de Boissieu",
"postal_code" => "75015",
"city_name" => "Paris 15e Arrondissement",
"city_code" => "75115",
"departement_code" => "75",
"departement_name" => "Paris",
"region_code" => "11",
"region_name" => "Île-de-France"
})
end
end
end
end