import more stuff

This commit is contained in:
simon lehericey 2023-05-15 12:54:20 +02:00 committed by LeSim
parent 6f41f0e1c6
commit 06b6663662
6 changed files with 205 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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