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.
This commit is contained in:
Pierre de La Morinerie 2019-07-15 14:00:49 +00:00
parent 4016b27002
commit a8354bd103
5 changed files with 26 additions and 36 deletions

View file

@ -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

View file

@ -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

View file

@ -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?)

View file

@ -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

View file

@ -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) }