From 3014ded9c082daa2e6c8ae53b5fb973d84d0a604 Mon Sep 17 00:00:00 2001 From: Quentin Champenois Date: Sat, 18 Jan 2025 17:43:02 +0100 Subject: [PATCH] fix: Add Archive Phone Users --- app/jobs/archive_users_phone_job.rb | 80 +++++++++++++++++++++++++++ lib/tasks/clear_duplicated_users.rake | 2 - lib/tasks/db.rake | 7 +++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 app/jobs/archive_users_phone_job.rb diff --git a/app/jobs/archive_users_phone_job.rb b/app/jobs/archive_users_phone_job.rb new file mode 100644 index 0000000000..01f50e4662 --- /dev/null +++ b/app/jobs/archive_users_phone_job.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +class ArchiveUsersPhoneJob < ApplicationJob + include Decidim::Logging + + def perform + metrics = { total: 0, quick_auth_users: 0, decidim_users: 0 } + log! "Start clearing phone numbers from accounts..." + + users_to_archive.find_in_batches(batch_size: 1000) do |users| + users.each do |user| + metrics[:total] += 1 + + if user.email.include?("quick_auth") + metrics[:quick_auth_users] += 1 + soft_delete_user(user, delete_reason) + else + metrics[:decidim_users] += 1 + clear_account_phone_number(user) + end + end + end + + log! "Total distinct numbers to clear : #{metrics[:total]}" + log! "Half signup users archived : #{metrics[:quick_auth_users]}" + log! "Decidim users account updated : #{metrics[:decidim_users]}" + log! "Terminated !" + end + + private + + def users_to_archive + Decidim::User.where.not(phone_number: [nil, ""]).where.not(phone_country: [nil, ""]) + end + + def soft_delete_user(user, reason) + email = user.email + phone = user.phone_number + user.extended_data = user.extended_data.merge({ + half_signup: { + email: email, + phone_number: phone, + phone_country: user.phone_country + } + }) + + user.phone_number = nil + user.phone_country = nil + + form = Decidim::DeleteAccountForm.from_params(delete_reason: reason) + Decidim::DestroyAccount.call(user, form) do + on(:invalid) do + log!("User (ID/#{user.id} email/#{email} phone/#{obfuscate_phone_number(phone)}) cannot be deleted: #{form.errors.full_messages}") + end + end + end + + def clear_account_phone_number(user) + Decidim::User.transaction do + user.extended_data = user.extended_data.merge({ + half_signup: { + phone_number: user.phone_number, + phone_country: user.phone_country + } + }) + + user.phone_number = nil + user.phone_country = nil + user.save(validate: false) + end + end + + def current_date + Date.current.strftime "%Y-%m-%d" + end + + def delete_reason + "Archived account - #{current_date}" + end +end diff --git a/lib/tasks/clear_duplicated_users.rake b/lib/tasks/clear_duplicated_users.rake index ba042895b7..dcccdfe3e8 100644 --- a/lib/tasks/clear_duplicated_users.rake +++ b/lib/tasks/clear_duplicated_users.rake @@ -3,8 +3,6 @@ namespace :decidim do desc "Clear duplicated users with the same phone_numbers in the database" task clear_duplicated_users: :environment do - include Decidim::Logging - ClearDuplicatedHalfSignupUsersJob.perform_now end end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 29aa16b9e3..ab2bd5e775 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -50,6 +50,13 @@ namespace :decidim do end end + namespace :users do + desc "Clean users phone numbers" + task phone: :environment do + ArchiveUsersPhoneJob.perform_now + end + end + namespace :restore do desc "Clear database dump to work with localhost" task local: :environment do