From c25dba850e35c9d5babbce3d27b22cb483aede0a Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 26 Jan 2022 15:33:42 +0100 Subject: [PATCH] hotfix(attestation): add missing attestations on dossiers --- lib/tasks/fix_attestations.rake | 19 ------------------- lib/tasks/hotfix.rake | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) delete mode 100644 lib/tasks/fix_attestations.rake create mode 100644 lib/tasks/hotfix.rake diff --git a/lib/tasks/fix_attestations.rake b/lib/tasks/fix_attestations.rake deleted file mode 100644 index 09bce879c..000000000 --- a/lib/tasks/fix_attestations.rake +++ /dev/null @@ -1,19 +0,0 @@ -namespace :hotfix do - desc 'Fix attestation templates' - task attestation_templates: :environment do - attestation_templates = Rails.root.join('lib', 'tasks', 'attestation_templates.json') - file = File.read attestation_templates - json = JSON.parse file - progress = ProgressReport.new(json.size) - - json.each do |row| - attestation_template = AttestationTemplate.find_by(id: row['id']) - procedure = Procedure.find_by(id: row['procedure_id']) - if attestation_template.present? && procedure.present? - attestation_template.update_column(:procedure_id, procedure.id) - end - progress.inc - end - progress.finish - end -end diff --git a/lib/tasks/hotfix.rake b/lib/tasks/hotfix.rake new file mode 100644 index 000000000..e03c0f35e --- /dev/null +++ b/lib/tasks/hotfix.rake @@ -0,0 +1,22 @@ +namespace :hotfix do + desc 'Fix dossiers attestations' + task dossiers_attestations: :environment do + dossiers = Dossier + .joins(procedure: :attestation_template) + .left_outer_joins(:attestation) + .where(attestation_templates: { activated: true }, + attestations: { id: nil }, + state: "accepte") + .where("dossiers.processed_at > '2022-01-24'") + progress = ProgressReport.new(dossiers.count) + + dossiers.find_each do |dossier| + if dossier.attestation.blank? + dossier.attestation = dossier.build_attestation + dossier.save! + end + progress.inc + end + progress.finish + end +end