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
procedure = current_administrateur.procedures.find(params[:id])
if procedure.locked?
return render json: {}, status: 401
if procedure.can_be_deleted_by_administrateur?
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
procedure.reset!
procedure.destroy
flash.notice = 'Démarche supprimée'
redirect_to admin_procedures_draft_path
end
def publish_validate

View file

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

View file

@ -25,7 +25,7 @@
%td= link_to(try_format_datetime(procedure.created_at), admin_procedure_href)
%td
= 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')
= smart_listing.paginate

View file

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