From 4e801b188cd73ad461e1e85281ab04d30ba94a48 Mon Sep 17 00:00:00 2001
From: Pierre de La Morinerie |<\/p>||<\/small>||<\/b>| )*(Veuillez)?(Merci)?( |\r|\n)*(de)? ne pas répondre(.*)$/m
+ # When the footer contains any of these words, it is kept untouched.
+ FOOTER_EXCEPTIONS = [
+ 'PDF',
+ '@',
+ 'Hadrien',
+ 'Esther',
+ 'Sicoval',
+ 'a323',
+ 'SNC',
+ 'Polynésie',
+ 'drac',
+ 'theplatform'
+ ]
+
+ desc 'Deployment task: remove_footer_from_email_templates'
+ task remove_footer_from_email_templates: :environment do
+ rake_puts "Running deploy task 'remove_footer_from_email_templates'"
+
+ models = [
+ Mails::ClosedMail,
+ Mails::InitiatedMail,
+ Mails::ReceivedMail,
+ Mails::RefusedMail,
+ Mails::WithoutContinuationMail
+ ]
+
+ models.each do |model_class|
+ model_class.all.find_each do |template|
+ remove_footer(template)
+ end
+ end
+
+ # Update task as completed. If you remove the line below, the task will
+ # run with every deploy (or every time you call after_party:run).
+ AfterParty::TaskRecord.create version: '20181120133842'
+ end # task :remove_footer_from_email_templates
+
+ def remove_footer(template)
+ matches = template.body.match(FOOTER_REGEXP)
+ if matches && FOOTER_EXCEPTIONS.none? { |exception| matches[0].include?(exception) }
+ rake_puts "#{template.model_name.to_s} \##{template.id}: removing footer"
+ template.update(body: matches.pre_match)
+ end
+ end
+end # namespace :after_party
diff --git a/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb b/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb
new file mode 100644
index 000000000..d1a914240
--- /dev/null
+++ b/spec/lib/tasks/deployment/20181120133842_remove_footer_from_email_templates.rake_spec.rb
@@ -0,0 +1,72 @@
+describe '20181120133842_remove_footer_from_email_templates.rake' do
+ let(:rake_task) { Rake::Task['after_party:remove_footer_from_email_templates'] }
+ let(:templates) do
+ bodies.map { |body| create(:received_mail, body: body) }
+ end
+
+ before do
+ templates
+ end
+
+ subject! do
+ rake_task.invoke
+ end
+
+ after do
+ rake_task.reenable
+ end
+
+ context 'when emails have "do not reply" footers' do
+ let(:bodies) do
+ [
+ " Some content
|
\r\n
\r\nMerci de ne pas répondre à cet email. Postez directement vos questions dans\r\nvotre dossier sur la plateforme.
Some content
-Some content
—Some content
---Some content
—Some content
---Some content
---\r\n\r\n
\r\n
Some content
---Some content
---\r\n\r\n
\r\n
Some content
---Some content
' + end + end + end + + context 'when emails don’t have the standard boilerplate in the footer' do + let(:bodies) do + [ + "Some content.
Merci, l'équipe demarches-simplifiees.fr.\r\n
", + "Some content.
Merci, l'équipe TPS.\r\n
" + ] + end + + it 'keeps the footer' do + templates.each do |template| + expect(bodies).to include(template.reload.body) + end + end + end + + context 'when the footer contains some excluded strings' do + let(:bodies) do + [ + "Some content
---Some content
---Some content
---