task(phone): add task to fix invalid phones
This commit is contained in:
parent
a960395edb
commit
1ecd05df54
2 changed files with 29 additions and 2 deletions
29
lib/tasks/phone_fixer.rake
Normal file
29
lib/tasks/phone_fixer.rake
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
require Rails.root.join("lib", "tasks", "task_helper")
|
||||||
|
|
||||||
|
namespace :phone_fixer do
|
||||||
|
desc <<~EOD
|
||||||
|
Given a procedure_id in argument, run the PhoneFixer.
|
||||||
|
ex: rails phone_fixer:run\[1\]
|
||||||
|
EOD
|
||||||
|
task :run, [:procedure_id] => :environment do |_t, args|
|
||||||
|
procedure = Procedure.find(args[:procedure_id])
|
||||||
|
|
||||||
|
phone_champs = Champ
|
||||||
|
.where(dossier_id: procedure.dossiers.pluck(:id))
|
||||||
|
.where(type: "Champs::PhoneChamp")
|
||||||
|
|
||||||
|
invalid_phone_champs = phone_champs.reject(&:valid?)
|
||||||
|
|
||||||
|
fixable_phone_champs = invalid_phone_champs.filter { |phone| PhoneFixer.fixable?(phone.value) }
|
||||||
|
|
||||||
|
fixable_phone_champs.each do |phone|
|
||||||
|
fixable_phone_value = phone.value
|
||||||
|
fixed_phone_value = PhoneFixer.fix(fixable_phone_value)
|
||||||
|
if phone.update(value: fixed_phone_value)
|
||||||
|
rake_puts "Invalid phone #{fixable_phone_value} is fixed as #{fixed_phone_value}"
|
||||||
|
else
|
||||||
|
rake_puts "Failed to fix #{fixable_phone_value}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,6 +1,5 @@
|
||||||
describe PhoneFixer do
|
describe PhoneFixer do
|
||||||
describe '#fix' do
|
describe '#fix' do
|
||||||
|
|
||||||
subject { described_class.fix(phone_str) }
|
subject { described_class.fix(phone_str) }
|
||||||
|
|
||||||
context 'when separated evenly with space between and after dash' do
|
context 'when separated evenly with space between and after dash' do
|
||||||
|
@ -22,7 +21,6 @@ describe PhoneFixer do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#fixable' do
|
describe '#fixable' do
|
||||||
|
|
||||||
subject { described_class.fixable?(phone_str) }
|
subject { described_class.fixable?(phone_str) }
|
||||||
|
|
||||||
context 'when separated evenly with space between and after dash' do
|
context 'when separated evenly with space between and after dash' do
|
||||||
|
|
Loading…
Reference in a new issue