From 32088512fffe777063e7fd1efa8ad3c69d207d42 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 16 May 2023 16:33:21 +0200 Subject: [PATCH] amelioration(importer): evite de re-importer un dossier pointant vers un parent qui est maintenant detruit --- app/lib/recovery/importer.rb | 8 +++++++- spec/lib/recovery/life_cycle_spec.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/lib/recovery/importer.rb b/app/lib/recovery/importer.rb index cb8f6500b..09b972031 100644 --- a/app/lib/recovery/importer.rb +++ b/app/lib/recovery/importer.rb @@ -11,8 +11,14 @@ module Recovery def load @dossiers.map do |dossier| 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? Etablissement.insert(dossier.etablissement.attributes) diff --git a/spec/lib/recovery/life_cycle_spec.rb b/spec/lib/recovery/life_cycle_spec.rb index 8209ada1f..a0b8354b4 100644 --- a/spec/lib/recovery/life_cycle_spec.rb +++ b/spec/lib/recovery/life_cycle_spec.rb @@ -115,5 +115,18 @@ describe 'Recovery::LifeCycle' do expect(reloaded_dossier.transfer_logs).to be_present 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