diff --git a/app/models/concerns/dossier_state_concern.rb b/app/models/concerns/dossier_state_concern.rb index fae8b5f91..dc1b2ed51 100644 --- a/app/models/concerns/dossier_state_concern.rb +++ b/app/models/concerns/dossier_state_concern.rb @@ -20,6 +20,7 @@ module DossierStateConcern def after_commit_passer_en_construction NotificationMailer.send_en_construction_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + remove_piece_justificative_file_not_visible! end def after_passer_en_instruction(h) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 22c9056d8..f070ccff1 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -894,6 +894,7 @@ class Dossier < ApplicationRecord resolve_pending_correction! process_sva_svr! + remove_piece_justificative_file_not_visible! end def process_declarative! @@ -934,6 +935,10 @@ class Dossier < ApplicationRecord champs_public.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later) end + def remove_piece_justificative_file_not_visible! + champs.filter { _1.piece_justificative_file.attached? && !_1.visible? && _1.piece_justificative_file.purge_later } + end + def check_mandatory_and_visible_champs champs_for_revision(scope: :public) .filter { _1.child? ? _1.parent.visible? : true } diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index f09fc4f47..97462cfe1 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -599,7 +599,10 @@ describe Users::DossiersController, type: :controller do end context 'when dossier was already submitted' do - before { post :submit_en_construction, params: payload } + before do + expect_any_instance_of(Dossier).to receive(:remove_piece_justificative_file_not_visible!) + post :submit_en_construction, params: payload + end it 'redirects to the dossier' do subject diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index bf3e600fd..6a716acf9 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -476,18 +476,22 @@ describe Dossier, type: :model do context 'when dossier is en_construction' do context 'when the procedure.routing_enabled? is false' do - before do + subject do dossier.passer_en_construction! dossier.reload end - it { expect(dossier.state).to eq(Dossier.states.fetch(:en_construction)) } - it { expect(dossier.en_construction_at).to eq(beginning_of_day) } - it { expect(dossier.depose_at).to eq(beginning_of_day) } - it { expect(dossier.traitement.state).to eq(Dossier.states.fetch(:en_construction)) } - it { expect(dossier.traitement.processed_at).to eq(beginning_of_day) } + it do + subject + expect(dossier.state).to eq(Dossier.states.fetch(:en_construction)) + expect(dossier.en_construction_at).to eq(beginning_of_day) + expect(dossier.depose_at).to eq(beginning_of_day) + expect(dossier.traitement.state).to eq(Dossier.states.fetch(:en_construction)) + expect(dossier.traitement.processed_at).to eq(beginning_of_day) + end it 'should keep first en_construction_at date' do + subject Timecop.return dossier.passer_en_instruction!(instructeur: instructeur) dossier.repasser_en_construction!(instructeur: instructeur) @@ -498,6 +502,45 @@ describe Dossier, type: :model do expect(dossier.depose_at).to eq(beginning_of_day) expect(dossier.en_construction_at).to be > beginning_of_day end + + context 'when dossier have piece_justificative or titre_identite' do + include Logic + + let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:dossier) { create(:dossier, :brouillon, :with_populated_champs, procedure:) } + + before { expect(champ).to receive(:visible?).and_return(visible) } + + context 'when piece_justificative' do + let(:types_de_champ_public) { [{ type: :piece_justificative }] } + let(:champ) { dossier.champs_for_revision(scope: :public).find(&:piece_justificative?) } + + context 'when not visible' do + let(:visible) { false } + it { expect { subject }.to change { champ.reload.piece_justificative_file.attached? } } + end + + context 'when visible' do + let(:visible) { true } + it { expect { subject }.not_to change { champ.reload.piece_justificative_file.attached? } } + end + end + + context 'when titre identite' do + let(:types_de_champ_public) { [{ type: :titre_identite }] } + let(:champ) { dossier.champs_for_revision(scope: :public).find(&:piece_justificative?) } + + context 'when not visible' do + let(:visible) { false } + it { expect { subject }.to change { champ.reload.piece_justificative_file.attached? } } + end + + context 'when visible' do + let(:visible) { true } + it { expect { subject }.not_to change { champ.reload.piece_justificative_file.attached? } } + end + end + end end context 'when the procedure.routing_enabled? is true' do