Support task to delete a user account

This commit is contained in:
Frederic Merizen 2018-06-27 12:38:38 +02:00
parent 5af6d0511a
commit a429a38a38

View file

@ -31,4 +31,21 @@ namespace :support do
pp.update(administrateur: new_owner)
end
end
desc <<~EOD
Delete the user account for a given USER_MAIL.
Only works if the user has no dossier where the instruction has started.
EOD
task delete_user_account: :environment do
user_mail = ENV['USER_MAIL']
if user_mail.nil?
fail "Must specify a USER_MAIL"
end
user = User.find_by(email: user_mail)
if user.dossiers.state_instruction_commencee.any?
fail "Cannot delete this user because instruction has started for some dossiers"
end
user.dossiers.each { |d| d.delete_and_keep_track }
user.destroy
end
end