amelioration(user.expiration): ajoute une raison specifique a la suppression d'un dossier : user_expired

This commit is contained in:
Martin 2023-11-17 10:22:34 +01:00 committed by mfo
parent bfe27b5eb7
commit 520863d814
6 changed files with 16 additions and 15 deletions

View file

@ -56,7 +56,7 @@ module Manager
if !user.can_be_deleted? if !user.can_be_deleted?
fail "Impossible de supprimer cet utilisateur. Il a des dossiers en instruction ou il est administrateur." fail "Impossible de supprimer cet utilisateur. Il a des dossiers en instruction ou il est administrateur."
end end
user.delete_and_keep_track_dossiers_also_delete_user(current_super_admin) user.delete_and_keep_track_dossiers_also_delete_user(current_super_admin, reason: :user_removed)
logger.info("L'utilisateur #{user.id} est supprimé par #{current_super_admin.id}") logger.info("L'utilisateur #{user.id} est supprimé par #{current_super_admin.id}")
flash[:notice] = "L'utilisateur #{user.id} est supprimé" flash[:notice] = "L'utilisateur #{user.id} est supprimé"

View file

@ -12,7 +12,8 @@ class DeletedDossier < ApplicationRecord
user_removed: 'user_removed', user_removed: 'user_removed',
procedure_removed: 'procedure_removed', procedure_removed: 'procedure_removed',
expired: 'expired', expired: 'expired',
instructeur_request: 'instructeur_request' instructeur_request: 'instructeur_request',
user_expired: 'user_expired'
} }
enum state: { enum state: {

View file

@ -176,7 +176,7 @@ class User < ApplicationRecord
!administrateur? && !instructeur? && !expert? !administrateur? && !instructeur? && !expert?
end end
def delete_and_keep_track_dossiers_also_delete_user(super_admin) def delete_and_keep_track_dossiers_also_delete_user(super_admin, reason:)
if !can_be_deleted? if !can_be_deleted?
raise "Cannot delete this user because they are also instructeur, expert or administrateur" raise "Cannot delete this user because they are also instructeur, expert or administrateur"
end end
@ -185,29 +185,29 @@ class User < ApplicationRecord
# delete invites # delete invites
Invite.where(dossier: dossiers).destroy_all Invite.where(dossier: dossiers).destroy_all
delete_and_keep_track_dossiers(super_admin) delete_and_keep_track_dossiers(super_admin, reason: :user_removed)
destroy! destroy!
end end
end end
def delete_and_keep_track_dossiers(super_admin) def delete_and_keep_track_dossiers(super_admin, reason:)
transaction do transaction do
# delete dossiers brouillon # delete dossiers brouillon
dossiers.state_brouillon.each do |dossier| dossiers.state_brouillon.each do |dossier|
dossier.hide_and_keep_track!(dossier.user, :user_removed) dossier.hide_and_keep_track!(dossier.user, reason)
end end
dossiers.state_brouillon.find_each(&:purge_discarded) dossiers.state_brouillon.find_each(&:purge_discarded)
# delete dossiers en_construction # delete dossiers en_construction
dossiers.state_en_construction.each do |dossier| dossiers.state_en_construction.each do |dossier|
dossier.hide_and_keep_track!(dossier.user, :user_removed) dossier.hide_and_keep_track!(dossier.user, reason)
end end
dossiers.state_en_construction.find_each(&:purge_discarded) dossiers.state_en_construction.find_each(&:purge_discarded)
# delete dossiers terminé # delete dossiers terminé
dossiers.state_termine.each do |dossier| dossiers.state_termine.each do |dossier|
dossier.hide_and_keep_track!(dossier.user, :user_removed) dossier.hide_and_keep_track!(dossier.user, reason)
end end
dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil) dossiers.update_all(deleted_user_email_never_send: email, user_id: nil, dossier_transfer_id: nil)
end end

View file

@ -23,7 +23,7 @@ class Expired::UsersDeletionService < Expired::MailRateLimiter
def delete_notified_users(users) def delete_notified_users(users)
only_notified(users).find_each do |user| only_notified(users).find_each do |user|
begin begin
user.delete_and_keep_track_dossiers_also_delete_user(nil) user.delete_and_keep_track_dossiers_also_delete_user(nil, reason: :user_expired)
rescue => e rescue => e
Sentry.capture_exception(e, extra: { user_id: user.id }) Sentry.capture_exception(e, extra: { user_id: user.id })
end end

View file

@ -15,7 +15,7 @@ namespace :support do
user = User.find_by!(email: user_email) user = User.find_by!(email: user_email)
administration = Administration.find_by!(email: administration_email) administration = Administration.find_by!(email: administration_email)
user.delete_and_keep_track_dossiers_also_delete_user(administration) user.delete_and_keep_track_dossiers_also_delete_user(administration, reason: :user_removed)
user.destroy user.destroy
end end
@ -59,7 +59,7 @@ namespace :support do
# remove all the other dossier from the user side # remove all the other dossier from the user side
rake_puts "hide #{user.reload.dossiers.count} dossiers" rake_puts "hide #{user.reload.dossiers.count} dossiers"
user.delete_and_keep_track_dossiers(super_admin) user.delete_and_keep_track_dossiers(super_admin, reason: :user_removed)
owned_procedures, shared_procedures = user.administrateur owned_procedures, shared_procedures = user.administrateur
.procedures .procedures

View file

@ -301,14 +301,14 @@ describe User, type: :model do
describe '#delete_and_keep_track_dossiers_also_delete_user' do describe '#delete_and_keep_track_dossiers_also_delete_user' do
let(:super_admin) { create(:super_admin) } let(:super_admin) { create(:super_admin) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:reason) { :user_rmoved }
context 'without a dossier with processing strted' do context 'without a dossier with processing strted' do
let!(:dossier_en_construction) { create(:dossier, :en_construction, user: user) } let!(:dossier_en_construction) { create(:dossier, :en_construction, user: user) }
let!(:dossier_brouillon) { create(:dossier, user: user) } let!(:dossier_brouillon) { create(:dossier, user: user) }
context 'without a discarded dossier' do context 'without a discarded dossier' do
it "keep track of dossiers and delete user" do it "keep track of dossiers and delete user" do
user.delete_and_keep_track_dossiers_also_delete_user(super_admin) user.delete_and_keep_track_dossiers_also_delete_user(super_admin, reason:)
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
@ -322,7 +322,7 @@ describe User, type: :model do
it "keep track of dossiers and delete user" do it "keep track of dossiers and delete user" do
dossier_to_delete.hide_and_keep_track!(user, :user_request) dossier_to_delete.hide_and_keep_track!(user, :user_request)
user.delete_and_keep_track_dossiers_also_delete_user(super_admin) user.delete_and_keep_track_dossiers_also_delete_user(super_admin, reason:)
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
@ -337,7 +337,7 @@ describe User, type: :model do
let!(:dossier_termine) { create(:dossier, :accepte, user: user) } let!(:dossier_termine) { create(:dossier, :accepte, user: user) }
it "keep track of dossiers and delete user" do it "keep track of dossiers and delete user" do
user.delete_and_keep_track_dossiers_also_delete_user(super_admin) user.delete_and_keep_track_dossiers_also_delete_user(super_admin, reason:)
expect(dossier_en_instruction.reload).to be_present expect(dossier_en_instruction.reload).to be_present
expect(dossier_en_instruction.user).to be_nil expect(dossier_en_instruction.user).to be_nil