From a8354bd103485fd71f09c471004c788776c03864 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 15 Jul 2019 14:00:49 +0000 Subject: [PATCH] dossiers: unify deletion of dossiers between manager and user The code paths for deleting a dossier were different, depending on whether the dossier was deleted by the user, or from the Manager. This commit unifies the two code paths into one. This has the effect of: - An operation log is now recorded when an user deletes its own dossier; - Gestionnaires are now notified even when the dossier is deleted from the Manager; - The `support:delete_user_account` task now requires the email address of the author. --- .../manager/dossiers_controller.rb | 4 +-- app/controllers/users/dossiers_controller.rb | 2 +- app/models/dossier.rb | 12 +++------ lib/tasks/support.rake | 19 +++++++++----- spec/models/dossier_spec.rb | 25 ++++++------------- 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/app/controllers/manager/dossiers_controller.rb b/app/controllers/manager/dossiers_controller.rb index 31867443f..35b7c785c 100644 --- a/app/controllers/manager/dossiers_controller.rb +++ b/app/controllers/manager/dossiers_controller.rb @@ -22,10 +22,10 @@ module Manager def hide dossier = Dossier.find(params[:id]) - dossier.hide!(current_administration) + dossier.delete_and_keep_track(current_administration) logger.info("Le dossier #{dossier.id} est supprimé par #{current_administration.email}") - flash[:notice] = "Le dossier #{dossier.id} est supprimé" + flash[:notice] = "Le dossier #{dossier.id} a été supprimé." redirect_to manager_dossier_path(dossier) end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 3e13bbc46..4479e5a18 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -193,7 +193,7 @@ module Users dossier = current_user.dossiers.includes(:user, procedure: :administrateurs).find(params[:id]) if dossier.can_be_deleted_by_user? - dossier.delete_and_keep_track + dossier.delete_and_keep_track(current_user) flash.notice = 'Votre dossier a bien été supprimé.' redirect_to dossiers_path else diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 28682f056..a0251397b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -317,7 +317,7 @@ class Dossier < ApplicationRecord end end - def delete_and_keep_track + def delete_and_keep_track(author) deleted_dossier = DeletedDossier.create_from_dossier(self) update(hidden_at: deleted_dossier.deleted_at) @@ -328,6 +328,8 @@ class Dossier < ApplicationRecord end end DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later + + log_dossier_operation(author, :supprimer, self) end def after_passer_en_instruction(gestionnaire) @@ -409,14 +411,6 @@ class Dossier < ApplicationRecord log_dossier_operation(gestionnaire, :classer_sans_suite, self) end - def hide!(administration) - update(hidden_at: Time.zone.now) - - deleted_dossier = DeletedDossier.create_from_dossier(self) - DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later - log_dossier_operation(administration, :supprimer, self) - end - def check_mandatory_champs (champs + champs.select(&:repetition?).flat_map(&:champs)) .select(&:mandatory_and_blank?) diff --git a/lib/tasks/support.rake b/lib/tasks/support.rake index 9cdb35adb..7d96ac8e2 100644 --- a/lib/tasks/support.rake +++ b/lib/tasks/support.rake @@ -2,19 +2,26 @@ require Rails.root.join("lib", "tasks", "task_helper") namespace :support do desc <<~EOD - Delete the user account for a given USER_EMAIL. + Delete the user account for a given USER_EMAIL on the behalf of ADMIN_EMAIL. Only works if the user has no dossier where the instruction has started. EOD task delete_user_account: :environment do user_email = ENV['USER_EMAIL'] - if user_email.nil? - fail "Must specify a USER_EMAIL" - end - user = User.find_by(email: user_email) + fail "Must specify a USER_EMAIL" if user_email.nil? + + administration_email = ENV['ADMIN_EMAIL'] + fail "Must specify the ADMIN_EMAIL of the operator performing the deletion (yourself)" if administration_email.nil? + + user = User.find_by!(email: user_email) + administration = Administration.find_by!(email: administration_email) + if user.dossiers.state_instruction_commencee.any? fail "Cannot delete this user because instruction has started for some dossiers" end - user.dossiers.each(&:delete_and_keep_track) + + user.dossiers.each do |dossier| + dossier.delete_and_keep_track(administration) + end user.destroy end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 67ad2a39a..312e5d9ab 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -632,13 +632,14 @@ describe Dossier do describe "#delete_and_keep_track" do let(:dossier) { create(:dossier) } let(:deleted_dossier) { DeletedDossier.find_by!(dossier_id: dossier.id) } + let(:last_operation) { dossier.dossier_operation_logs.last } before do allow(DossierMailer).to receive(:notify_deletion_to_user).and_return(double(deliver_later: nil)) allow(DossierMailer).to receive(:notify_deletion_to_administration).and_return(double(deliver_later: nil)) end - subject! { dossier.delete_and_keep_track } + subject! { dossier.delete_and_keep_track(dossier.user) } it 'hides the dossier' do expect(dossier.hidden_at).to be_present @@ -655,6 +656,11 @@ describe Dossier do expect(DossierMailer).to have_received(:notify_deletion_to_user).with(deleted_dossier, dossier.user.email) end + it 'records the operation in the log' do + expect(last_operation.operation).to eq("supprimer") + expect(last_operation.automatic_operation?).to be_falsey + end + context 'where gestionnaires are following the dossier' do let(:dossier) { create(:dossier, :en_construction, :followed) } let!(:non_following_gestionnaire) do @@ -990,23 +996,6 @@ describe Dossier do end end - describe '#hide!' do - let(:dossier) { create(:dossier) } - let(:administration) { create(:administration) } - let(:last_operation) { dossier.dossier_operation_logs.last } - - before do - Timecop.freeze - dossier.hide!(administration) - end - - after { Timecop.return } - - it { expect(dossier.hidden_at).to eq(Time.zone.now) } - it { expect(last_operation.operation).to eq('supprimer') } - it { expect(last_operation.automatic_operation?).to be_falsey } - end - describe '#repasser_en_instruction!' do let(:dossier) { create(:dossier, :refuse, :with_attestation) } let!(:gestionnaire) { create(:gestionnaire) }