diff --git a/app/tasks/maintenance/remove_piece_justificative_file_not_visible_task.rb b/app/tasks/maintenance/remove_piece_justificative_file_not_visible_task.rb new file mode 100644 index 000000000..473b5698c --- /dev/null +++ b/app/tasks/maintenance/remove_piece_justificative_file_not_visible_task.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Maintenance + class RemovePieceJustificativeFileNotVisibleTask < MaintenanceTasks::Task + attribute :procedure_id, :string + validates :procedure_id, presence: true + + def collection + procedure = Procedure.with_discarded.find(procedure_id.strip.to_i) + procedure.dossiers.state_not_brouillon + end + + def process(dossier) + dossier.remove_piece_justificative_file_not_visible! + end + end +end diff --git a/spec/tasks/maintenance/remove_piece_justificative_file_not_visible_task_spec.rb b/spec/tasks/maintenance/remove_piece_justificative_file_not_visible_task_spec.rb new file mode 100644 index 000000000..d6d52631f --- /dev/null +++ b/spec/tasks/maintenance/remove_piece_justificative_file_not_visible_task_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe RemovePieceJustificativeFileNotVisibleTask do + describe "#process" do + subject(:process) { described_class.process(dossier) } + + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) } + let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:) } + + before { expect(champ).to receive(:visible?).and_return(visible) } + + context 'when piece_justificative' do + 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 +end