Merge pull request #9279 from colinux/fix-email-default-template

Correctif: utilise le template d'email de notification par défaut quand la démarche n'a pas personnalisé le sien
This commit is contained in:
Colin Darie 2023-07-06 09:30:04 +00:00 committed by GitHub
commit 6fa7ed1c1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 15 deletions

View file

@ -34,12 +34,12 @@ module MailTemplateConcern
before_save :update_rich_body
end
module ClassMethods
class_methods do
def default_for_procedure(procedure)
template_name = default_template_name_for_procedure(procedure)
rich_body = ActionController::Base.render template: template_name
trix_rich_body = rich_body.gsub(/(?<!^|[.-])(?<!<\/strong>)\n/, ' ')
new(subject: const_get(:DEFAULT_SUBJECT), rich_body: trix_rich_body, procedure: procedure)
new(subject: const_get(:DEFAULT_SUBJECT), body: trix_rich_body, rich_body: trix_rich_body, procedure: procedure)
end
def default_template_name_for_procedure(procedure)

View file

@ -1,24 +1,37 @@
RSpec.describe NotificationMailer, type: :mailer do
let(:administrateur) { create(:administrateur) }
let(:user) { create(:user) }
let(:procedure) { create(:simple_procedure) }
let(:procedure) { create(:simple_procedure, :with_service) }
describe 'send_en_construction_notification' do
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:initiated_mail, subject: 'Email subject', body: 'Your dossier was received. Thanks.') }
let(:dossier) { create(:dossier, :en_construction, :with_individual, user: user, procedure: procedure) }
subject(:mail) { described_class.send_en_construction_notification(dossier) }
let(:body) { (mail.html_part || mail).body }
context "without custom template" do
it 'renders default template' do
expect(mail.subject).to eq("Votre dossier nº #{dossier.id} a bien été déposé (#{procedure.libelle})")
expect(body).to include("Votre dossier nº #{dossier.id}")
expect(body).to include(procedure.service.nom)
expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf")
end
end
context "with a custom template" do
let(:email_template) { create(:initiated_mail, subject: 'Email subject', body: 'Your dossier was received. Thanks.') }
before do
dossier.procedure.initiated_mail = email_template
end
subject(:mail) { described_class.send_en_construction_notification(dossier) }
it 'renders the template' do
expect(mail.subject).to eq('Email subject')
expect((mail.html_part || mail).body).to include('Your dossier was received')
expect(body).to include('Your dossier was received')
expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf")
end
end
end
describe 'send_en_instruction_notification' do
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }

View file

@ -1019,7 +1019,7 @@ describe Procedure do
procedure.reset_draft_revision!
expect { published_tdc.reload }.not_to raise_error(ActiveRecord::RecordNotFound)
expect { published_tdc.reload }.not_to raise_error
expect { draft_tdc.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end