From 437e871f798eab4a8edcc9da25d7a12b74431061 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Apr 2022 16:21:38 +0200 Subject: [PATCH 1/6] extract operation_logs_and_signatures method --- app/services/pieces_justificatives_service.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index ca5554518..a490c9bf5 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -146,13 +146,19 @@ class PiecesJustificativesService ].flatten.compact if !for_expert - bill_signatures = dossier.dossier_operation_logs.filter_map(&:bill_signature).uniq - pjs += [ - dossier.dossier_operation_logs.map(&:serialized), - bill_signatures.map(&:serialized), - bill_signatures.map(&:signature) - ].flatten.compact + pjs += operation_logs_and_signatures(dossier) end + pjs end + + def self.operation_logs_and_signatures(dossier) + bill_signatures = dossier.dossier_operation_logs.filter_map(&:bill_signature).uniq + + [ + dossier.dossier_operation_logs.map(&:serialized), + bill_signatures.map(&:serialized), + bill_signatures.map(&:signature) + ].flatten.compact + end end From 62e0553a4e57e8a00f77fedb677074e211fa0af2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Apr 2022 18:01:48 +0200 Subject: [PATCH 2/6] faster operation logs --- app/services/pieces_justificatives_service.rb | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index a490c9bf5..d4ddf1ed5 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -153,12 +153,21 @@ class PiecesJustificativesService end def self.operation_logs_and_signatures(dossier) - bill_signatures = dossier.dossier_operation_logs.filter_map(&:bill_signature).uniq + dol_ids_bill_id = DossierOperationLog + .where(dossier: dossier) + .pluck(:id, :bill_signature_id) - [ - dossier.dossier_operation_logs.map(&:serialized), - bill_signatures.map(&:serialized), - bill_signatures.map(&:signature) - ].flatten.compact + dol_ids = dol_ids_bill_id.map(&:first) + bill_ids = dol_ids_bill_id.map(&:second).uniq.compact + + serialized_dols = ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "DossierOperationLog", record_id: dol_ids) + + bill_docs = ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "BillSignature", record_id: bill_ids) + + serialized_dols + bill_docs end end From f2fea1f882297c325f2428248673aef19c623a6e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 4 Apr 2022 14:40:49 +0200 Subject: [PATCH 3/6] faster pjs_for_dossier --- app/services/pieces_justificatives_service.rb | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index d4ddf1ed5..075fe90eb 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -4,7 +4,7 @@ class PiecesJustificativesService pjs_commentaires = pjs_for_commentaires(dossier) pjs_dossier = pjs_for_dossier(dossier, for_expert) - pjs_champs + pjs_commentaires + pjs_dossier.filter(&:attached?) + pjs_champs + pjs_commentaires + pjs_dossier end def self.liste_pieces_justificatives(dossier) @@ -138,12 +138,9 @@ class PiecesJustificativesService end def self.pjs_for_dossier(dossier, for_expert = false) - pjs = [ - dossier.justificatif_motivation, - dossier.attestation&.pdf, - dossier.etablissement&.entreprise_attestation_sociale, - dossier.etablissement&.entreprise_attestation_fiscale - ].flatten.compact + pjs = motivation(dossier) + + attestation(dossier) + + etablissement(dossier) if !for_expert pjs += operation_logs_and_signatures(dossier) @@ -152,6 +149,30 @@ class PiecesJustificativesService pjs end + def self.etablissement(dossier) + etablissement = Etablissement.where(dossier: dossier) + + ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "Etablissement", record_id: etablissement) + end + + def self.motivation(dossier) + ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "Dossier", name: "justificatif_motivation", record_id: dossier) + end + + def self.attestation(dossier) + attestation = Attestation + .joins(:pdf_attachment) + .where(dossier: dossier) + + ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "Attestation", record_id: attestation) + end + def self.operation_logs_and_signatures(dossier) dol_ids_bill_id = DossierOperationLog .where(dossier: dossier) From 4c3b9979e919ea46f7446f55221e684d3d655f22 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 4 Apr 2022 17:02:02 +0200 Subject: [PATCH 4/6] fix test --- spec/services/procedure_archive_service_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/services/procedure_archive_service_spec.rb b/spec/services/procedure_archive_service_spec.rb index 2528114e9..cb7e62712 100644 --- a/spec/services/procedure_archive_service_spec.rb +++ b/spec/services/procedure_archive_service_spec.rb @@ -44,7 +44,7 @@ describe ProcedureArchiveService do let(:year) { 2021 } it 'collects files with success' do - allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io") + allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io") VCR.use_cassette('archive/new_file_to_get_200') do service.make_and_upload_archive(archive, instructeur) @@ -145,7 +145,7 @@ describe ProcedureArchiveService do let(:archive) { create(:archive, time_span_type: 'everything', status: 'pending', groupe_instructeurs: groupe_instructeurs) } it 'collect files' do - allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110") + allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110") VCR.use_cassette('archive/old_file_to_get_200') do service.make_and_upload_archive(archive, instructeur) From 1f98f75cccce93f04c1012e35edf2dbf4e5560c3 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 4 Apr 2022 17:36:17 +0200 Subject: [PATCH 5/6] remove unused method --- app/services/pieces_justificatives_service.rb | 7 ------- .../pieces_justificatives_service_spec.rb | 17 ----------------- 2 files changed, 24 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 075fe90eb..63192f350 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -7,13 +7,6 @@ class PiecesJustificativesService pjs_champs + pjs_commentaires + pjs_dossier end - def self.liste_pieces_justificatives(dossier) - pjs_champs = pjs_for_champs(dossier) - pjs_commentaires = pjs_for_commentaires(dossier) - - pjs_champs + pjs_commentaires - end - def self.serialize_types_de_champ_as_type_pj(revision) tdcs = revision.types_de_champ.filter { |type_champ| type_champ.old_pj.present? } tdcs.map.with_index do |type_champ, order_place| diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 32e46303d..8fbdd43eb 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -15,23 +15,6 @@ describe PiecesJustificativesService do create(:dossier_operation_log, dossier: dossier, bill_signature: bill_signature) end - describe '.liste_pieces_justificatives' do - subject { PiecesJustificativesService.liste_pieces_justificatives(dossier) } - - it "doesn't return sensitive documents like titre_identite" do - expect(champ_identite.piece_justificative_file).to be_attached - expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy - end - - it "doesn't return export pdf of the dossier" do - expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_falsy - end - - it "doesn't return operation logs of the dossier" do - expect(subject.any? { |piece| piece.name == 'serialized' }).to be_falsy - end - end - describe '.liste_documents' do subject { PiecesJustificativesService.liste_documents(dossier, false) } From bd0b88a4106ee15a8e725a7643500fcd2556a971 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 4 Apr 2022 17:45:10 +0200 Subject: [PATCH 6/6] move create_list_of_attachments --- app/lib/active_storage/downloadable_file.rb | 6 ++++++ app/services/procedure_archive_service.rb | 9 ++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index eca681943..8c3855ad4 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -11,6 +11,12 @@ class ActiveStorage::DownloadableFile end end + def self.create_list_from_dossiers(dossiers) + dossiers.flat_map do |dossier| + create_list_from_dossier(dossier) + end + end + private def self.timestamped_filename(attachment) diff --git a/app/services/procedure_archive_service.rb b/app/services/procedure_archive_service.rb index 9f244c06c..31f0786f6 100644 --- a/app/services/procedure_archive_service.rb +++ b/app/services/procedure_archive_service.rb @@ -25,7 +25,8 @@ class ProcedureArchiveService dossiers.processed_in_month(archive.month) end - attachments = create_list_of_attachments(dossiers) + attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers) + download_and_zip(archive, attachments) do |zip_filepath| ArchiveUploader.new(procedure: @procedure, archive: archive, filepath: zip_filepath) .upload @@ -72,12 +73,6 @@ class ProcedureArchiveService "procedure-#{@procedure.id}-#{archive.id}" end - def create_list_of_attachments(dossiers) - dossiers.flat_map do |dossier| - ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) - end - end - def self.attachments_from_champs_piece_justificative(champs) champs .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }