From ae017d79372b30f6acb3fb47a1d4ab45e1aade23 Mon Sep 17 00:00:00 2001 From: phil-l-brockwell Date: Mon, 11 Nov 2024 12:21:18 +0000 Subject: [PATCH 1/2] Add phonelib gem --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 5868b4d4c..5fff468a5 100644 --- a/Gemfile +++ b/Gemfile @@ -82,6 +82,7 @@ gem "simple_form", "~> 5.0" gem "country_select", "~> 3.1" gem "email_validator" gem "enumerize" +gem "phonelib" # PDF generation gem "prawn" diff --git a/Gemfile.lock b/Gemfile.lock index 0eefc5ca0..588d1845f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -403,6 +403,7 @@ GEM pg_search (2.3.6) activerecord (>= 5.2) activesupport (>= 5.2) + phonelib (0.9.3) poltergeist (1.18.1) capybara (>= 2.1, < 4) cliver (~> 0.3.1) @@ -780,6 +781,7 @@ DEPENDENCIES pdf-inspector pg pg_search (~> 2.3.3) + phonelib poltergeist prawn prawn-table From 196acb2c7e982358f9009de43c24332e0bcb719a Mon Sep 17 00:00:00 2001 From: phil-l-brockwell Date: Tue, 5 Nov 2024 17:21:43 +0000 Subject: [PATCH 2/2] Add rake task to export/remove invalid user phone numbers --- .../remove_invalid_user_phone_numbers.rake | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/tasks/temprary/remove_invalid_user_phone_numbers.rake diff --git a/lib/tasks/temprary/remove_invalid_user_phone_numbers.rake b/lib/tasks/temprary/remove_invalid_user_phone_numbers.rake new file mode 100644 index 000000000..01bbe5093 --- /dev/null +++ b/lib/tasks/temprary/remove_invalid_user_phone_numbers.rake @@ -0,0 +1,47 @@ +namespace :db do + desc "Remove invalid user phone_numbers" + task remove_invalid_user_phone_numbers: :environment do + User.find_each do |user| + invalid = false + + if user.phone_number && Phonelib.invalid_for_country?(user.phone_number, "GB") + user.phone_number = nil + invalid = true + end + + if user.company_phone_number && Phonelib.invalid_for_country?(user.company_phone_number, "GB") + user.company_phone_number = nil + invalid = true + end + + user.save! if invalid + end + end + + desc "Export users with invalid phone numbers" + task export_user_phone_numbers: :environment do + CSV.open("user_phone_numbers.csv", "w") do |csv| + csv << ["Email", "Name", "Number", "Field"] + + User.find_each do |user| + if user.phone_number && Phonelib.invalid_for_country?(user.phone_number, "GB") + csv << [ + user.email, + user.full_name, + user.phone_number, + "phone_number", + ] + end + + if user.company_phone_number && Phonelib.invalid_for_country?(user.company_phone_number, "GB") + csv << [ + user.email, + user.full_name, + user.company_phone_number, + "company_phone_number", + ] + end + end + end + end +end