Skip to content

Commit

Permalink
Merge pull request #660 from OpenSourcePolitics/feat/archive_phone_users
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois authored Jan 18, 2025
2 parents 5a4a097 + 3014ded commit fe1fd06
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
80 changes: 80 additions & 0 deletions app/jobs/archive_users_phone_job.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions lib/tasks/clear_duplicated_users.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe1fd06

Please sign in to comment.