From bb3dcbbf221cf10d507119ff619f681a7d1feae7 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Tue, 5 Jun 2018 17:00:22 +0200 Subject: [PATCH] Resend attestation for a procedure whose attestation template changed --- app/mailers/resend_attestation_mailer.rb | 29 +++++++++++++++++++++ lib/tasks/2018_06_05_resend_attestations.rb | 18 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 app/mailers/resend_attestation_mailer.rb create mode 100644 lib/tasks/2018_06_05_resend_attestations.rb diff --git a/app/mailers/resend_attestation_mailer.rb b/app/mailers/resend_attestation_mailer.rb new file mode 100644 index 000000000..5997c12b9 --- /dev/null +++ b/app/mailers/resend_attestation_mailer.rb @@ -0,0 +1,29 @@ +module Mailers + class ResendAttestationMailer < ApplicationMailer + include Rails.application.routes.url_helpers + + def resend_attestation(dossier) + to = dossier.user.email + subject = "Nouvelle attestation pour votre dossier nº #{dossier.id}" + + mail(to: to, subject: subject, body: body(dossier)) + end + + private + + def body(dossier) + <<~HEREDOC + Bonjour, + + L'attestation de votre dossier nº #{dossier.id} (procédure "#{dossier.procedure.libelle}") a été modifiée. + + Votre nouvelle attestation est disponible à l'adresse suivante : + #{dossier_attestation_url(dossier)} + + Cordialement, + + L’équipe demarches-simplifiees.fr + HEREDOC + end + end +end diff --git a/lib/tasks/2018_06_05_resend_attestations.rb b/lib/tasks/2018_06_05_resend_attestations.rb new file mode 100644 index 000000000..37f56b0b6 --- /dev/null +++ b/lib/tasks/2018_06_05_resend_attestations.rb @@ -0,0 +1,18 @@ +namespace :'2018_06_05_resend_attestation' do + task set: :environment do + procedure = Procedure.find(4247) + dossiers = procedure.dossiers.includes(:attestation).where(state: 'accepte').select do |d| + d.processed_at < procedure.attestation_template.updated_at + end + + dossiers.each do |dossier| + attestation = dossier.attestation + attestation.destroy + + dossier.attestation = dossier.build_attestation + + Mailers::ResendAttestationMailer.resend_attestation(dossier).deliver_later + puts "Email envoyé à #{email} pour le dossier #{dossier.id}" + end + end +end