From 23fb7e812ea173a50dfef9845fead98a7a270bfa Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 7 Jul 2023 09:53:31 +0200 Subject: [PATCH 1/2] =?UTF-8?q?amelioration(a11y):=20utilise=20une=20balis?= =?UTF-8?q?e=20

=20pour=20passer=20a=20la=20ligne=20avec=20un=20espace?= =?UTF-8?q?=20suppl=C3=A9mentaire=20plut=C3=B4t=20que=20deux=20
?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mailers/notification_mailer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index c4bfdf593..a2e042f64 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -81,7 +81,7 @@ class NotificationMailer < ApplicationMailer end def create_commentaire_for_notification - body = ["[#{@subject}]", @body].join("

") + body = ["

[#{@subject}]

", @body].join('') CommentaireService.create!(CONTACT_EMAIL, @dossier, body: body) end end From ce9dbed6637612febd01af443893f4eae58f3d2b Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 7 Jul 2023 14:06:10 +0200 Subject: [PATCH 2/2] 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...] --- app/controllers/users/dossiers_controller.rb | 2 - app/mailers/notification_mailer.rb | 20 +++------- app/models/dossier.rb | 20 +++++++++- app/models/mails/closed_mail.rb | 1 + app/models/mails/initiated_mail.rb | 1 + app/models/mails/received_mail.rb | 1 + app/models/mails/refused_mail.rb | 1 + app/models/mails/without_continuation_mail.rb | 1 + .../mail_template_presenter_service.rb | 24 +++++++++++ .../instructeurs/dossiers_controller_spec.rb | 4 ++ spec/mailers/notification_mailer_spec.rb | 40 ++----------------- .../concern/dossier_rebase_concern_spec.rb | 2 +- spec/models/dossier_spec.rb | 13 ++++++ 13 files changed, 75 insertions(+), 55 deletions(-) create mode 100644 app/services/mail_template_presenter_service.rb diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 6cba4c35a..8f9fed8d8 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -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 diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index a2e042f64..d969295b8 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -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 = ["

[#{@subject}]

", @body].join('') - CommentaireService.create!(CONTACT_EMAIL, @dossier, body: body) - end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d5ca7bbcd..1ea1efb3e 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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 diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb index 60660e23a..9e6ba017d 100644 --- a/app/models/mails/closed_mail.rb +++ b/app/models/mails/closed_mail.rb @@ -10,6 +10,7 @@ # procedure_id :integer # module Mails + # accepte class ClosedMail < ApplicationRecord include MailTemplateConcern diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb index 0178971ff..9611ef125 100644 --- a/app/models/mails/initiated_mail.rb +++ b/app/models/mails/initiated_mail.rb @@ -10,6 +10,7 @@ # procedure_id :integer # module Mails + # en_construction class InitiatedMail < ApplicationRecord include MailTemplateConcern diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb index ccfc6ecc1..69242d60d 100644 --- a/app/models/mails/received_mail.rb +++ b/app/models/mails/received_mail.rb @@ -10,6 +10,7 @@ # procedure_id :integer # module Mails + # en_instruction class ReceivedMail < ApplicationRecord include MailTemplateConcern diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index 50dfea0ea..eff138c4f 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -10,6 +10,7 @@ # procedure_id :integer # module Mails + # refuse class RefusedMail < ApplicationRecord include MailTemplateConcern diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb index 28558f330..bc9c71843 100644 --- a/app/models/mails/without_continuation_mail.rb +++ b/app/models/mails/without_continuation_mail.rb @@ -10,6 +10,7 @@ # procedure_id :integer # module Mails + # classe_sans_suite class WithoutContinuationMail < ApplicationRecord include MailTemplateConcern diff --git a/app/services/mail_template_presenter_service.rb b/app/services/mail_template_presenter_service.rb new file mode 100644 index 000000000..6745d4fe3 --- /dev/null +++ b/app/services/mail_template_presenter_service.rb @@ -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 = ["

[#{service.safe_subject}]

", 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 diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index f083cb29b..a18018031 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -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 diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index ac14a9063..635dc5c6a 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -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) } diff --git a/spec/models/concern/dossier_rebase_concern_spec.rb b/spec/models/concern/dossier_rebase_concern_spec.rb index 5d5b7e9ff..d43a9c339 100644 --- a/spec/models/concern/dossier_rebase_concern_spec.rb +++ b/spec/models/concern/dossier_rebase_concern_spec.rb @@ -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 } } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 0b3f46105..8f2641507 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -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