diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index d2902ebd8..de116e651 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -9,7 +9,7 @@ class ActiveStorage::DownloadableFile include_avis_for_expert: false ) PiecesJustificativesService.generate_dossier_export(dossiers, include_infos_administration:, include_avis_for_expert:) + - PiecesJustificativesService.liste_documents(dossiers, with_bills:, with_champs_private:) + PiecesJustificativesService.liste_documents(dossiers, with_bills:, with_champs_private:, with_avis_piece_justificative: include_infos_administration) end def self.cleanup_list_from_dossier(files) @@ -79,6 +79,8 @@ class ActiveStorage::DownloadableFile 'horodatage/' when 'Commentaire' 'messagerie/' + when 'Avis' + 'avis/' else 'pieces_justificatives/' end diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index cff6d8548..c3a496755 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -1,11 +1,15 @@ class PiecesJustificativesService - def self.liste_documents(dossiers, with_bills:, with_champs_private:) + def self.liste_documents(dossiers, + with_bills:, + with_champs_private:, + with_avis_piece_justificative:) bill_ids = [] docs = dossiers.in_batches.flat_map do |batch| pjs = pjs_for_champs(batch, with_champs_private:) + pjs_for_commentaires(batch) + - pjs_for_dossier(batch) + pjs_for_dossier(batch) + + pjs_for_avis(batch, with_avis_piece_justificative:) if with_bills # some bills are shared among operations @@ -206,6 +210,22 @@ class PiecesJustificativesService end end + def self.pjs_for_avis(dossiers, with_avis_piece_justificative:) + avis_ids_dossier_id_query = Avis.joins(:dossier) + .where(dossier: dossiers) + avis_ids_dossier_id_query = avis_ids_dossier_id_query.where(confidentiel: false) if !with_avis_piece_justificative + avis_ids_dossier_id = avis_ids_dossier_id_query.pluck(:id, :dossier_id).to_h + + ActiveStorage::Attachment + .includes(:blob) + .where(record_type: "Avis", name: "piece_justificative_file", record_id: avis_ids_dossier_id.keys) + .filter { |a| safe_attachment(a) } + .map do |a| + dossier_id = avis_ids_dossier_id[a.record_id] + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end + end + def self.operation_logs_and_signature_ids(dossiers) dol_id_dossier_id_bill_id = DossierOperationLog .where(dossier: dossiers, data: nil) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index ea6de277f..5555e097e 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -2,10 +2,11 @@ describe PiecesJustificativesService do describe '.liste_documents' do let(:with_champs_private) { true } let(:with_bills) { true } + let(:with_avis_piece_justificative) { true } subject do PiecesJustificativesService - .liste_documents(Dossier.where(id: dossier.id), with_bills:, with_champs_private:) + .liste_documents(Dossier.where(id: dossier.id), with_bills:, with_champs_private:, with_avis_piece_justificative:) .map(&:first) end @@ -67,6 +68,50 @@ describe PiecesJustificativesService do end end + context 'with avis.piece_justificative being confidentiel' do + let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, procedure: procedure) } + let(:avis) { create(:avis, dossier: dossier, confidentiel: true) } + let(:with_avis_piece_justificative) { false } + + before do + to_be_attached = { + io: StringIO.new("toto"), + filename: "toto.png", + content_type: "image/png", + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + } + + avis.piece_justificative_file.attach(to_be_attached) + end + + it "doesn't return confidentiel avis.piece_justificative_file" do + expect(subject).to be_empty + end + end + + context 'with avis.piece_justificative being public' do + let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, procedure: procedure) } + let(:avis) { create(:avis, dossier: dossier) } + let(:with_avis_piece_justificative) { false } + + before do + to_be_attached = { + io: StringIO.new("toto"), + filename: "toto.png", + content_type: "image/png", + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + } + + avis.piece_justificative_file.attach(to_be_attached) + end + + it "return avis.piece_justificative_file not confidentiel" do + expect(subject).not_to be_empty + end + end + context 'with a identite champ pj' do let(:procedure) { create(:procedure, :with_titre_identite) } let(:dossier) { create(:dossier, procedure: procedure) }