From 8ce79f7d5eeb817a8e617694c93d6f46200561a0 Mon Sep 17 00:00:00 2001 From: Cyrille Bollu Date: Tue, 23 Apr 2024 11:55:25 +0200 Subject: [PATCH] Adds a rake task to delete all users created more than 1 year ago and who never had any activity Signed-off-by: Cyrille Bollu --- lib/tasks/belnet.rake | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/tasks/belnet.rake diff --git a/lib/tasks/belnet.rake b/lib/tasks/belnet.rake new file mode 100644 index 00000000..aadba9fa --- /dev/null +++ b/lib/tasks/belnet.rake @@ -0,0 +1,27 @@ +namespace :belnet do + + namespace :users do + + desc "Deletes users who never had any activity, after some time" + task :clean => :environment do + + User.where(last_sign_in_at:nil).find_each do |user| + + # Only consider users that have been created more than 1 year ago + if user.created_at.year < ( Date.today.year - 1 ) + + # Unlink user from any existing plan + role = Role.where(user_id:user.id).destroy_all + + # Deletes user + user.destroy + + end + + end + + end + + end + +end