diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index 15b796aa5..e42c19b54 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -4,11 +4,12 @@ class ActiveStorage::DownloadableFile def self.create_list_from_dossiers(dossiers:, user_profile:, export_template: nil) pj_service = PiecesJustificativesService.new(user_profile:, export_template:) - dossiers = dossiers - .includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert], revision: [:revision_types_de_champ, :types_de_champ_public, :types_de_champ_private]) + files = [] + DossierPreloader.new(dossiers).in_batches_with_block do |loaded_dossiers| + files += pj_service.generate_dossiers_export(loaded_dossiers) + pj_service.liste_documents(loaded_dossiers) + end - loaded_dossiers = DossierPreloader.new(dossiers).in_batches - pj_service.generate_dossiers_export(loaded_dossiers) + pj_service.liste_documents(loaded_dossiers) + files end def self.cleanup_list_from_dossier(files) diff --git a/app/models/dossier_preloader.rb b/app/models/dossier_preloader.rb index 28cb4b769..c1a3e614e 100644 --- a/app/models/dossier_preloader.rb +++ b/app/models/dossier_preloader.rb @@ -13,6 +13,16 @@ class DossierPreloader dossiers end + def in_batches_with_block(size = DEFAULT_BATCH_SIZE, &block) + @dossiers.in_batches(of: size) do |batch| + data = Dossier.where(id: batch.ids).includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert], revision: :revision_types_de_champ) + + dossiers = data.to_a + load_dossiers(dossiers) + yield(dossiers) + end + end + def all(pj_template: false) dossiers = @dossiers.to_a load_dossiers(dossiers, pj_template:)