amelioration(NotificationMailer): sort la creation des commentaire sur la messagerie du mailer [trop de prob possible sinon: au retry on dupliquait les commenaitres, si le mail foirait, on commentait pas etc...]

This commit is contained in:
Martin 2023-07-07 14:06:10 +02:00
parent 23fb7e812e
commit ce9dbed663
13 changed files with 75 additions and 55 deletions

View file

@ -186,11 +186,9 @@ module Users
@dossier.passer_en_construction!
@dossier.process_declarative!
@dossier.process_sva_svr!
NotificationMailer.send_en_construction_notification(@dossier).deliver_later
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
end
redirect_to merci_dossier_path(@dossier)
else
flash.now.alert = errors

View file

@ -6,12 +6,8 @@
# The subject and body of a Notification can be customized by each demarche.
#
class NotificationMailer < ApplicationMailer
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
before_action :set_dossier
before_action :set_services_publics_plus, only: :send_notification
after_action :create_commentaire_for_notification
helper ServiceHelper
helper MailerHelper
@ -22,11 +18,9 @@ class NotificationMailer < ApplicationMailer
def send_notification
@service = @dossier.procedure.service
@logo_url = attach_logo(@dossier.procedure)
@rendered_template = sanitize(@body, scrubber: Sanitizers::MailScrubber.new)
attachments[@attachment[:filename]] = @attachment[:content] if @attachment.present?
I18n.with_locale(@dossier.user_locale) do
mail(subject: Nokogiri::HTML.parse(@subject).text, to: @email, template_name: 'send_notification')
mail(subject: @subject, to: @email, template_name: 'send_notification')
end
end
@ -69,19 +63,15 @@ class NotificationMailer < ApplicationMailer
mail.perform_deliveries = false
else
I18n.with_locale(@dossier.user_locale) do
mail_template = @dossier.procedure.mail_template_for(params[:state])
mail_template = @dossier.mail_template_for_state
mail_template_presenter = MailTemplatePresenterService.new(@dossier)
@email = @dossier.user_email_for(:notification)
@subject = truncate(mail_template.subject_for_dossier(@dossier), length: 100)
@body = mail_template.body_for_dossier(@dossier)
@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)
end
end
end
def create_commentaire_for_notification
body = ["<p>[#{@subject}]</p>", @body].join('')
CommentaireService.create!(CONTACT_EMAIL, @dossier, body: body)
end
end

View file

@ -886,12 +886,19 @@ class Dossier < ApplicationRecord
end
end
def mail_template_for_state
procedure.mail_template_for(state)
end
def after_passer_en_construction
self.conservation_extension = 0.days
self.depose_at = self.en_construction_at = self.traitements
.passer_en_construction
.processed_at
save!
MailTemplatePresenterService.create_commentaire_for_state(self)
NotificationMailer.send_en_construction_notification(self).deliver_later
procedure.compute_dossiers_count
end
@ -910,6 +917,7 @@ class Dossier < ApplicationRecord
resolve_pending_correction!
MailTemplatePresenterService.create_commentaire_for_state(self)
if !disable_notification
NotificationMailer.send_en_instruction_notification(self).deliver_later
end
@ -926,7 +934,7 @@ class Dossier < ApplicationRecord
end
save!
MailTemplatePresenterService.create_commentaire_for_state(self)
NotificationMailer.send_en_instruction_notification(self).deliver_later
if procedure.sva_svr_enabled?
@ -969,6 +977,7 @@ class Dossier < ApplicationRecord
save!
rebase_later
if !disable_notification
# pourquoi pas de commentaire automatique ici ?
DossierMailer.notify_revert_to_instruction(self).deliver_later
end
log_dossier_operation(instructeur, :repasser_en_instruction)
@ -995,6 +1004,8 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
if !disable_notification
NotificationMailer.send_accepte_notification(self).deliver_later
end
@ -1020,6 +1031,7 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
NotificationMailer.send_accepte_notification(self).deliver_later
log_automatic_dossier_operation(:accepter, self)
end
@ -1041,6 +1053,9 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
if !disable_notification
NotificationMailer.send_refuse_notification(self).deliver_later
end
@ -1065,6 +1080,9 @@ class Dossier < ApplicationRecord
save!
remove_titres_identite!
MailTemplatePresenterService.create_commentaire_for_state(self)
if !disable_notification
NotificationMailer.send_sans_suite_notification(self).deliver_later
end

View file

@ -10,6 +10,7 @@
# procedure_id :integer
#
module Mails
# accepte
class ClosedMail < ApplicationRecord
include MailTemplateConcern

View file

@ -10,6 +10,7 @@
# procedure_id :integer
#
module Mails
# en_construction
class InitiatedMail < ApplicationRecord
include MailTemplateConcern

View file

@ -10,6 +10,7 @@
# procedure_id :integer
#
module Mails
# en_instruction
class ReceivedMail < ApplicationRecord
include MailTemplateConcern

View file

@ -10,6 +10,7 @@
# procedure_id :integer
#
module Mails
# refuse
class RefusedMail < ApplicationRecord
include MailTemplateConcern

View file

@ -10,6 +10,7 @@
# procedure_id :integer
#
module Mails
# classe_sans_suite
class WithoutContinuationMail < ApplicationRecord
include MailTemplateConcern

View file

@ -0,0 +1,24 @@
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)
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)
end
def safe_subject
Nokogiri::HTML.parse(truncate(mail_template_for_state.subject_for_dossier(@dossier), length: 100)).text
end
def initialize(dossier)
@dossier = dossier
end
end

View file

@ -300,6 +300,10 @@ describe Instructeurs::DossiersController, type: :controller do
subject
end
it 'creates a commentaire' do
expect { subject }.to change { Commentaire.count }.by(1)
end
end
context 'refusal with a justificatif' do

View file

@ -34,7 +34,7 @@ RSpec.describe NotificationMailer, type: :mailer do
end
describe 'send_en_instruction_notification' do
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Your dossier was processed. Thanks.') }
before do
@ -43,15 +43,6 @@ RSpec.describe NotificationMailer, type: :mailer do
subject(:mail) { described_class.send_en_instruction_notification(dossier) }
it 'creates a commentaire in the messagerie' do
expect { subject.deliver_now }.to change { Commentaire.count }.by(1)
expect(subject.perform_deliveries).to be_truthy
commentaire = Commentaire.last
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))
expect(commentaire.dossier).to eq(dossier)
end
it 'renders the template' do
expect(mail.subject).to eq('Email subject')
expect(mail.body).to include('Your dossier was processed')
@ -92,32 +83,9 @@ RSpec.describe NotificationMailer, type: :mailer do
end
end
describe 'send_accepte_notification' do
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:closed_mail, subject: 'Email subject', body: 'Your dossier was accepted. Thanks.') }
before do
dossier.procedure.closed_mail = email_template
end
subject(:mail) { described_class.send_accepte_notification(dossier) }
context 'when dossier user is deleted' do
before do
dossier.user.delete_and_keep_track_dossiers_also_delete_user(administrateur)
dossier.reload
end
it 'should not send notification' do
expect { subject.deliver_now }.not_to change { Commentaire.count }
expect(subject.perform_deliveries).to be_falsey
end
end
end
describe 'subject length' do
let(:procedure) { create(:simple_procedure, libelle: "My super long title " + ("xo " * 100)) }
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:dossier) { create(:dossier, :accepte, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
before do
@ -141,10 +109,10 @@ RSpec.describe NotificationMailer, type: :mailer do
describe 'subject with apostrophe' do
let(:procedure) { create(:simple_procedure, libelle: "Mon titre avec l'apostrophe") }
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
let(:email_template) { create(:received_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
before do
dossier.procedure.closed_mail = email_template
dossier.procedure.received_mail = email_template
end
subject(:mail) { described_class.send_accepte_notification(dossier) }

View file

@ -265,7 +265,7 @@ describe DossierRebaseConcern do
]
end
let(:types_de_champ_private) { [{ type: :text, stable_id: 11 }] }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier) { create(:dossier, :with_entreprise, procedure: procedure) }
let(:types_de_champ) { procedure.active_revision.types_de_champ }
let(:text_type_de_champ) { types_de_champ.find { _1.stable_id == 1 } }

View file

@ -1008,6 +1008,7 @@ describe Dossier, type: :model do
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
it { expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
it { expect(dossier.commentaires.count).to eq(1) }
end
describe '#accepter_automatiquement!' do
@ -1061,6 +1062,7 @@ describe Dossier, type: :model do
expect(last_operation.automatic_operation?).to be_truthy
expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier)
expect(subject.attestation).to eq(attestation)
expect(dossier.commentaires.count).to eq(1)
end
end
end
@ -1082,11 +1084,21 @@ describe Dossier, type: :model do
it { expect(operation_serialized['operation']).to eq('passer_en_instruction') }
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
it { expect(dossier.commentaires.count).to eq(1) }
it "resolve pending correction" do
expect(dossier.pending_correction?).to be_falsey
expect(correction.reload.resolved_at).to be_present
end
it 'creates a commentaire in the messagerie with expected wording' do
email_template = dossier.procedure.mail_template_for(Dossier.states.fetch(:en_instruction))
commentaire = dossier.commentaires.first
expect(dossier.commentaires.count).to eq(1)
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))
expect(commentaire.dossier).to eq(dossier)
end
end
describe '#passer_automatiquement_en_instruction!' do
@ -1111,6 +1123,7 @@ describe Dossier, type: :model do
expect(operation_serialized['operation']).to eq('passer_en_instruction')
expect(operation_serialized['dossier_id']).to eq(dossier.id)
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
expect(dossier.commentaires.count).to eq(1)
end
end