From 6f14e7783f5cf3662ecd608f3555bbb00b8aa140 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 24 Jun 2020 12:56:58 +0200 Subject: [PATCH] dossier: refactor piece_justificative service --- app/services/pieces_justificatives_service.rb | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index c58997acc..ba67fba16 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -1,16 +1,10 @@ class PiecesJustificativesService def self.liste_pieces_justificatives(dossier) - pjs_commentaires = dossier.commentaires - .map(&:piece_jointe) + pjs_champs = pjs_for_champs(dossier) + pjs_commentaires = pjs_for_commentaires(dossier) + + (pjs_champs + pjs_commentaires) .filter(&:attached?) - - champs_blocs_repetables = dossier.champs - .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } - .flat_map(&:champs) - - pjs_commentaires + champs_pieces_justificatives_with_attachments( - champs_blocs_repetables + dossier.champs - ) end def self.pieces_justificatives_total_size(dossier) @@ -48,10 +42,21 @@ class PiecesJustificativesService private - def self.champs_pieces_justificatives_with_attachments(champs) - champs + def self.pjs_for_champs(dossier) + allowed_champs = dossier.champs + + allowed_child_champs = allowed_champs + .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } + .flat_map(&:champs) + + (allowed_champs + allowed_child_champs) .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) } - .filter { |pj| pj.piece_justificative_file.attached? } .map(&:piece_justificative_file) end + + def self.pjs_for_commentaires(dossier) + dossier + .commentaires + .map(&:piece_jointe) + end end