From a151a304e292982f17f11fa09d53cf4d9b544aee Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 19 Feb 2024 16:22:08 +0100 Subject: [PATCH] feat(pj_service): add expert's avis (even when confidential) to exports asked by same expert --- app/services/pieces_justificatives_service.rb | 11 ++++++++++- spec/services/pieces_justificatives_service_spec.rb | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 7d67ee8a7..2d5a002a9 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -223,7 +223,16 @@ class PiecesJustificativesService def pjs_for_avis(dossiers) 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 !liste_documents_allows?(:with_avis_piece_justificative) + if !liste_documents_allows?(:with_avis_piece_justificative) + avis_ids_dossier_id_query = avis_ids_dossier_id_query.where(confidentiel: false) + end + if @user_profile.is_a?(Expert) + avis_ids = Avis.joins(:dossier, experts_procedure: :expert) + .where(experts_procedure: { expert: @user_profile }) + .where(dossier: dossiers) + .pluck(:id) + avis_ids_dossier_id_query = avis_ids_dossier_id_query.or(Avis.where(id: avis_ids)) + end avis_ids_dossier_id = avis_ids_dossier_id_query.pluck(:id, :dossier_id).to_h ActiveStorage::Attachment diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 61420f603..eaf7a919d 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -244,7 +244,16 @@ describe PiecesJustificativesService do context 'given an expert' do let(:user_profile) { create(:expert) } it "doesn't return confidentiel avis.piece_justificative_file" do - expect(subject).to be_empty + expect(subject.size).to eq(0) + end + end + + context 'when the expert has given the avis' do + let(:experts_procedure) { create(:experts_procedure, expert: user_profile, procedure:) } + let(:avis) { create(:avis, experts_procedure:, dossier: dossier, confidentiel: true) } + let(:user_profile) { create(:expert) } + it "doesn't return confidentiel avis.piece_justificative_file" do + expect(subject.size).to eq(2) end end end