diff --git a/app/controllers/admin/pieces_justificatives_controller.rb b/app/controllers/admin/pieces_justificatives_controller.rb index 99012c304..d81f568ee 100644 --- a/app/controllers/admin/pieces_justificatives_controller.rb +++ b/app/controllers/admin/pieces_justificatives_controller.rb @@ -1,6 +1,7 @@ class Admin::PiecesJustificativesController < AdminController before_action :retrieve_procedure before_action :procedure_locked? + before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down] def show end @@ -22,12 +23,6 @@ class Admin::PiecesJustificativesController < AdminController render json: { message: 'Type de piece justificative not found' }, status: 404 end - def update_params - params - .require(:procedure) - .permit(types_de_piece_justificative_attributes: [:libelle, :description, :id, :order_place, :mandatory, :lien_demarche]) - end - def move_up index = params[:index].to_i - 1 if @procedure.switch_types_de_piece_justificative index @@ -44,4 +39,12 @@ class Admin::PiecesJustificativesController < AdminController render json: {}, status: 400 end end + + private + + def update_params + params + .require(:procedure) + .permit(types_de_piece_justificative_attributes: [:libelle, :description, :id, :order_place, :mandatory, :lien_demarche]) + end end diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index cca29a761..df2f7d3a7 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -83,11 +83,12 @@ class Admin::ProceduresController < AdminController @procedure = current_administrateur.procedures.find(params[:id]) if !@procedure.update(procedure_params) - flash.now.alert = @procedure.errors.full_messages - return render 'edit' + flash.alert = @procedure.errors.full_messages + else + reset_procedure + flash.notice = 'Procédure modifiée' end - flash.notice = 'Procédure modifiée' redirect_to edit_admin_procedure_path(id: @procedure.id) end diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index 256e06220..4a13b80e9 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -1,6 +1,7 @@ class Admin::TypesDeChampController < AdminController before_action :retrieve_procedure before_action :procedure_locked? + before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down] def destroy @procedure.types_de_champ.destroy(params[:id]) diff --git a/app/controllers/admin/types_de_champ_private_controller.rb b/app/controllers/admin/types_de_champ_private_controller.rb index 320e0064e..5dfdc368b 100644 --- a/app/controllers/admin/types_de_champ_private_controller.rb +++ b/app/controllers/admin/types_de_champ_private_controller.rb @@ -1,6 +1,7 @@ class Admin::TypesDeChampPrivateController < AdminController before_action :retrieve_procedure before_action :procedure_locked? + before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down] def destroy @procedure.types_de_champ_private.destroy(params[:id]) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index d0b05532f..02df47ff5 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -21,4 +21,10 @@ class AdminController < ApplicationController redirect_to admin_procedure_path(id: @procedure.id) end end + + def reset_procedure + if @procedure.brouillon_avec_lien? + @procedure.reset! + end + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 526759957..54a249bb9 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -109,6 +109,14 @@ class Procedure < ApplicationRecord dossiers.update_all(hidden_at: now) end + def reset! + if locked? + raise "Can not reset a locked procedure." + else + dossiers.delete_all + end + end + def locked? publiee_ou_archivee? end diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 3a1f16831..242d2388b 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -258,7 +258,7 @@ describe Admin::ProceduresController, type: :controller do end context 'when administrateur is connected' do - before do + def update_procedure put :update, params: { id: procedure.id, procedure: procedure_params } procedure.reload end @@ -274,6 +274,8 @@ describe Admin::ProceduresController, type: :controller do let(:duree_conservation_dossiers_dans_ds) { 7 } let(:duree_conservation_dossiers_hors_ds) { 5 } + before { update_procedure } + describe 'procedure attributs in database' do subject { procedure } @@ -299,6 +301,7 @@ describe Admin::ProceduresController, type: :controller do end context 'when many attributs are not valid' do + before { update_procedure } let(:libelle) { '' } let(:description) { '' } @@ -315,12 +318,30 @@ describe Admin::ProceduresController, type: :controller do end end + context 'when procedure is brouillon' do + let(:procedure) { create(:procedure_with_dossiers, :with_path, :with_type_de_champ, :with_two_type_de_piece_justificative, administrateur: admin) } + let!(:dossiers_count) { procedure.dossiers.count } + + describe 'dossiers are dropped' do + before do + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) + end + + subject { update_procedure } + + it { + expect(dossiers_count).to eq(1) + expect(subject.dossiers.count).to eq(0) + } + end + end + context 'when procedure is published' do - let!(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, :published, administrateur: admin) } + let(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, :published, administrateur: admin) } + + subject { update_procedure } describe 'only some properties can be updated' do - subject { procedure } - it { expect(subject.libelle).to eq procedure_params[:libelle] } it { expect(subject.description).to eq procedure_params[:description] } it { expect(subject.organisation).to eq procedure_params[:organisation] } diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 0e5b3c413..b5944c502 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -29,6 +29,15 @@ FactoryBot.define do end end + trait :with_path do + after(:create) do |procedure| + create(:procedure_path, + procedure: procedure, + administrateur: procedure.administrateur, + path: generate(:published_path)) + end + end + trait :with_service do after(:build) do |procedure, _evaluator| procedure.service = create(:service)