From da52ec30bdb06058cadff518e6b14a201f04640d Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 26 Mar 2020 17:35:50 +0100 Subject: [PATCH] Undo discard dossiers and demarches from manager --- .../manager/dossiers_controller.rb | 9 ++++ .../manager/procedures_controller.rb | 8 ++++ app/models/deleted_dossier.rb | 4 ++ app/models/dossier.rb | 13 +++++ app/models/dossier_operation_log.rb | 1 + app/models/procedure.rb | 8 ++++ app/views/manager/dossiers/show.html.erb | 2 + app/views/manager/procedures/show.html.erb | 2 + config/routes.rb | 2 + .../manager/dossiers_controller_spec.rb | 30 ++++++++++-- .../manager/procedures_controller_spec.rb | 47 ++++++++++++++----- 11 files changed, 109 insertions(+), 17 deletions(-) diff --git a/app/controllers/manager/dossiers_controller.rb b/app/controllers/manager/dossiers_controller.rb index 143dbf048..ebf98f7d1 100644 --- a/app/controllers/manager/dossiers_controller.rb +++ b/app/controllers/manager/dossiers_controller.rb @@ -30,6 +30,15 @@ module Manager redirect_to manager_dossier_path(dossier) end + def restore + dossier = Dossier.with_discarded.find(params[:id]) + dossier.restore(current_administration) + + flash[:notice] = "Le dossier #{dossier.id} a été restauré." + + redirect_to manager_dossier_path(dossier) + end + def repasser_en_instruction dossier = Dossier.find(params[:id]) dossier.repasser_en_instruction(current_administration) diff --git a/app/controllers/manager/procedures_controller.rb b/app/controllers/manager/procedures_controller.rb index 810d0a1a9..a6c45dbd9 100644 --- a/app/controllers/manager/procedures_controller.rb +++ b/app/controllers/manager/procedures_controller.rb @@ -31,6 +31,14 @@ module Manager redirect_to manager_procedure_path(procedure) end + def restore + procedure.restore(current_administration) + + flash[:notice] = "La démarche #{procedure.id} a été restauré." + + redirect_to manager_procedure_path(procedure) + end + def add_administrateur administrateur = Administrateur.by_email(params[:email]) if administrateur diff --git a/app/models/deleted_dossier.rb b/app/models/deleted_dossier.rb index 0a0ebc9a8..6a7845b08 100644 --- a/app/models/deleted_dossier.rb +++ b/app/models/deleted_dossier.rb @@ -20,4 +20,8 @@ class DeletedDossier < ApplicationRecord deleted_at: Time.zone.now ) end + + def procedure_removed? + reason == self.class.reasons.fetch(:procedure_removed) + end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 544acfd20..ad2f8487b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -487,6 +487,19 @@ class Dossier < ApplicationRecord discard! end + def restore(author, only_discarded_with_procedure = false) + if discarded? + deleted_dossier = DeletedDossier.find_by(dossier_id: id) + + if !only_discarded_with_procedure || deleted_dossier&.procedure_removed? + if undiscard && keep_track_on_deletion? && en_construction? + deleted_dossier&.destroy + log_dossier_operation(author, :restaurer, self) + end + end + end + end + def after_passer_en_instruction(instructeur) instructeur.follow(self) diff --git a/app/models/dossier_operation_log.rb b/app/models/dossier_operation_log.rb index f1cd8ce1e..924fbb962 100644 --- a/app/models/dossier_operation_log.rb +++ b/app/models/dossier_operation_log.rb @@ -8,6 +8,7 @@ class DossierOperationLog < ApplicationRecord refuser: 'refuser', classer_sans_suite: 'classer_sans_suite', supprimer: 'supprimer', + restaurer: 'restaurer', modifier_annotation: 'modifier_annotation', demander_un_avis: 'demander_un_avis' } diff --git a/app/models/procedure.rb b/app/models/procedure.rb index b212b7ca9..32e6067f9 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -523,6 +523,14 @@ class Procedure < ApplicationRecord discard! end + def restore(author) + if discarded? && undiscard + dossiers.with_discarded.discarded.find_each do |dossier| + dossier.restore(author, true) + end + end + end + def flipper_id "Procedure;#{id}" end diff --git a/app/views/manager/dossiers/show.html.erb b/app/views/manager/dossiers/show.html.erb index 140497f4c..12b1415cc 100644 --- a/app/views/manager/dossiers/show.html.erb +++ b/app/views/manager/dossiers/show.html.erb @@ -33,6 +33,8 @@ as well as a link to its edit page. <% end %> <% if dossier.can_be_deleted_by_manager? %> <%= link_to 'Supprimer le dossier', discard_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la suppression du dossier ?" } %> + <% elsif dossier.discarded? && !dossier.procedure.discarded? %> + <%= link_to 'Restaurer le dossier', restore_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la restauration du dossier ?" } %> <% end %> diff --git a/app/views/manager/procedures/show.html.erb b/app/views/manager/procedures/show.html.erb index ede594883..faa058874 100644 --- a/app/views/manager/procedures/show.html.erb +++ b/app/views/manager/procedures/show.html.erb @@ -44,6 +44,8 @@ as well as a link to its edit page. <% if procedure.can_be_deleted_by_manager? %> <%= link_to 'Supprimer la démarche', discard_manager_procedure_path(procedure), method: :post, class: 'button', data: { confirm: "Confirmez-vous la suppression de la démarche ?" } %> + <% elsif procedure.discarded? %> + <%= link_to 'Restaurer la démarche', restore_manager_procedure_path(procedure), method: :post, class: 'button', data: { confirm: "Confirmez-vous la restauration de la démarche ?" } %> <% end %>
diff --git a/config/routes.rb b/config/routes.rb index 3e5412e6c..273880d2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,12 +10,14 @@ Rails.application.routes.draw do post 'whitelist', on: :member post 'draft', on: :member post 'discard', on: :member + post 'restore', on: :member post 'add_administrateur', on: :member post 'change_piece_justificative_template', on: :member end resources :dossiers, only: [:index, :show] do post 'discard', on: :member + post 'restore', on: :member post 'repasser_en_instruction', on: :member end diff --git a/spec/controllers/manager/dossiers_controller_spec.rb b/spec/controllers/manager/dossiers_controller_spec.rb index c64100a22..3b1969fc5 100644 --- a/spec/controllers/manager/dossiers_controller_spec.rb +++ b/spec/controllers/manager/dossiers_controller_spec.rb @@ -1,23 +1,43 @@ describe Manager::DossiersController, type: :controller do + let(:administration) { create :administration } + let(:deleted_dossier) { DeletedDossier.find_by(dossier_id: dossier) } + let(:operations) { dossier.dossier_operation_logs.map(&:operation).map(&:to_sym) } + + before { sign_in administration } + describe '#discard' do - let(:administration) { create :administration } - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, :en_construction) } before do - sign_in administration post :discard, params: { id: dossier.id } dossier.reload end it { expect(dossier.discarded?).to be_truthy } + it { expect(deleted_dossier).not_to be_nil } + it { expect(deleted_dossier.reason).to eq("manager_request") } + it { expect(operations).to eq([:supprimer]) } + end + + describe '#restore' do + let(:dossier) { create(:dossier, :en_construction) } + + before do + dossier.discard_and_keep_track!(administration, :manager_request) + + post :restore, params: { id: dossier.id } + dossier.reload + end + + it { expect(dossier.kept?).to be_truthy } + it { expect(deleted_dossier).to be_nil } + it { expect(operations).to eq([:supprimer, :restaurer]) } end describe '#repasser_en_instruction' do - let(:administration) { create :administration } let(:dossier) { create(:dossier, :accepte) } before do - sign_in administration post :repasser_en_instruction, params: { id: dossier.id } dossier.reload end diff --git a/spec/controllers/manager/procedures_controller_spec.rb b/spec/controllers/manager/procedures_controller_spec.rb index 0e98c2660..b8bebd461 100644 --- a/spec/controllers/manager/procedures_controller_spec.rb +++ b/spec/controllers/manager/procedures_controller_spec.rb @@ -1,25 +1,25 @@ describe Manager::ProceduresController, type: :controller do + let(:administration) { create :administration } + + before { sign_in administration } + describe '#whitelist' do - let(:administration) { create :administration } let(:procedure) { create(:procedure) } before do - sign_in administration post :whitelist, params: { id: procedure.id } procedure.reload end - it { expect(procedure.whitelisted_at).not_to be_nil } + it { expect(procedure.whitelisted?).to be_truthy } end describe '#show' do render_views - let(:administration) { create(:administration) } let(:procedure) { create(:procedure, :with_repetition) } before do - sign_in(administration) get :show, params: { id: procedure.id } end @@ -27,27 +27,50 @@ describe Manager::ProceduresController, type: :controller do end describe '#discard' do - let(:administration) { create :administration } - let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, :en_construction) } + let(:procedure) { dossier.procedure } + let(:deleted_dossier) { DeletedDossier.find_by(dossier_id: dossier.id) } + let(:operations) { dossier.dossier_operation_logs.map(&:operation).map(&:to_sym) } before do - sign_in administration post :discard, params: { id: procedure.id } procedure.reload + dossier.reload end it { expect(procedure.discarded?).to be_truthy } + it { expect(dossier.discarded?).to be_truthy } + it { expect(deleted_dossier).not_to be_nil } + it { expect(deleted_dossier.reason).to eq("procedure_removed") } + it { expect(operations).to eq([:supprimer]) } + end + + describe '#restore' do + let(:dossier) { create(:dossier, :en_construction) } + let(:procedure) { dossier.procedure } + let(:deleted_dossier) { DeletedDossier.find_by(dossier_id: dossier.id) } + let(:operations) { dossier.dossier_operation_logs.map(&:operation).map(&:to_sym) } + + before do + procedure.discard_and_keep_track!(administration) + + post :restore, params: { id: procedure.id } + procedure.reload + end + + it { expect(procedure.kept?).to be_truthy } + it { expect(dossier.kept?).to be_truthy } + it { expect(deleted_dossier).to be_nil } + it { expect(operations).to eq([:supprimer, :restaurer]) } end describe '#index' do render_views - let(:administration) { create(:administration) } - let!(:dossier) { create(:dossier) } - context 'sort by dossiers' do + let!(:dossier) { create(:dossier) } + before do - sign_in(administration) get :index, params: { procedure: { direction: 'asc', order: 'dossiers' } } end