Merge pull request #9057 from demarches-simplifiees/US/restore-dossier-kc

amelioration(recovery:import): meilleure log et gestion de edge cases
This commit is contained in:
Paul Chavard 2023-05-16 17:01:07 +00:00 committed by GitHub
commit 634257e242
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -9,10 +9,20 @@ module Recovery
end end
def load def load
@dossiers.map do |dossier| @dossiers.each do |dossier|
if Dossier.exists?(dossier.id)
puts "Dossier #{dossier.id} already exists"
next
end
dossier.instance_variable_set :@new_record, true dossier.instance_variable_set :@new_record, true
dossier_attributes = dossier.attributes.dup
Dossier.insert(dossier.attributes) parent_dossier_id = dossier_attributes['parent_dossier_id']
if parent_dossier_id && !Dossier.exists?(id: parent_dossier_id)
dossier_attributes.delete('parent_dossier_id')
end
Dossier.insert(dossier_attributes)
if dossier.etablissement.present? if dossier.etablissement.present?
Etablissement.insert(dossier.etablissement.attributes) Etablissement.insert(dossier.etablissement.attributes)
@ -98,7 +108,7 @@ module Recovery
champ.geo_areas.each { GeoArea.insert(_1.attributes) } champ.geo_areas.each { GeoArea.insert(_1.attributes) }
end end
end end
puts "imported dossier: #{dossier.id}" puts "imported dossier #{dossier.id}: #{Dossier.exists?(dossier.id)}"
end end
end end

View file

@ -115,5 +115,18 @@ describe 'Recovery::LifeCycle' do
expect(reloaded_dossier.transfer_logs).to be_present expect(reloaded_dossier.transfer_logs).to be_present
end end
it 'skip parent_dossier_id when dossier does not exists any more' do
parent = create(:dossier)
dossier.update!(parent_dossier_id: parent.id)
@dossier_ids = [dossier.id]
Recovery::Exporter.new(dossier_ids: @dossier_ids, file_path: fp).dump
Dossier.where(id: @dossier_ids).destroy_all
parent.destroy
Recovery::Importer.new(file_path: fp).load
expect(Dossier.count).to eq(1)
end
end end
end end