2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-12 15:39:51 +02:00
|
|
|
require Rails.root.join("lib", "tasks", "task_helper")
|
|
|
|
|
2023-10-16 17:19:31 +02:00
|
|
|
namespace :data_fixer do
|
2023-10-12 15:39:51 +02:00
|
|
|
desc <<~EOD
|
2023-10-16 17:25:06 +02:00
|
|
|
Given a procedure_id in argument, run the DataFixer::ChampsPhoneInvalid.
|
2023-10-16 17:19:31 +02:00
|
|
|
ex: rails data_fixer:fix_phones\[1\]
|
2023-10-12 15:39:51 +02:00
|
|
|
EOD
|
2023-10-16 17:19:31 +02:00
|
|
|
task :fix_phones, [:procedure_id] => :environment do |_t, args|
|
2023-10-12 15:39:51 +02:00
|
|
|
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?)
|
|
|
|
|
2023-10-16 17:25:06 +02:00
|
|
|
fixable_phone_champs = invalid_phone_champs.filter { |phone| DataFixer::ChampsPhoneInvalid.fixable?(phone.value) }
|
2023-10-12 15:39:51 +02:00
|
|
|
|
|
|
|
fixable_phone_champs.each do |phone|
|
|
|
|
fixable_phone_value = phone.value
|
2023-10-16 17:25:06 +02:00
|
|
|
fixed_phone_value = DataFixer::ChampsPhoneInvalid.fix(fixable_phone_value)
|
2023-10-12 15:39:51 +02:00
|
|
|
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
|
2023-10-16 17:20:17 +02:00
|
|
|
|
|
|
|
desc <<~EOD
|
|
|
|
Given a dossier_id in argument, run the DossierChampsMissing.
|
|
|
|
ex: rails data_fixer:dossier_missing_champ\[1\]
|
|
|
|
EOD
|
|
|
|
task :dossier_missing_champ, [:dossier_id] => :environment do |_t, args|
|
|
|
|
dossier = Dossier.find(args[:dossier_id])
|
|
|
|
result = DataFixer::DossierChampsMissing.new(dossier:).fix
|
|
|
|
|
|
|
|
if result > 0
|
|
|
|
rake_puts "Dossier#[#{args[:dossier_id]}] fixed"
|
|
|
|
else
|
|
|
|
rake_puts "Dossier#[#{args[:dossier_id]}] not fixed"
|
|
|
|
end
|
|
|
|
end
|
2023-10-12 15:39:51 +02:00
|
|
|
end
|