diff --git a/app/lib/recovery/exporter.rb b/app/lib/recovery/exporter.rb new file mode 100644 index 000000000..e397feb82 --- /dev/null +++ b/app/lib/recovery/exporter.rb @@ -0,0 +1,21 @@ +module Recovery + class Exporter + FILE_PATH = Rails.root.join('lib', 'data', 'export.dump') + + attr_reader :dossiers + def initialize(dossier_ids:, file_path: FILE_PATH) + dossier_with_data = Dossier.where(id: dossier_ids) + .preload(:user, + :individual, + :etablissement, + revision: :procedure) + @dossiers = DossierPreloader.new(dossier_with_data).all + @file_path = file_path + end + + def dump + File.open(@file_path, 'wb') { _1.write(Marshal.dump(@dossiers)) } + end + end +end + diff --git a/app/lib/recovery/importer.rb b/app/lib/recovery/importer.rb new file mode 100644 index 000000000..c2f2543b8 --- /dev/null +++ b/app/lib/recovery/importer.rb @@ -0,0 +1,17 @@ +module Recovery + class Importer + attr_reader :dossiers + + def initialize(file_path: Recovery::Exporter::FILE_PATH) + @dossiers = Marshal.load(File.read(file_path)) + end + + def load + @dossiers.map do |dossier| + dossier.instance_variable_set :@new_record, true + dossier.save! + end + end + end +end + diff --git a/app/lib/recovery/life_cycle.rb b/app/lib/recovery/life_cycle.rb new file mode 100644 index 000000000..ed2bf83e4 --- /dev/null +++ b/app/lib/recovery/life_cycle.rb @@ -0,0 +1,33 @@ +module Recovery + class LifeCycle + def initialize(dossier_ids:) + @dossier_ids = dossier_ids + end + + def load_export_destroy_and_import + export_dossiers + destroy_dossiers + import_dossiers + end + + def exporter + @exporter ||= Recovery::Exporter.new(dossier_ids: @dossier_ids) + end + + def importer + @importer ||= Importer.new() + end + + def export_dossiers + exporter.dump + end + + def destroy_dossiers + Dossier.where(id: @dossier_ids).destroy_all + end + + def import_dossiers + importer.load + end + end +end diff --git a/spec/fixtures/recovery/export.dump b/spec/fixtures/recovery/export.dump new file mode 100644 index 000000000..983cf88b9 Binary files /dev/null and b/spec/fixtures/recovery/export.dump differ diff --git a/spec/lib/recovery/exporter_spec.rb b/spec/lib/recovery/exporter_spec.rb new file mode 100644 index 000000000..d336f100d --- /dev/null +++ b/spec/lib/recovery/exporter_spec.rb @@ -0,0 +1,48 @@ +describe Recovery::Exporter do + let(:dossier_ids) { [create(:dossier, :with_individual).id, create(:dossier, :with_individual).id] } + let(:fp) { Rails.root.join('spec', 'fixtures', 'recovery', 'export.dump') } + subject { Recovery::Exporter.new(dossier_ids:, file_path: fp).dump } + + def cleanup_export_file + # if File.exist?(fp) + # FileUtils.rm(fp) + # end + end + + before { cleanup_export_file } + after { cleanup_export_file } + + it 'exports dossiers to .dump' do + expect{ subject }.not_to raise_error + end + + it 'exports dossiers local file .dump' do + expect{ subject }.to change { File.exist?(fp) } + .from(false).to(true) + end + + context 'exported' do + before { subject } + let(:exported_dossiers) { Marshal.load(File.read(fp)) } + + it 'contains as much as dossiers as input' do + expect(exported_dossiers.size).to eq(dossier_ids.size) + end + + it 'contains input dossier ids' do + expect(exported_dossiers.map(&:id)).to match_array(dossier_ids) + end + + it 'contains procedure dossier ids' do + expect(exported_dossiers.first.procedure).to be_an_instance_of(Procedure) + end + + it 'contains dossier.revision ids' do + expect(exported_dossiers.first.revision).to be_an_instance_of(ProcedureRevision) + end + + it 'contains dossier.user' do + expect(exported_dossiers.first.user).to be_an_instance_of(User) + end + end +end diff --git a/spec/lib/recovery/importer_spec.rb b/spec/lib/recovery/importer_spec.rb new file mode 100644 index 000000000..e0579d341 --- /dev/null +++ b/spec/lib/recovery/importer_spec.rb @@ -0,0 +1,16 @@ +describe Recovery::Importer do + let(:file_path) { Rails.root.join('spec', 'fixtures', 'recovery', 'export.dump') } + let(:importer) { Recovery::Importer.new(file_path:) } + subject { importer.load } + context 'loaded_data' do + let(:loaded_dossiers) { importer.dossiers} + + it 'contains user' do + expect(loaded_dossiers.first.user).to be_an_instance_of(User) + end + end + + it 're-import dossiers from .dump' do + expect{ subject }.to change { Dossier.count }.by(importer.dossiers.size) + end +end diff --git a/spec/lib/recovery/life_cycle_spec.rb b/spec/lib/recovery/life_cycle_spec.rb new file mode 100644 index 000000000..8dc2318a2 --- /dev/null +++ b/spec/lib/recovery/life_cycle_spec.rb @@ -0,0 +1,8 @@ +describe Recovery::LifeCycle do + describe '.load_export_destroy_and_import' do + it 'works with one dossier' do + dossier = create(:dossier, :with_individual) + expect { Recovery::LifeCycle.new(dossier_ids: [dossier.id]).load_export_destroy_and_import }.not_to change {Dossier.count} + end + end +end