fix(email_template): less magic - less bugs !

This commit is contained in:
Paul Chavard 2023-11-29 11:59:55 +00:00
parent a330118929
commit 3de0367ed9
10 changed files with 72 additions and 79 deletions

View file

@ -54,12 +54,12 @@ module Administrateurs
def mail_templates
[
procedure.initiated_mail_template,
procedure.received_mail_template,
procedure.closed_mail_template,
procedure.refused_mail_template,
procedure.without_continuation_mail_template,
procedure.re_instructed_mail_template
procedure.passer_en_construction_email_template,
procedure.passer_en_instruction_email_template,
procedure.accepter_email_template,
procedure.refuser_email_template,
procedure.classer_sans_suite_email_template,
procedure.repasser_en_instruction_email_template
]
end

View file

@ -34,12 +34,12 @@ class ProcedureDashboard < Administrate::BaseDashboard
whitelisted_at: Field::DateTime,
hidden_at_as_template: Field::DateTime,
service: Field::BelongsTo,
initiated_mail_template: MailTemplateField,
received_mail_template: MailTemplateField,
closed_mail_template: MailTemplateField,
refused_mail_template: MailTemplateField,
without_continuation_mail_template: MailTemplateField,
re_instructed_mail_template: MailTemplateField,
passer_en_construction_email_template: MailTemplateField,
passer_en_instruction_email_template: MailTemplateField,
accepter_email_template: MailTemplateField,
refuser_email_template: MailTemplateField,
classer_sans_suite_email_template: MailTemplateField,
repasser_en_instruction_email_template: MailTemplateField,
attestation_template: AttestationTemplateField,
procedure_expires_when_termine_enabled: Field::Boolean,
duree_conservation_dossiers_dans_ds: Field::Number,
@ -95,12 +95,12 @@ class ProcedureDashboard < Administrate::BaseDashboard
:published_types_de_champ_private,
:for_individual,
:auto_archive_on,
:initiated_mail_template,
:received_mail_template,
:closed_mail_template,
:refused_mail_template,
:without_continuation_mail_template,
:re_instructed_mail_template,
:passer_en_construction_email_template,
:passer_en_instruction_email_template,
:accepter_email_template,
:refuser_email_template,
:classer_sans_suite_email_template,
:repasser_en_instruction_email_template,
:attestation_template,
:procedure_expires_when_termine_enabled,
:duree_conservation_dossiers_dans_ds,

View file

@ -45,11 +45,7 @@ class NotificationMailer < ApplicationMailer
end
def self.send_repasser_en_instruction_notification(dossier)
with(dossier: dossier, state: Dossier.states.fetch(:en_instruction)).send_notification
end
def self.send_pending_correction(dossier)
with(dossier: dossier).send_notification
with(dossier: dossier, state: DossierOperationLog.operations.fetch(:repasser_en_instruction)).send_notification
end
def self.critical_email?(action_name)
@ -71,14 +67,14 @@ class NotificationMailer < ApplicationMailer
mail.perform_deliveries = false
else
I18n.with_locale(@dossier.user_locale) do
mail_template = @dossier.mail_template_for_state
mail_template_presenter = MailTemplatePresenterService.new(@dossier)
email_template = @dossier.email_template_for(params[:state])
email_template_presenter = MailTemplatePresenterService.new(@dossier, params[:state])
@email = @dossier.user_email_for(:notification)
@rendered_template = mail_template_presenter.safe_body
@subject = mail_template_presenter.safe_subject
@actions = mail_template.actions_for_dossier(@dossier)
@attachment = mail_template.attachment_for_dossier(@dossier)
@rendered_template = email_template_presenter.safe_body
@subject = email_template_presenter.safe_subject
@actions = email_template.actions_for_dossier(@dossier)
@attachment = email_template.attachment_for_dossier(@dossier)
end
end
end

View file

@ -872,8 +872,8 @@ class Dossier < ApplicationRecord
end
end
def mail_template_for_state
procedure.mail_template_for(self)
def email_template_for(state)
procedure.email_template_for(state)
end
def after_passer_en_construction
@ -882,7 +882,7 @@ class Dossier < ApplicationRecord
.passer_en_construction
.processed_at
save!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_construction))
NotificationMailer.send_en_construction_notification(self).deliver_later
procedure.compute_dossiers_count
RoutingEngine.compute(self)
@ -913,7 +913,7 @@ class Dossier < ApplicationRecord
resolve_pending_correction!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_instruction))
if !disable_notification
NotificationMailer.send_en_instruction_notification(self).deliver_later
end
@ -930,7 +930,7 @@ class Dossier < ApplicationRecord
end
save!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_instruction))
NotificationMailer.send_en_instruction_notification(self).deliver_later
if procedure.sva_svr_enabled?
@ -977,7 +977,7 @@ class Dossier < ApplicationRecord
save!
rebase_later
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, DossierOperationLog.operations.fetch(:repasser_en_instruction))
if !disable_notification
NotificationMailer.send_repasser_en_instruction_notification(self).deliver_later
end
@ -1006,7 +1006,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:accepte))
if !disable_notification
NotificationMailer.send_accepte_notification(self).deliver_later
end
@ -1032,7 +1032,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:accepte))
NotificationMailer.send_accepte_notification(self).deliver_later
log_automatic_dossier_operation(:accepter, self)
end
@ -1055,7 +1055,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:refuse))
if !disable_notification
NotificationMailer.send_refuse_notification(self).deliver_later
@ -1076,7 +1076,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:refuse))
NotificationMailer.send_refuse_notification(self).deliver_later
log_automatic_dossier_operation(:refuser, self)
end
@ -1099,7 +1099,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:sans_suite))
if !disable_notification
NotificationMailer.send_sans_suite_notification(self).deliver_later

View file

@ -573,46 +573,44 @@ class Procedure < ApplicationRecord
ProcedureOverview.new(self, start_date, groups)
end
def initiated_mail_template
def passer_en_construction_email_template
initiated_mail || Mails::InitiatedMail.default_for_procedure(self)
end
def received_mail_template
def passer_en_instruction_email_template
received_mail || Mails::ReceivedMail.default_for_procedure(self)
end
def closed_mail_template
def accepter_email_template
closed_mail || Mails::ClosedMail.default_for_procedure(self)
end
def refused_mail_template
def refuser_email_template
refused_mail || Mails::RefusedMail.default_for_procedure(self)
end
def without_continuation_mail_template
def classer_sans_suite_email_template
without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self)
end
def re_instructed_mail_template
def repasser_en_instruction_email_template
re_instructed_mail || Mails::ReInstructedMail.default_for_procedure(self)
end
def mail_template_for(dossier)
case dossier.state
def email_template_for(state)
case state
when Dossier.states.fetch(:en_construction)
initiated_mail_template
passer_en_construction_email_template
when Dossier.states.fetch(:en_instruction)
if dossier.traitements.where(state: Dossier.states.fetch(:en_instruction)).one?
received_mail_template
else
re_instructed_mail_template
end
passer_en_instruction_email_template
when DossierOperationLog.operations.fetch(:repasser_en_instruction)
repasser_en_instruction_email_template
when Dossier.states.fetch(:accepte)
closed_mail_template
accepter_email_template
when Dossier.states.fetch(:refuse)
refused_mail_template
refuser_email_template
when Dossier.states.fetch(:sans_suite)
without_continuation_mail_template
classer_sans_suite_email_template
else
raise "Unknown dossier state: #{state}"
end

View file

@ -2,23 +2,22 @@ class MailTemplatePresenterService
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
delegate :mail_template_for_state, to: :@dossier
def self.create_commentaire_for_state(dossier)
service = new(dossier)
def self.create_commentaire_for_state(dossier, state)
service = new(dossier, state)
body = ["<p>[#{service.safe_subject}]</p>", service.safe_body].join('')
CommentaireService.create!(CONTACT_EMAIL, dossier, body: body)
end
def safe_body
sanitize(mail_template_for_state.body_for_dossier(@dossier), scrubber: Sanitizers::MailScrubber.new)
sanitize(@email_template.body_for_dossier(@dossier), scrubber: Sanitizers::MailScrubber.new)
end
def safe_subject
Nokogiri::HTML.parse(truncate(mail_template_for_state.subject_for_dossier(@dossier), length: 100)).text
Nokogiri::HTML.parse(truncate(@email_template.subject_for_dossier(@dossier), length: 100)).text
end
def initialize(dossier)
def initialize(dossier, state)
@dossier = dossier
@email_template = dossier.email_template_for(state)
end
end

View file

@ -59,7 +59,7 @@ describe Administrateurs::MailTemplatesController, type: :controller do
it { expect(response).to redirect_to edit_admin_procedure_mail_template_path(procedure, initiated_mail.class.const_get(:SLUG)) }
context 'with valid email template' do
subject { procedure.reload; procedure.initiated_mail_template }
subject { procedure.reload; procedure.passer_en_construction_email_template }
it do
expect(subject.subject).to eq(mail_subject)
@ -68,7 +68,7 @@ describe Administrateurs::MailTemplatesController, type: :controller do
end
context 'with invalid email template' do
subject { procedure.reload; procedure.initiated_mail_template }
subject { procedure.reload; procedure.passer_en_construction_email_template }
let(:mail_body) { '<div>Une mise à jour a été effectuée sur votre démarche n° --numéro--.</div>' }
it do

View file

@ -116,7 +116,7 @@ RSpec.describe NotificationMailer, type: :mailer do
dossier.procedure.received_mail = email_template
end
subject(:mail) { described_class.send_accepte_notification(dossier) }
subject(:mail) { described_class.send_en_instruction_notification(dossier) }
context "subject has a special character" do
let(:subject) { '--libellé démarche--' }

View file

@ -1177,7 +1177,7 @@ describe Dossier, type: :model do
it 'creates a commentaire in the messagerie with expected wording' do
passer_en_instruction
email_template = dossier.procedure.mail_template_for(dossier)
email_template = dossier.procedure.email_template_for(dossier.state)
commentaire = dossier.commentaires.last
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))

View file

@ -2,12 +2,12 @@ describe Procedure do
describe 'mail templates' do
subject { create(:procedure) }
it { expect(subject.initiated_mail_template).to be_a(Mails::InitiatedMail) }
it { expect(subject.received_mail_template).to be_a(Mails::ReceivedMail) }
it { expect(subject.closed_mail_template).to be_a(Mails::ClosedMail) }
it { expect(subject.refused_mail_template).to be_a(Mails::RefusedMail) }
it { expect(subject.without_continuation_mail_template).to be_a(Mails::WithoutContinuationMail) }
it { expect(subject.re_instructed_mail_template).to be_a(Mails::ReInstructedMail) }
it { expect(subject.passer_en_construction_email_template).to be_a(Mails::InitiatedMail) }
it { expect(subject.passer_en_instruction_email_template).to be_a(Mails::ReceivedMail) }
it { expect(subject.accepter_email_template).to be_a(Mails::ClosedMail) }
it { expect(subject.refuser_email_template).to be_a(Mails::RefusedMail) }
it { expect(subject.classer_sans_suite_email_template).to be_a(Mails::WithoutContinuationMail) }
it { expect(subject.repasser_en_instruction_email_template).to be_a(Mails::ReInstructedMail) }
end
describe 'compute_dossiers_count' do
@ -30,7 +30,7 @@ describe Procedure do
subject { procedure }
context 'when initiated_mail is not customize' do
it { expect(subject.initiated_mail_template.body).to eq(Mails::InitiatedMail.default_for_procedure(procedure).body) }
it { expect(subject.passer_en_construction_email_template.body).to eq(Mails::InitiatedMail.default_for_procedure(procedure).body) }
end
context 'when initiated_mail is customize' do
@ -39,7 +39,7 @@ describe Procedure do
subject.save
subject.reload
end
it { expect(subject.initiated_mail_template.body).to eq('sisi') }
it { expect(subject.passer_en_construction_email_template.body).to eq('sisi') }
end
context 'when initiated_mail is customize ... again' do
@ -48,7 +48,7 @@ describe Procedure do
subject.save
subject.reload
end
it { expect(subject.initiated_mail_template.body).to eq('toto') }
it { expect(subject.passer_en_construction_email_template.body).to eq('toto') }
it { expect(Mails::InitiatedMail.count).to eq(1) }
end
@ -58,7 +58,7 @@ describe Procedure do
let(:procedure) { create(:procedure, attestation_template: attestation_template) }
let(:attestation_template) { nil }
subject { procedure.closed_mail_template.rich_body.body.to_html }
subject { procedure.accepter_email_template.rich_body.body.to_html }
context 'for procedures without an attestation' do
it { is_expected.not_to include('lien attestation') }
@ -768,7 +768,7 @@ describe Procedure do
end
it 'should not duplicate default mail_template' do
expect(subject.initiated_mail_template.attributes).to eq Mails::InitiatedMail.default_for_procedure(subject).attributes
expect(subject.passer_en_construction_email_template.attributes).to eq Mails::InitiatedMail.default_for_procedure(subject).attributes
end
it 'should not duplicate specific related objects' do