[Refactor] Unify dossier deletion between manager and user (#4099)
Refactor de la suppression de dossier (#4099)
This commit is contained in:
commit
fa2d6f7b8b
5 changed files with 26 additions and 36 deletions
|
@ -22,10 +22,10 @@ module Manager
|
||||||
|
|
||||||
def hide
|
def hide
|
||||||
dossier = Dossier.find(params[:id])
|
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}")
|
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)
|
redirect_to manager_dossier_path(dossier)
|
||||||
end
|
end
|
||||||
|
|
|
@ -193,7 +193,7 @@ module Users
|
||||||
dossier = current_user.dossiers.includes(:user, procedure: :administrateurs).find(params[:id])
|
dossier = current_user.dossiers.includes(:user, procedure: :administrateurs).find(params[:id])
|
||||||
|
|
||||||
if dossier.can_be_deleted_by_user?
|
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é.'
|
flash.notice = 'Votre dossier a bien été supprimé.'
|
||||||
redirect_to dossiers_path
|
redirect_to dossiers_path
|
||||||
else
|
else
|
||||||
|
|
|
@ -317,7 +317,7 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_and_keep_track
|
def delete_and_keep_track(author)
|
||||||
deleted_dossier = DeletedDossier.create_from_dossier(self)
|
deleted_dossier = DeletedDossier.create_from_dossier(self)
|
||||||
update(hidden_at: deleted_dossier.deleted_at)
|
update(hidden_at: deleted_dossier.deleted_at)
|
||||||
|
|
||||||
|
@ -328,6 +328,8 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later
|
DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later
|
||||||
|
|
||||||
|
log_dossier_operation(author, :supprimer, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_passer_en_instruction(gestionnaire)
|
def after_passer_en_instruction(gestionnaire)
|
||||||
|
@ -409,14 +411,6 @@ class Dossier < ApplicationRecord
|
||||||
log_dossier_operation(gestionnaire, :classer_sans_suite, self)
|
log_dossier_operation(gestionnaire, :classer_sans_suite, self)
|
||||||
end
|
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
|
def check_mandatory_champs
|
||||||
(champs + champs.select(&:repetition?).flat_map(&:champs))
|
(champs + champs.select(&:repetition?).flat_map(&:champs))
|
||||||
.select(&:mandatory_and_blank?)
|
.select(&:mandatory_and_blank?)
|
||||||
|
|
|
@ -2,19 +2,26 @@ require Rails.root.join("lib", "tasks", "task_helper")
|
||||||
|
|
||||||
namespace :support do
|
namespace :support do
|
||||||
desc <<~EOD
|
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.
|
Only works if the user has no dossier where the instruction has started.
|
||||||
EOD
|
EOD
|
||||||
task delete_user_account: :environment do
|
task delete_user_account: :environment do
|
||||||
user_email = ENV['USER_EMAIL']
|
user_email = ENV['USER_EMAIL']
|
||||||
if user_email.nil?
|
fail "Must specify a USER_EMAIL" if user_email.nil?
|
||||||
fail "Must specify a USER_EMAIL"
|
|
||||||
end
|
administration_email = ENV['ADMIN_EMAIL']
|
||||||
user = User.find_by(email: user_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?
|
if user.dossiers.state_instruction_commencee.any?
|
||||||
fail "Cannot delete this user because instruction has started for some dossiers"
|
fail "Cannot delete this user because instruction has started for some dossiers"
|
||||||
end
|
end
|
||||||
user.dossiers.each(&:delete_and_keep_track)
|
|
||||||
|
user.dossiers.each do |dossier|
|
||||||
|
dossier.delete_and_keep_track(administration)
|
||||||
|
end
|
||||||
user.destroy
|
user.destroy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -632,13 +632,14 @@ describe Dossier do
|
||||||
describe "#delete_and_keep_track" do
|
describe "#delete_and_keep_track" do
|
||||||
let(:dossier) { create(:dossier) }
|
let(:dossier) { create(:dossier) }
|
||||||
let(:deleted_dossier) { DeletedDossier.find_by!(dossier_id: dossier.id) }
|
let(:deleted_dossier) { DeletedDossier.find_by!(dossier_id: dossier.id) }
|
||||||
|
let(:last_operation) { dossier.dossier_operation_logs.last }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(DossierMailer).to receive(:notify_deletion_to_user).and_return(double(deliver_later: nil))
|
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))
|
allow(DossierMailer).to receive(:notify_deletion_to_administration).and_return(double(deliver_later: nil))
|
||||||
end
|
end
|
||||||
|
|
||||||
subject! { dossier.delete_and_keep_track }
|
subject! { dossier.delete_and_keep_track(dossier.user) }
|
||||||
|
|
||||||
it 'hides the dossier' do
|
it 'hides the dossier' do
|
||||||
expect(dossier.hidden_at).to be_present
|
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)
|
expect(DossierMailer).to have_received(:notify_deletion_to_user).with(deleted_dossier, dossier.user.email)
|
||||||
end
|
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
|
context 'where gestionnaires are following the dossier' do
|
||||||
let(:dossier) { create(:dossier, :en_construction, :followed) }
|
let(:dossier) { create(:dossier, :en_construction, :followed) }
|
||||||
let!(:non_following_gestionnaire) do
|
let!(:non_following_gestionnaire) do
|
||||||
|
@ -990,23 +996,6 @@ describe Dossier do
|
||||||
end
|
end
|
||||||
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
|
describe '#repasser_en_instruction!' do
|
||||||
let(:dossier) { create(:dossier, :refuse, :with_attestation) }
|
let(:dossier) { create(:dossier, :refuse, :with_attestation) }
|
||||||
let!(:gestionnaire) { create(:gestionnaire) }
|
let!(:gestionnaire) { create(:gestionnaire) }
|
||||||
|
|
Loading…
Reference in a new issue