From 9c73bf193281d4afb4b3714082f80f66f6d3320b Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 7 Apr 2022 15:23:18 +0200 Subject: [PATCH] batch it --- app/lib/active_storage/downloadable_file.rb | 5 ++- app/services/pieces_justificatives_service.rb | 36 +++++++++++-------- .../pieces_justificatives_service_spec.rb | 2 +- .../services/procedure_export_service_spec.rb | 3 +- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index 3f38007be..ac1cb2a8d 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -1,8 +1,7 @@ class ActiveStorage::DownloadableFile def self.create_list_from_dossiers(dossiers, for_expert = false) - dossiers - .map { |d| pj_and_path(d.id, PiecesJustificativesService.generate_dossier_export(d)) } + - PiecesJustificativesService.liste_documents(dossiers, for_expert) + PiecesJustificativesService.generate_dossier_export(dossiers) + + PiecesJustificativesService.liste_documents(dossiers, for_expert) end private diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 1e0642e82..30743ad20 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -107,21 +107,29 @@ class PiecesJustificativesService end end - def self.generate_dossier_export(dossier) - pdf = ApplicationController - .render(template: 'dossiers/show', formats: [:pdf], - assigns: { - include_infos_administration: true, - dossier: dossier - }) + def self.generate_dossier_export(dossiers) + pdfs = [] - FakeAttachment.new( - file: StringIO.new(pdf), - filename: "export-#{dossier.id}.pdf", - name: 'pdf_export_for_instructeur', - id: dossier.id, - created_at: dossier.updated_at - ) + dossiers.find_each do |dossier| + pdf = ApplicationController + .render(template: 'dossiers/show', formats: [:pdf], + assigns: { + include_infos_administration: true, + dossier: dossier + }) + + a = FakeAttachment.new( + file: StringIO.new(pdf), + filename: "export-#{dossier.id}.pdf", + name: 'pdf_export_for_instructeur', + id: dossier.id, + created_at: dossier.updated_at + ) + + pdfs << ActiveStorage::DownloadableFile.pj_and_path(dossier.id, a) + end + + pdfs end private diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index a7b715786..a262d6817 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -192,7 +192,7 @@ describe PiecesJustificativesService do describe '.generate_dossier_export' do let(:dossier) { create(:dossier) } - subject { PiecesJustificativesService.generate_dossier_export(dossier) } + subject { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier.id)) } it "doesn't update dossier" do expect { subject }.not_to change { dossier.updated_at } diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index cda5a864f..0422bdfa7 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -437,6 +437,7 @@ describe ProcedureExportService do context 'with files (and http calls)' do let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) } + let(:dossier_exports) { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier)) } before do allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io") @@ -454,7 +455,7 @@ describe ProcedureExportService do "#{service.send(:base_filename)}/dossier-#{dossier.id}/", "#{service.send(:base_filename)}/dossier-#{dossier.id}/pieces_justificatives/", "#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}", - "#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(PiecesJustificativesService.generate_dossier_export(dossier))}" + "#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}" ] expect(files.size).to eq(structure.size) expect(files.map(&:filename)).to match_array(structure)