Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add Archive Phone Users #660

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading