diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 47ab405a8..ec0bd9e4c 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -944,11 +944,15 @@ class Procedure < ApplicationRecord end def pieces_jointes_list_without_conditionnal - active_revision.types_de_champ_public.not_condition.filter(&:piece_justificative?) + pieces_jointes_list do |base_scope| + base_scope.where(types_de_champ: { condition: nil }) + end end def pieces_jointes_list_with_conditionnal - active_revision.types_de_champ_public.where.not(condition: nil).filter(&:piece_justificative?) + pieces_jointes_list do |base_scope| + base_scope.where.not(types_de_champ: { condition: nil }) + end end def toggle_routing @@ -961,6 +965,22 @@ class Procedure < ApplicationRecord private + def pieces_jointes_list + scope = yield active_revision.revision_types_de_champ_public + .includes(:type_de_champ, revision_types_de_champ: :type_de_champ) + .where(types_de_champ: { type_champ: ['repetition', 'piece_justificative', 'titre_identite'] }) + + scope.each_with_object([]) do |rtdc, list| + if rtdc.type_de_champ.repetition? + rtdc.revision_types_de_champ.each do |rtdc_in_repetition| + list << [rtdc_in_repetition.type_de_champ, rtdc.type_de_champ] if rtdc_in_repetition.type_de_champ.piece_justificative? + end + else + list << [rtdc.type_de_champ] + end + end + end + def validate_auto_archive_on_in_the_future return if auto_archive_on.nil? return if auto_archive_on.future? diff --git a/app/views/shared/_procedure_description.html.haml b/app/views/shared/_procedure_description.html.haml index 1ca1d8504..ef20da3b6 100644 --- a/app/views/shared/_procedure_description.html.haml +++ b/app/views/shared/_procedure_description.html.haml @@ -55,19 +55,13 @@ #accordion-116.fr-collapse - if procedure.pieces_jointes_list_without_conditionnal.present? %ul - - procedure.pieces_jointes_list_without_conditionnal.each do |pj| - %li - = pj.libelle - = t('utils.no_mandatory') if !pj.mandatory? + = render partial: "shared/procedure_pieces_jointes_list", collection: procedure.pieces_jointes_list_without_conditionnal, as: :pj - if procedure.pieces_jointes_list_with_conditionnal.present? %h3.fr-text--sm.fr-mb-0.fr-mt-2w = t('shared.procedure_description.pieces_jointes_conditionnal_list_title') %ul - - procedure.pieces_jointes_list_with_conditionnal.each do |pj| - %li - = pj.libelle - = t('utils.no_mandatory') if !pj.mandatory? + = render partial: "shared/procedure_pieces_jointes_list", collection: procedure.pieces_jointes_list_with_conditionnal, as: :pj - if @usual_traitement_time.present? %section.fr-accordion diff --git a/app/views/shared/_procedure_pieces_jointes_list.html.haml b/app/views/shared/_procedure_pieces_jointes_list.html.haml new file mode 100644 index 000000000..213010cfd --- /dev/null +++ b/app/views/shared/_procedure_pieces_jointes_list.html.haml @@ -0,0 +1,6 @@ +- tdc, parent_tdc = pj + +%li + = tdc.libelle + = "(#{parent_tdc.libelle})" if parent_tdc + = t('utils.no_mandatory') if !tdc.mandatory? diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 516d54eda..1445edf1a 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1693,6 +1693,32 @@ describe Procedure do end end + describe '#pieces_jointes_list' do + include Logic + let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:types_de_champ_public) do + [ + { type: :integer_number, stable_id: 900 }, + { type: :piece_justificative, libelle: "PJ", mandatory: true, stable_id: 910 }, + { type: :piece_justificative, libelle: "PJ-cond", mandatory: true, stable_id: 911, condition: ds_eq(champ_value(900), constant(1)) }, + { type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "PJ2", stable_id: 921 }] } + ] + end + + let(:pj1) { procedure.active_revision.types_de_champ.find { _1.stable_id == 910 } } + let(:pjcond) { procedure.active_revision.types_de_champ.find { _1.stable_id == 911 } } + let(:repetition) { procedure.active_revision.types_de_champ.find { _1.stable_id == 920 } } + let(:pj2) { procedure.active_revision.types_de_champ.find { _1.stable_id == 921 } } + + it "returns the list of pieces jointes without conditional" do + expect(procedure.pieces_jointes_list_without_conditionnal).to match_array([[pj1], [pj2, repetition]]) + end + + it "returns the list of pieces jointes having conditional" do + expect(procedure.pieces_jointes_list_with_conditionnal).to match_array([[pjcond]]) + end + end + private def create_dossier_with_pj_of_size(size, procedure)