diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index bd2dd2af3..ea7475d17 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -1,8 +1,12 @@ class PiecesJustificativesService def self.liste_pieces_justificatives(dossier) - dossier.champs - .select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) } - .filter { |pj| pj.piece_justificative_file.attached? } + champs_blocs_repetables = dossier.champs + .select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } + .flat_map(&:champs) + + champs_pieces_justificatives_with_attachments( + champs_blocs_repetables + dossier.champs + ) end def self.pieces_justificatives_total_size(dossier) @@ -37,4 +41,12 @@ class PiecesJustificativesService } end end + + private + + def self.champs_pieces_justificatives_with_attachments(champs) + champs + .select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) } + .filter { |pj| pj.piece_justificative_file.attached? } + end end diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 114b25bdd..b9913a536 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -175,4 +175,18 @@ FactoryBot.define do create(:champ_number, row: 1, type_de_champ: type_de_champ_number, parent: champ_repetition) end end + + factory :champ_repetition_with_piece_jointe, class: 'Champs::RepetitionChamp' do + type_de_champ { create(:type_de_champ_repetition) } + + after(:build) do |champ_repetition, _evaluator| + type_de_champ_pj0 = create(:type_de_champ_piece_justificative, order_place: 0, parent: champ_repetition.type_de_champ, libelle: 'Justificatif de domicile') + type_de_champ_pj1 = create(:type_de_champ_piece_justificative, order_place: 1, parent: champ_repetition.type_de_champ, libelle: 'Carte d\'identité') + + create(:champ_piece_justificative, row: 0, type_de_champ: type_de_champ_pj0, parent: champ_repetition) + create(:champ_piece_justificative, row: 0, type_de_champ: type_de_champ_pj1, parent: champ_repetition) + create(:champ_piece_justificative, row: 1, type_de_champ: type_de_champ_pj0, parent: champ_repetition) + create(:champ_piece_justificative, row: 1, type_de_champ: type_de_champ_pj1, parent: champ_repetition) + end + end end diff --git a/spec/lib/active_storage/downloadable_file_spec.rb b/spec/lib/active_storage/downloadable_file_spec.rb index dc40ec1d8..271915378 100644 --- a/spec/lib/active_storage/downloadable_file_spec.rb +++ b/spec/lib/active_storage/downloadable_file_spec.rb @@ -15,5 +15,14 @@ describe ActiveStorage::DownloadableFile do it { expect(list.length).to be 1 } end + + context 'when there is a repetition bloc' do + let(:champ) { build(:champ_repetition_with_piece_jointe) } + let(:dossier) { create(:dossier, :en_construction, champs: [champ]) } + + it 'should have 4 piece_justificatives' do + expect(list.size).to eq(4) + end + end end end