Skip to content

Commit

Permalink
fix: Clear job
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois committed Jan 13, 2025
1 parent 22c7339 commit 52df954
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
74 changes: 35 additions & 39 deletions app/jobs/clear_duplicated_half_signup_users_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
class ClearDuplicatedHalfSignupUsersJob < ApplicationJob
include Decidim::Logging

def perform(clear_user_accounts = false)
def perform
@dup_decidim_users_count = 0
@dup_half_signup_count = 0

log! "Start clearing half signup accounts..."
if duplicated_phone_numbers.blank?
log! "No duplicated phone numbers found"
return
end

log! "Found #{duplicated_phone_numbers.count} rows to cleanup"
alerts = duplicated_phone_numbers.map do |phone_info|
log! "Found #{duplicated_phone_numbers.count} duplicated phone number to cleanup"
duplicated_phone_numbers.each do |phone_info|
phone_number, phone_country = phone_info
users_with_phone = Decidim::User.where(phone_number: phone_number, phone_country: phone_country)
decidim_user_dup_accounts = []
users = Decidim::User.where(phone_number: phone_number, phone_country: phone_country)

clear_data(users_with_phone, clear_user_accounts)
generate_alert_message(phone_number, decidim_user_dup_accounts) if decidim_user_dup_accounts.map(&:email).uniq.size > 1
clear_data users
end

display_alerts alerts

log! "Total distinct numbers to clear : #{duplicated_phone_numbers.size}"
log! "Half signup users archived : #{@dup_half_signup_count}"
log! "Decidim users account updated : #{@dup_decidim_users_count}"
log! "Total accounts modified : #{@dup_half_signup_count + @dup_decidim_users_count}"
log! "Terminated !"
end

Expand All @@ -36,28 +39,30 @@ def duplicated_phone_numbers
.pluck(:phone_number, :phone_country)
end

def clear_data(users, clear_user_accounts)
def clear_data(users)
decidim_user_dup_accounts = []

users.each do |user|
if user.email.include?("quick_auth")
soft_delete_user(user, "HalfSignup duplicated account")
elsif clear_user_accounts
clear_account_phone_number(user)
@dup_half_signup_count += 1
soft_delete_user(user, delete_reason)
else
@dup_decidim_users_count += 1
decidim_user_dup_accounts << user
end
end

decidim_user_dup_accounts
end
return if decidim_user_dup_accounts.blank?
# The unique user might be a user without email, if so, it should be cleared
return if decidim_user_dup_accounts.size <= 1 && decidim_user_dup_accounts.first.email.present?

def soft_delete_user(user, reason)
unless user.email.include?("quick_auth")
log! "User (##{user.id}) - Decidim user, skipping deletion..."
return
# if there is multiple decidim user accounts, clear all phone number for these accounts
decidim_user_dup_accounts.each do |decidim_user|
clear_account_phone_number(decidim_user)
end
end

def soft_delete_user(user, reason)
email = user.email
phone = user.phone_number
user.extended_data = user.extended_data.merge({
Expand All @@ -70,6 +75,7 @@ def soft_delete_user(user, reason)

user.phone_number = nil
user.phone_country = nil

form = Decidim::DeleteAccountForm.from_params(delete_reason: reason)
Decidim::DestroyAccount.call(user, form) do
on(:ok) do
Expand All @@ -82,6 +88,7 @@ def soft_delete_user(user, reason)
end

def clear_account_phone_number(user)
phone_number = user.phone_number
Decidim::User.transaction do
user.extended_data = user.extended_data.merge({
half_signup: {
Expand All @@ -95,26 +102,7 @@ def clear_account_phone_number(user)
user.save(validate: false)
end

log! "User (ID/#{user.id} email/#{user.email}) has been cleaned"
end

def generate_alert_message(phone_number, users)
phone = obfuscate_phone_number(phone_number)
infos = users.map { |user| "(ID/#{user.id} email/#{user.email})" }

"#{phone} : #{infos.join(" | ")}"
end

def display_alerts(alerts)
alerts&.compact!
return if alerts.blank?

log! "Decidim users account to cleanup manually:"
alerts.each do |alert|
next if alert.blank?

log!(alert)
end
log! "User (ID/#{user.id} phone/#{obfuscate_phone_number(phone_number)} email/#{user.email}) has been cleaned"
end

def obfuscate_phone_number(phone_number)
Expand All @@ -126,4 +114,12 @@ def obfuscate_phone_number(phone_number)

visible_prefix + obfuscated_middle + visible_suffix
end

def current_date
Date.current.strftime "%Y-%m-%d"
end

def delete_reason
"HalfSignup duplicated account (#{current_date})"
end
end
4 changes: 1 addition & 3 deletions lib/tasks/clear_duplicated_users.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace :decidim do
task clear_duplicated_users: :environment do
include Decidim::Logging

clear_user_accounts = ENV.fetch("CLEAR_USER_ACCOUNTS", "false") == "true"
log!("HELP: Run task with env var CLEAR_USER_ACCOUNTS=true to clear Half Signup and Decidim Users phone numbers") unless clear_user_accounts
ClearDuplicatedHalfSignupUsersJob.perform_now(clear_user_accounts)
ClearDuplicatedHalfSignupUsersJob.perform_now
end
end
44 changes: 25 additions & 19 deletions spec/jobs/clear_duplicated_half_signup_users_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,64 @@

module Decidim
describe ClearDuplicatedHalfSignupUsersJob do
subject { described_class.perform_now }

let(:logger) { Logger.new(StringIO.new) }

let!(:user1) { create(:user, phone_number: "1234567890", phone_country: "US", email: "[email protected]") }
let!(:user2) { create(:user, phone_number: "1234567890", phone_country: "US", email: "[email protected]") }
let!(:user3) { create(:user, phone_number: "1234567890", phone_country: "US", email: "[email protected]") }
let!(:user2) { create(:user, phone_number: phonenumberuser2, phone_country: "US", email: "[email protected]") }
let!(:user3) { create(:user, phone_number: phonenumberuser3, phone_country: "US", email: "[email protected]") }
let!(:user4) { create(:user, phone_number: "9876543210", phone_country: "US", email: "[email protected]") }
let(:delete_reason) { "HalfSignup duplicated account" }
let(:delete_reason) { "HalfSignup duplicated account (#{current_date})" }
let(:current_date) { Date.current.strftime "%Y-%m-%d" }

let(:phonenumberuser2) { "1234567890" }
let(:phonenumberuser3) { "1234567890" }

before do
allow_any_instance_of(Decidim::Logging).to receive(:stdout_logger).and_return(logger)
allow_any_instance_of(Decidim::DestroyAccount).to receive(:call).and_return(true)
allow(Rails.logger).to receive(:warn)
allow(Rails.logger).to receive(:info)
end

describe "#perform" do
context "when no duplicated phone numbers are found" do
it "prints that no duplicated phone numbers are found" do
allow_any_instance_of(ClearDuplicatedHalfSignupUsersJob).to receive(:find_duplicated_phone_numbers).and_return([])
let(:phonenumberuser2) { "1234567891" }
let(:phonenumberuser3) { "1234567892" }

expect { described_class.perform_now }.to output(/No duplicated phone numbers found/).to_stdout
it "prints that no duplicated phone numbers are found" do
expect { subject }.to output(/No duplicated phone numbers found/).to_stdout
puts "ok"
end
end

context "when duplicated phone numbers are found" do
before do
allow_any_instance_of(ClearDuplicatedHalfSignupUsersJob).to receive(:find_duplicated_phone_numbers).and_return([%w(1234567890 US)])
allow_any_instance_of(ClearDuplicatedHalfSignupUsersJob).to receive(:duplicated_phone_numbers).and_return([%w(1234567890 US)])
end

it "soft deletes quick_auth users" do
expect_any_instance_of(ClearDuplicatedHalfSignupUsersJob).to receive(:soft_delete_user).with(user2, delete_reason)

described_class.perform_now
subject
end

it "alerts about duplicated phone numbers for non-quick_auth users" do
expect(Rails.logger).to receive(:warn).and_call_original
expect(Rails.logger).to receive(:warn).with(/ALERT: Duplicated Phone Number Detected/)

described_class.perform_now
subject
end

it "does not soft delete non quick_auth users" do
expect_any_instance_of(ClearDuplicatedHalfSignupUsersJob).not_to receive(:soft_delete_user).with(user1, delete_reason)

described_class.perform_now
subject
end

it "does not soft delete users with different phone numbers" do
expect_any_instance_of(ClearDuplicatedHalfSignupUsersJob).not_to receive(:soft_delete_user).with(user4, delete_reason)

described_class.perform_now
subject
end
end
end
Expand Down Expand Up @@ -111,13 +121,9 @@ module Decidim
end
end

describe "#find_duplicated_phone_numbers" do
describe "#duplicated_phone_numbers" do
it "finds duplicated phone numbers" do
user1
user2
user3

result = described_class.new.send(:find_duplicated_phone_numbers)
result = described_class.new.send(:duplicated_phone_numbers)

expect(result).to include(%w(1234567890 US))
end
Expand Down

0 comments on commit 52df954

Please sign in to comment.