2018-09-12 17:57:00 +02:00
|
|
|
namespace :'2018_09_12_fix_templates' do
|
|
|
|
task run: :environment do
|
|
|
|
dossiers_with_invalid_attestations = find_dossiers_with_sent_and_invalid_attestations
|
|
|
|
fix_templates
|
2018-09-18 10:33:37 +02:00
|
|
|
fixed_attestations = delete_then_regenerate_attestations(dossiers_with_invalid_attestations)
|
2018-09-12 17:57:00 +02:00
|
|
|
send_regenerated_attestations(dossiers_with_invalid_attestations)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 16:15 in Paris -> 14:15 UTC
|
2018-10-25 17:03:03 +02:00
|
|
|
|
|
|
|
# rubocop:disable Rails/TimeZone
|
|
|
|
# because we are in a ruby context so Time.zone = nil
|
|
|
|
DEPLOY_DATETIME = Time.local(2018, 9, 5, 14, 15, 0)
|
|
|
|
# rubocop:enable Rails/TimeZone
|
2018-09-12 17:57:00 +02:00
|
|
|
|
|
|
|
def find_dossiers_with_sent_and_invalid_attestations
|
|
|
|
invalid_procedures_ids = AttestationTemplate
|
|
|
|
.where("body LIKE '%--libellé procédure--%'")
|
|
|
|
.pluck(:procedure_id)
|
|
|
|
|
|
|
|
dossiers_with_invalid_template_ids = Dossier
|
|
|
|
.where(procedure_id: invalid_procedures_ids)
|
2018-10-25 15:11:12 +02:00
|
|
|
.where(processed_at: DEPLOY_DATETIME..Time.zone.now)
|
2018-09-12 17:57:00 +02:00
|
|
|
.pluck(:id)
|
|
|
|
|
|
|
|
Attestation
|
|
|
|
.includes(:dossier)
|
2018-10-25 15:11:12 +02:00
|
|
|
.where(created_at: DEPLOY_DATETIME..Time.zone.now)
|
2018-09-12 17:57:00 +02:00
|
|
|
.where(dossier_id: dossiers_with_invalid_template_ids)
|
|
|
|
.map(&:dossier)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fix_templates
|
|
|
|
klasses = [
|
|
|
|
Mails::ClosedMail,
|
|
|
|
Mails::InitiatedMail,
|
|
|
|
Mails::ReceivedMail,
|
|
|
|
Mails::RefusedMail,
|
|
|
|
Mails::WithoutContinuationMail,
|
|
|
|
AttestationTemplate
|
|
|
|
]
|
|
|
|
|
|
|
|
klasses.each do |klass|
|
|
|
|
klass
|
|
|
|
.where("body LIKE '%--libellé procédure--%'")
|
|
|
|
.each do |instance|
|
|
|
|
|
|
|
|
instance.update(body: instance.body.gsub("--libellé procédure--", "--libellé démarche--"))
|
|
|
|
rake_puts "Body mis-à-jour pour #{klass.to_s}##{instance.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_then_regenerate_attestations(dossiers_with_invalid_attestations)
|
|
|
|
dossiers_with_invalid_attestations.each do |dossier|
|
|
|
|
begin
|
|
|
|
dossier.attestation.destroy
|
|
|
|
|
|
|
|
dossier.attestation = dossier.build_attestation
|
|
|
|
dossier.save
|
|
|
|
|
|
|
|
rake_puts "Attestation regénérée pour le dossier #{dossier.id}"
|
|
|
|
rescue
|
|
|
|
rake_puts "Erreur lors de la régénération de l'attestation pour le dossier #{dossier.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_regenerated_attestations(dossiers_with_invalid_attestations)
|
|
|
|
dossiers_with_invalid_attestations.each do |dossier|
|
|
|
|
begin
|
|
|
|
ResendAttestationMailer.resend_attestation(dossier).deliver_later
|
|
|
|
rake_puts "Email envoyé à #{dossier.user.email} pour le dossier #{dossier.id}"
|
|
|
|
rescue
|
|
|
|
rake_puts "Erreur lors de l'envoi de l'email à #{dossier.user.email} pour le dossier #{dossier.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|