From 06b66636623a32e78c06c7c6eebad4e7023e4386 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 15 May 2023 12:54:20 +0200 Subject: [PATCH] import more stuff --- app/lib/recovery/exporter.rb | 11 ++- app/lib/recovery/importer.rb | 88 +++++++++++++++++++++- app/models/dossier_preloader.rb | 4 +- spec/lib/recovery/exporter_spec.rb | 6 +- spec/lib/recovery/importer_spec.rb | 4 +- spec/lib/recovery/life_cycle_spec.rb | 106 ++++++++++++++++++++++++++- 6 files changed, 205 insertions(+), 14 deletions(-) diff --git a/app/lib/recovery/exporter.rb b/app/lib/recovery/exporter.rb index e397feb82..0c5946b34 100644 --- a/app/lib/recovery/exporter.rb +++ b/app/lib/recovery/exporter.rb @@ -7,7 +7,15 @@ module Recovery dossier_with_data = Dossier.where(id: dossier_ids) .preload(:user, :individual, - :etablissement, + :invites, + :traitements, + :transfer_logs, + commentaires: { piece_jointe_attachment: :blob }, + avis: { introduction_file_attachment: :blob, piece_justificative_file_attachment: :blob }, + dossier_operation_logs: { serialized_attachment: :blob }, + attestation: { pdf_attachment: :blob }, + justificatif_motivation_attachment: :blob, + etablissement: :exercices, revision: :procedure) @dossiers = DossierPreloader.new(dossier_with_data).all @file_path = file_path @@ -18,4 +26,3 @@ module Recovery end end end - diff --git a/app/lib/recovery/importer.rb b/app/lib/recovery/importer.rb index c2f2543b8..913641394 100644 --- a/app/lib/recovery/importer.rb +++ b/app/lib/recovery/importer.rb @@ -9,9 +9,93 @@ module Recovery def load @dossiers.map do |dossier| dossier.instance_variable_set :@new_record, true - dossier.save! + + Dossier.insert(dossier.attributes) + + Etablissement.insert(dossier.etablissement.attributes) + if dossier.etablissement.present? + APIEntreprise::EntrepriseJob.perform_later(dossier.etablissement.id, dossier.procedure.id) + end + + Individual.insert(dossier.individual.attributes) + + dossier.invites.each do |invite| + Invite.insert(invite.attributes) + end + + dossier.traitements.each do |traitement| + Traitement.insert(traitement.attributes) + end + + dossier.transfer_logs.each do |transfer| + DossierTransferLog.insert(transfer.attributes) + end + + dossier.etablissement.exercices.each do |exercice| + Exercice.insert(exercice.attributes) + end + + dossier.commentaires.each do |commentaire| + Commentaire.insert(commentaire.attributes) + if commentaire.piece_jointe.attached? + import(commentaire.piece_jointe) + end + end + + dossier.avis.each do |avis| + Avis.insert(avis.attributes) + + if avis.introduction_file.attached? + import(avis.introduction_file) + end + + if avis.piece_justificative_file.attached? + import(avis.piece_justificative_file) + end + end + + dossier.dossier_operation_logs.each do |dol| + DossierOperationLog.insert(dol.attributes) + + if dol.serialized.attached? + import(dol.serialized) + end + end + + if dossier.attestation.present? + Attestation.insert(dossier.attestation.attributes) + import(dossier.attestation.pdf) + end + + if dossier.justificatif_motivation.attached? + import(dossier.justificatif_motivation) + end + + dossier.champs.each do |champ| + champ.piece_justificative_file.each { |pj| import(pj) } + + if champ.etablissement.present? + APIEntreprise::EntrepriseJob.perform_later(champ.etablissement.id, dossier.procedure.id) + + champ.etablissement.exercices.each do |exercice| + Exercice.insert(exercice.attributes) + end + + Etablissement.insert(champ.etablissement.attributes) + end + + Champ.insert(champ.attributes) + + if champ.geo_areas.present? + champ.geo_areas.each { GeoArea.insert(_1.attributes) } + end + end end end + + def import(pj) + ActiveStorage::Blob.insert(pj.blob.attributes) + ActiveStorage::Attachment.insert(pj.attributes) + end end end - diff --git a/app/models/dossier_preloader.rb b/app/models/dossier_preloader.rb index 61e147492..69eedc82e 100644 --- a/app/models/dossier_preloader.rb +++ b/app/models/dossier_preloader.rb @@ -35,7 +35,7 @@ class DossierPreloader end def load_dossiers(dossiers, pj_template: false) - to_include = [piece_justificative_file_attachments: :blob] + to_include = [:geo_areas, piece_justificative_file_attachments: :blob, etablissement: :exercices] if pj_template to_include << { type_de_champ: { piece_justificative_template_attachment: :blob } } @@ -65,7 +65,7 @@ class DossierPreloader def load_etablissements(champs) champs_siret = champs.filter(&:siret?) - etablissements_by_id = Etablissement.where(id: champs_siret.map(&:etablissement_id).compact).index_by(&:id) + etablissements_by_id = Etablissement.includes(:exercices).where(id: champs_siret.map(&:etablissement_id).compact).index_by(&:id) champs_siret.each do |champ| etablissement = etablissements_by_id[champ.etablissement_id] champ.association(:etablissement).target = etablissement diff --git a/spec/lib/recovery/exporter_spec.rb b/spec/lib/recovery/exporter_spec.rb index d336f100d..d5a23b5e4 100644 --- a/spec/lib/recovery/exporter_spec.rb +++ b/spec/lib/recovery/exporter_spec.rb @@ -5,7 +5,7 @@ describe Recovery::Exporter do def cleanup_export_file # if File.exist?(fp) - # FileUtils.rm(fp) + # FileUtils.rm(fp) # end end @@ -13,11 +13,11 @@ describe Recovery::Exporter do after { cleanup_export_file } it 'exports dossiers to .dump' do - expect{ subject }.not_to raise_error + expect { subject }.not_to raise_error end it 'exports dossiers local file .dump' do - expect{ subject }.to change { File.exist?(fp) } + expect { subject }.to change { File.exist?(fp) } .from(false).to(true) end diff --git a/spec/lib/recovery/importer_spec.rb b/spec/lib/recovery/importer_spec.rb index e0579d341..7e5f3678d 100644 --- a/spec/lib/recovery/importer_spec.rb +++ b/spec/lib/recovery/importer_spec.rb @@ -3,7 +3,7 @@ describe Recovery::Importer do let(:importer) { Recovery::Importer.new(file_path:) } subject { importer.load } context 'loaded_data' do - let(:loaded_dossiers) { importer.dossiers} + let(:loaded_dossiers) { importer.dossiers } it 'contains user' do expect(loaded_dossiers.first.user).to be_an_instance_of(User) @@ -11,6 +11,6 @@ describe Recovery::Importer do end it 're-import dossiers from .dump' do - expect{ subject }.to change { Dossier.count }.by(importer.dossiers.size) + 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 index 8dc2318a2..c5e752bc5 100644 --- a/spec/lib/recovery/life_cycle_spec.rb +++ b/spec/lib/recovery/life_cycle_spec.rb @@ -1,8 +1,108 @@ 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} + let(:procedure) do + create(:procedure, + types_de_champ_public: [ + { type: :repetition, children: [{ type: :piece_justificative }] }, + { type: :carte }, + { type: :siret } + ]) + end + + let(:some_file) { Rack::Test::UploadedFile.new('spec/fixtures/files/white.png', 'image/png') } + let(:geo_area) { build(:geo_area, :selection_utilisateur, :polygon) } + + let(:dossier) do + d = create(:dossier, procedure:) + + repetition(d).add_row(d.revision) + pj_champ(d).piece_justificative_file.attach(some_file) + carte(d).update(geo_areas: [geo_area]) + d.etablissement = create(:etablissement, :with_exercices) + d.etablissement.entreprise_attestation_sociale.attach(some_file) + d.etablissement.entreprise_attestation_fiscale.attach(some_file) + + siret(d).update(etablissement: create(:etablissement, :with_exercices)) + siret(d).etablissement.entreprise_attestation_sociale.attach(some_file) + siret(d).etablissement.entreprise_attestation_fiscale.attach(some_file) + + d.individual = build(:individual) + + d.attestation = build(:attestation, :with_pdf) + d.justificatif_motivation.attach(some_file) + + d.commentaires << build(:commentaire, :with_file) + + d.invites << build(:invite, :with_user) + + d.avis << build(:avis, :with_introduction, :with_piece_justificative) + + d.traitements.accepter(motivation: 'oui', processed_at: Time.zone.now) + d.save + + d.dossier_operation_logs << build(:dossier_operation_log, :with_serialized) + + d.transfer_logs.create(from: create(:user), to: create(:user)) + + d + end + + def repetition(d) = d.champs.find_by(type: "Champs::RepetitionChamp") + def pj_champ(d) = d.champs.find_by(type: "Champs::PieceJustificativeChamp") + def carte(d) = d.champs.find_by(type: "Champs::CarteChamp") + def siret(d) = d.champs.find_by(type: "Champs::SiretChamp") + + let(:instructeur) { create(:instructeur) } + + before do + instructeur.followed_dossiers << dossier + end + + it 'reloads the full grappe' do + expect(Dossier.count).to eq(1) + expect(Dossier.first.champs.count).not_to be(0) + + Recovery::LifeCycle.new(dossier_ids: [dossier.id]).load_export_destroy_and_import + + expect(Dossier.count).to eq(1) + + reloaded_dossier = Dossier.first + + expect(reloaded_dossier.champs.count).not_to be(0) + + expect(repetition(reloaded_dossier).champs.map(&:type)).to match_array(["Champs::PieceJustificativeChamp"]) + expect(pj_champ(reloaded_dossier).piece_justificative_file).to be_attached + expect(carte(reloaded_dossier).geo_areas).to be_present + + expect(reloaded_dossier.etablissement.exercices).to be_present + + # launch a job + # expect(reloaded_dossier.etablissement.entreprise_attestation_sociale).to be_attached + # expect(reloaded_dossier.etablissement.entreprise_attestation_fiscale).to be_attached + + expect(siret(reloaded_dossier).etablissement.exercices).to be_present + + # launch a job + # expect(siret(reloaded_dossier).etablissement.entreprise_attestation_sociale).to be_attached + # expect(siret(reloaded_dossier).etablissement.entreprise_attestation_fiscale).to be_attached + + expect(reloaded_dossier.individual).to be_present + expect(reloaded_dossier.attestation.pdf).to be_attached + expect(reloaded_dossier.justificatif_motivation).to be_attached + + expect(reloaded_dossier.commentaires.first.piece_jointe).to be_attached + + expect(reloaded_dossier.invites.first.user).to be_present + expect(reloaded_dossier.followers_instructeurs).to match_array([instructeur]) + + expect(reloaded_dossier.avis.first.introduction_file).to be_attached + expect(reloaded_dossier.avis.first.piece_justificative_file).to be_attached + + expect(reloaded_dossier.traitements).to be_present + + expect(reloaded_dossier.dossier_operation_logs.first.serialized).to be_attached + + expect(reloaded_dossier.transfer_logs).to be_present end end end