Démarches discarded by administrateur can be reactivated in manager

This commit is contained in:
Paul Chavard 2020-03-26 18:49:26 +01:00
parent 58c126308c
commit e4ab2574ce
4 changed files with 55 additions and 32 deletions

View file

@ -52,15 +52,14 @@ class Admin::ProceduresController < AdminController
def destroy def destroy
procedure = current_administrateur.procedures.find(params[:id]) procedure = current_administrateur.procedures.find(params[:id])
if procedure.locked? if procedure.can_be_deleted_by_administrateur?
return render json: {}, status: 401 procedure.discard_and_keep_track!(current_administrateur)
flash.notice = 'Démarche supprimée'
redirect_to admin_procedures_draft_path
else
render json: {}, status: 403
end end
procedure.reset!
procedure.destroy
flash.notice = 'Démarche supprimée'
redirect_to admin_procedures_draft_path
end end
def publish_validate def publish_validate

View file

@ -517,11 +517,21 @@ class Procedure < ApplicationRecord
groupe_instructeurs.count > 1 groupe_instructeurs.count > 1
end end
def can_be_deleted_by_administrateur?
brouillon? || dossiers.state_instruction_commencee.empty?
end
def can_be_deleted_by_manager? def can_be_deleted_by_manager?
kept? && dossiers.state_instruction_commencee.empty? kept? && can_be_deleted_by_administrateur?
end end
def discard_and_keep_track!(author) def discard_and_keep_track!(author)
if brouillon?
reset!
elsif publiee?
close!
end
dossiers.each do |dossier| dossiers.each do |dossier|
dossier.discard_and_keep_track!(author, :procedure_removed) dossier.discard_and_keep_track!(author, :procedure_removed)
end end

View file

@ -25,7 +25,7 @@
%td= link_to(try_format_datetime(procedure.created_at), admin_procedure_href) %td= link_to(try_format_datetime(procedure.created_at), admin_procedure_href)
%td %td
= link_to('Cloner', admin_procedure_clone_path(procedure.id), data: { method: :put }, class: 'btn-sm btn-primary clone-btn') = link_to('Cloner', admin_procedure_clone_path(procedure.id), data: { method: :put }, class: 'btn-sm btn-primary clone-btn')
- if !procedure.locked? - if !procedure.can_be_deleted_by_administrateur?
= link_to('X', url_for(controller: 'admin/procedures', action: :destroy, id: procedure.id), data: { method: :delete, confirm: "Confirmez-vous la suppression de la démarche ? \n\n Attention : toute suppression est définitive et sappliquera aux éventuels autres administrateurs de cette démarche !" }, class: 'btn-sm btn-danger') = link_to('X', url_for(controller: 'admin/procedures', action: :destroy, id: procedure.id), data: { method: :delete, confirm: "Confirmez-vous la suppression de la démarche ? \n\n Attention : toute suppression est définitive et sappliquera aux éventuels autres administrateurs de cette démarche !" }, class: 'btn-sm btn-danger')
= smart_listing.paginate = smart_listing.paginate

View file

@ -95,51 +95,65 @@ describe Admin::ProceduresController, type: :controller do
end end
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
let(:procedure_draft) { create :procedure_with_dossiers, administrateur: admin, instructeurs: [admin.instructeur], published_at: nil, closed_at: nil } let(:procedure_draft) { create(:procedure, administrateurs: [admin]) }
let(:procedure_published) { create :procedure_with_dossiers, administrateur: admin, instructeurs: [admin.instructeur], aasm_state: :publiee, published_at: Time.zone.now, closed_at: nil } let(:procedure_published) { create(:procedure, :published, administrateurs: [admin]) }
let(:procedure_closed) { create :procedure_with_dossiers, administrateur: admin, instructeurs: [admin.instructeur], aasm_state: :close, published_at: nil, closed_at: Time.zone.now } let(:procedure_closed) { create(:procedure, :closed, administrateurs: [admin]) }
let(:procedure) { dossier.procedure }
subject { delete :destroy, params: { id: procedure.id } } subject { delete :destroy, params: { id: procedure } }
context 'when the procedure is a draft' do context 'when the procedure is a brouillon' do
let!(:procedure) { procedure_draft } let(:dossier) { create(:dossier, :en_instruction, procedure: procedure_draft) }
it 'destroys the procedure' do before { subject }
expect { subject }.to change { Procedure.count }.by(-1)
it 'discard the procedure' do
expect(procedure.reload.discarded?).to be_truthy
end end
it 'deletes associated dossiers' do it 'deletes associated dossiers' do
subject expect(procedure.dossiers.with_discarded.count).to eq(0)
expect(Dossier.find_by(procedure_id: procedure.id)).to be_blank
end end
it 'redirects to the procedure drafts page' do it 'redirects to the procedure drafts page' do
subject
expect(response).to redirect_to admin_procedures_draft_path expect(response).to redirect_to admin_procedures_draft_path
expect(flash[:notice]).to be_present expect(flash[:notice]).to be_present
end end
end end
context 'when procedure is published' do context 'when procedure is published' do
let!(:procedure) { procedure_published } let(:dossier) { create(:dossier, :en_instruction, procedure: procedure_published) }
it { expect { subject }.not_to change { Procedure.count } } before { subject }
it { expect { subject }.not_to change { Dossier.count } }
it { expect(subject.status).to eq 401 } it { expect(response.status).to eq 403 }
context 'when dossier is en_construction' do
let(:dossier) { create(:dossier, :en_construction, procedure: procedure_published) }
it { expect(procedure.reload.close?).to be_truthy }
it { expect(procedure.reload.discarded?).to be_truthy }
it { expect(dossier.reload.discarded?).to be_truthy }
end
end end
context 'when procedure is closed' do context 'when procedure is closed' do
let!(:procedure) { procedure_closed } let(:dossier) { create(:dossier, :en_instruction, procedure: procedure_closed) }
it { expect { subject }.not_to change { Procedure.count } } before { subject }
it { expect { subject }.not_to change { Dossier.count } }
it { expect(subject.status).to eq 401 } it { expect(response.status).to eq 403 }
context 'when dossier is en_construction' do
let(:dossier) { create(:dossier, :en_construction, procedure: procedure_published) }
it { expect(procedure.reload.discarded?).to be_truthy }
it { expect(dossier.reload.discarded?).to be_truthy }
end
end end
context "when administrateur does not own the procedure" do context "when administrateur does not own the procedure" do
let(:procedure_not_owned) { create :procedure, administrateur: create(:administrateur), published_at: nil, closed_at: nil } let(:dossier) { create(:dossier) }
subject { delete :destroy, params: { id: procedure_not_owned.id } }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end end