Always add a reason to dossier deletion

This commit is contained in:
Paul Chavard 2020-03-19 11:53:25 +01:00
parent 9243ae69df
commit 5c2eba1dd1
9 changed files with 119 additions and 73 deletions

View file

@ -22,7 +22,7 @@ module Manager
def hide
dossier = Dossier.find(params[:id])
dossier.delete_and_keep_track(current_administration)
dossier.delete_and_keep_track!(current_administration, :manager_request)
logger.info("Le dossier #{dossier.id} est supprimé par #{current_administration.email}")
flash[:notice] = "Le dossier #{dossier.id} a été supprimé."

View file

@ -203,7 +203,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(current_user)
dossier.delete_and_keep_track!(current_user, :user_request)
flash.notice = 'Votre dossier a bien été supprimé.'
redirect_to dossiers_path
else

View file

@ -404,21 +404,27 @@ class Dossier < ApplicationRecord
end
end
def delete_and_keep_track(author)
deleted_dossier = DeletedDossier.create_from_dossier(self)
discard!
def expired_keep_track!
DeletedDossier.create_from_dossier(self, :expired)
log_automatic_dossier_operation(:supprimer, self)
end
def delete_and_keep_track!(author, reason)
if en_construction?
deleted_dossier = DeletedDossier.create_from_dossier(self, reason)
administration_emails = followers_instructeurs.present? ? followers_instructeurs.map(&:email) : procedure.administrateurs.map(&:email)
administration_emails.each do |email|
DossierMailer.notify_deletion_to_administration(deleted_dossier, email).deliver_later
end
end
DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later
log_dossier_operation(author, :supprimer, self)
end
discard!
end
def after_passer_en_instruction(instructeur)
instructeur.follow(self)

View file

@ -120,7 +120,7 @@ class User < ApplicationRecord
end
dossiers.each do |dossier|
dossier.delete_and_keep_track(administration)
dossier.delete_and_keep_track!(administration, :user_removed)
end
dossiers.with_discarded.destroy_all
destroy!

View file

@ -65,18 +65,12 @@ class ExpiredDossiersDeletionService
).deliver_later
end
dossiers_to_remove.each do |dossier|
DeletedDossier.create_from_dossier(dossier)
dossier.destroy
end
dossiers_to_remove.destroy_all
end
def self.delete_expired_en_construction_and_notify
dossiers_to_remove = Dossier.en_construction_expired
dossiers_to_remove.each do |dossier|
DeletedDossier.create_from_dossier(dossier)
end
dossiers_to_remove.each(&:expired_keep_track!)
dossiers_to_remove
.includes(:user)

View file

@ -98,7 +98,7 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_automatic_deletion_to_user' do
let(:dossier) { create(:dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier, :expired) }
before do
duree = dossier.procedure.duree_conservation_dossiers_dans_ds
@ -116,7 +116,7 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_automatic_deletion_to_administration' do
let(:dossier) { create(:dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier, :expired) }
subject { described_class.notify_automatic_deletion_to_administration([deleted_dossier], dossier.user.email) }

View file

@ -661,23 +661,42 @@ describe Dossier do
end
end
describe "#delete_and_keep_track" do
let(:dossier) { create(:dossier) }
let(:deleted_dossier) { DeletedDossier.find_by!(dossier_id: dossier.id) }
describe "#delete_and_keep_track!" do
let(:dossier) { create(:dossier, :en_construction) }
let(:deleted_dossier) { DeletedDossier.find_by(dossier_id: dossier.id) }
let(:last_operation) { dossier.dossier_operation_logs.last }
let(:reason) { :user_request }
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(dossier.user) }
subject! { dossier.delete_and_keep_track!(dossier.user, reason) }
context 'brouillon' do
let(:dossier) { create(:dossier) }
it 'hides the dossier' do
expect(dossier.discarded?).to be_truthy
end
it 'do not creates a DeletedDossier record' do
expect(deleted_dossier).to be_nil
end
it 'do not records the operation in the log' do
expect(last_operation).to be_nil
end
end
context 'en_construction' do
it 'hides the dossier' do
expect(dossier.hidden_at).to be_present
end
it 'creates a DeletedDossier record' do
expect(deleted_dossier.reason).to eq DeletedDossier.reasons.fetch(reason)
expect(deleted_dossier.dossier_id).to eq dossier.id
expect(deleted_dossier.procedure).to eq dossier.procedure
expect(deleted_dossier.state).to eq dossier.state
@ -721,6 +740,33 @@ describe Dossier do
expect(DossierMailer).not_to have_received(:notify_deletion_to_administration)
end
end
context 'with reason: manager_request' do
let(:reason) { :manager_request }
it 'hides the dossier' do
expect(dossier.discarded?).to be_truthy
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
end
context 'with reason: user_removed' do
let(:reason) { :user_removed }
it 'hides the dossier' do
expect(dossier.discarded?).to be_truthy
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
end
end
end
describe 'webhook' do

View file

@ -264,7 +264,7 @@ describe User, type: :model do
user.delete_and_keep_track_dossiers(administration)
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
expect(User.find_by(id: user.id)).to be_nil
end
end
@ -278,16 +278,16 @@ describe User, type: :model do
end
it "keep track of dossiers and delete user" do
dossier_cache.delete_and_keep_track(administration)
dossier_cache.delete_and_keep_track!(administration, :user_request)
user.delete_and_keep_track_dossiers(administration)
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
expect(User.find_by(id: user.id)).to be_nil
end
it "doesn't destroy dossiers of another user" do
dossier_cache.delete_and_keep_track(administration)
dossier_cache.delete_and_keep_track!(administration, :user_request)
user.delete_and_keep_track_dossiers(administration)
expect(Dossier.find_by(id: dossier_from_another_user.id)).to be_present

View file

@ -34,7 +34,7 @@ describe ExpiredDossiersDeletionService do
it 'deletes and notify expired brouillon' do
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with([expired_brouillon.hash_for_deletion_mail], expired_brouillon.user.email)
expect(DeletedDossier.find_by(dossier_id: expired_brouillon.id)).to be_present
expect(DeletedDossier.find_by(dossier_id: expired_brouillon.id)).not_to be_present
expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end