Merge pull request #9285 from mfo/US/fix-notifications-duplicated
correctif(notifications): ETQ usager, j'aimerais que les notifications soient fiable
This commit is contained in:
commit
543eee5a68
13 changed files with 75 additions and 55 deletions
|
@ -186,11 +186,9 @@ module Users
|
||||||
@dossier.passer_en_construction!
|
@dossier.passer_en_construction!
|
||||||
@dossier.process_declarative!
|
@dossier.process_declarative!
|
||||||
@dossier.process_sva_svr!
|
@dossier.process_sva_svr!
|
||||||
NotificationMailer.send_en_construction_notification(@dossier).deliver_later
|
|
||||||
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
|
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
|
||||||
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to merci_dossier_path(@dossier)
|
redirect_to merci_dossier_path(@dossier)
|
||||||
else
|
else
|
||||||
flash.now.alert = errors
|
flash.now.alert = errors
|
||||||
|
|
|
@ -6,12 +6,8 @@
|
||||||
# The subject and body of a Notification can be customized by each demarche.
|
# The subject and body of a Notification can be customized by each demarche.
|
||||||
#
|
#
|
||||||
class NotificationMailer < ApplicationMailer
|
class NotificationMailer < ApplicationMailer
|
||||||
include ActionView::Helpers::SanitizeHelper
|
|
||||||
include ActionView::Helpers::TextHelper
|
|
||||||
|
|
||||||
before_action :set_dossier
|
before_action :set_dossier
|
||||||
before_action :set_services_publics_plus, only: :send_notification
|
before_action :set_services_publics_plus, only: :send_notification
|
||||||
after_action :create_commentaire_for_notification
|
|
||||||
|
|
||||||
helper ServiceHelper
|
helper ServiceHelper
|
||||||
helper MailerHelper
|
helper MailerHelper
|
||||||
|
@ -22,11 +18,9 @@ class NotificationMailer < ApplicationMailer
|
||||||
def send_notification
|
def send_notification
|
||||||
@service = @dossier.procedure.service
|
@service = @dossier.procedure.service
|
||||||
@logo_url = attach_logo(@dossier.procedure)
|
@logo_url = attach_logo(@dossier.procedure)
|
||||||
@rendered_template = sanitize(@body, scrubber: Sanitizers::MailScrubber.new)
|
|
||||||
attachments[@attachment[:filename]] = @attachment[:content] if @attachment.present?
|
attachments[@attachment[:filename]] = @attachment[:content] if @attachment.present?
|
||||||
|
|
||||||
I18n.with_locale(@dossier.user_locale) do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,19 +63,15 @@ class NotificationMailer < ApplicationMailer
|
||||||
mail.perform_deliveries = false
|
mail.perform_deliveries = false
|
||||||
else
|
else
|
||||||
I18n.with_locale(@dossier.user_locale) do
|
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)
|
@email = @dossier.user_email_for(:notification)
|
||||||
@subject = truncate(mail_template.subject_for_dossier(@dossier), length: 100)
|
@rendered_template = mail_template_presenter.safe_body
|
||||||
@body = mail_template.body_for_dossier(@dossier)
|
@subject = mail_template_presenter.safe_subject
|
||||||
@actions = mail_template.actions_for_dossier(@dossier)
|
@actions = mail_template.actions_for_dossier(@dossier)
|
||||||
@attachment = mail_template.attachment_for_dossier(@dossier)
|
@attachment = mail_template.attachment_for_dossier(@dossier)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire_for_notification
|
|
||||||
body = ["[#{@subject}]", @body].join("<br><br>")
|
|
||||||
CommentaireService.create!(CONTACT_EMAIL, @dossier, body: body)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -886,12 +886,19 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mail_template_for_state
|
||||||
|
procedure.mail_template_for(state)
|
||||||
|
end
|
||||||
|
|
||||||
def after_passer_en_construction
|
def after_passer_en_construction
|
||||||
self.conservation_extension = 0.days
|
self.conservation_extension = 0.days
|
||||||
self.depose_at = self.en_construction_at = self.traitements
|
self.depose_at = self.en_construction_at = self.traitements
|
||||||
.passer_en_construction
|
.passer_en_construction
|
||||||
.processed_at
|
.processed_at
|
||||||
save!
|
save!
|
||||||
|
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
|
NotificationMailer.send_en_construction_notification(self).deliver_later
|
||||||
procedure.compute_dossiers_count
|
procedure.compute_dossiers_count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -910,6 +917,7 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
resolve_pending_correction!
|
resolve_pending_correction!
|
||||||
|
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
if !disable_notification
|
if !disable_notification
|
||||||
NotificationMailer.send_en_instruction_notification(self).deliver_later
|
NotificationMailer.send_en_instruction_notification(self).deliver_later
|
||||||
end
|
end
|
||||||
|
@ -926,7 +934,7 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
save!
|
save!
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
NotificationMailer.send_en_instruction_notification(self).deliver_later
|
NotificationMailer.send_en_instruction_notification(self).deliver_later
|
||||||
|
|
||||||
if procedure.sva_svr_enabled?
|
if procedure.sva_svr_enabled?
|
||||||
|
@ -969,6 +977,7 @@ class Dossier < ApplicationRecord
|
||||||
save!
|
save!
|
||||||
rebase_later
|
rebase_later
|
||||||
if !disable_notification
|
if !disable_notification
|
||||||
|
# pourquoi pas de commentaire automatique ici ?
|
||||||
DossierMailer.notify_revert_to_instruction(self).deliver_later
|
DossierMailer.notify_revert_to_instruction(self).deliver_later
|
||||||
end
|
end
|
||||||
log_dossier_operation(instructeur, :repasser_en_instruction)
|
log_dossier_operation(instructeur, :repasser_en_instruction)
|
||||||
|
@ -995,6 +1004,8 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
save!
|
save!
|
||||||
remove_titres_identite!
|
remove_titres_identite!
|
||||||
|
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
if !disable_notification
|
if !disable_notification
|
||||||
NotificationMailer.send_accepte_notification(self).deliver_later
|
NotificationMailer.send_accepte_notification(self).deliver_later
|
||||||
end
|
end
|
||||||
|
@ -1020,6 +1031,7 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
save!
|
save!
|
||||||
remove_titres_identite!
|
remove_titres_identite!
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
NotificationMailer.send_accepte_notification(self).deliver_later
|
NotificationMailer.send_accepte_notification(self).deliver_later
|
||||||
log_automatic_dossier_operation(:accepter, self)
|
log_automatic_dossier_operation(:accepter, self)
|
||||||
end
|
end
|
||||||
|
@ -1041,6 +1053,9 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
save!
|
save!
|
||||||
remove_titres_identite!
|
remove_titres_identite!
|
||||||
|
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
|
|
||||||
if !disable_notification
|
if !disable_notification
|
||||||
NotificationMailer.send_refuse_notification(self).deliver_later
|
NotificationMailer.send_refuse_notification(self).deliver_later
|
||||||
end
|
end
|
||||||
|
@ -1065,6 +1080,9 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
save!
|
save!
|
||||||
remove_titres_identite!
|
remove_titres_identite!
|
||||||
|
|
||||||
|
MailTemplatePresenterService.create_commentaire_for_state(self)
|
||||||
|
|
||||||
if !disable_notification
|
if !disable_notification
|
||||||
NotificationMailer.send_sans_suite_notification(self).deliver_later
|
NotificationMailer.send_sans_suite_notification(self).deliver_later
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# procedure_id :integer
|
# procedure_id :integer
|
||||||
#
|
#
|
||||||
module Mails
|
module Mails
|
||||||
|
# accepte
|
||||||
class ClosedMail < ApplicationRecord
|
class ClosedMail < ApplicationRecord
|
||||||
include MailTemplateConcern
|
include MailTemplateConcern
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# procedure_id :integer
|
# procedure_id :integer
|
||||||
#
|
#
|
||||||
module Mails
|
module Mails
|
||||||
|
# en_construction
|
||||||
class InitiatedMail < ApplicationRecord
|
class InitiatedMail < ApplicationRecord
|
||||||
include MailTemplateConcern
|
include MailTemplateConcern
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# procedure_id :integer
|
# procedure_id :integer
|
||||||
#
|
#
|
||||||
module Mails
|
module Mails
|
||||||
|
# en_instruction
|
||||||
class ReceivedMail < ApplicationRecord
|
class ReceivedMail < ApplicationRecord
|
||||||
include MailTemplateConcern
|
include MailTemplateConcern
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# procedure_id :integer
|
# procedure_id :integer
|
||||||
#
|
#
|
||||||
module Mails
|
module Mails
|
||||||
|
# refuse
|
||||||
class RefusedMail < ApplicationRecord
|
class RefusedMail < ApplicationRecord
|
||||||
include MailTemplateConcern
|
include MailTemplateConcern
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# procedure_id :integer
|
# procedure_id :integer
|
||||||
#
|
#
|
||||||
module Mails
|
module Mails
|
||||||
|
# classe_sans_suite
|
||||||
class WithoutContinuationMail < ApplicationRecord
|
class WithoutContinuationMail < ApplicationRecord
|
||||||
include MailTemplateConcern
|
include MailTemplateConcern
|
||||||
|
|
||||||
|
|
24
app/services/mail_template_presenter_service.rb
Normal file
24
app/services/mail_template_presenter_service.rb
Normal 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
|
|
@ -300,6 +300,10 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
|
|
||||||
subject
|
subject
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'creates a commentaire' do
|
||||||
|
expect { subject }.to change { Commentaire.count }.by(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'refusal with a justificatif' do
|
context 'refusal with a justificatif' do
|
||||||
|
|
|
@ -34,7 +34,7 @@ RSpec.describe NotificationMailer, type: :mailer do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'send_en_instruction_notification' do
|
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.') }
|
let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Your dossier was processed. Thanks.') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -43,15 +43,6 @@ RSpec.describe NotificationMailer, type: :mailer do
|
||||||
|
|
||||||
subject(:mail) { described_class.send_en_instruction_notification(dossier) }
|
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
|
it 'renders the template' do
|
||||||
expect(mail.subject).to eq('Email subject')
|
expect(mail.subject).to eq('Email subject')
|
||||||
expect(mail.body).to include('Your dossier was processed')
|
expect(mail.body).to include('Your dossier was processed')
|
||||||
|
@ -92,32 +83,9 @@ RSpec.describe NotificationMailer, type: :mailer do
|
||||||
end
|
end
|
||||||
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
|
describe 'subject length' do
|
||||||
let(:procedure) { create(:simple_procedure, libelle: "My super long title " + ("xo " * 100)) }
|
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.') }
|
let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -141,10 +109,10 @@ RSpec.describe NotificationMailer, type: :mailer do
|
||||||
describe 'subject with apostrophe' do
|
describe 'subject with apostrophe' do
|
||||||
let(:procedure) { create(:simple_procedure, libelle: "Mon titre avec l'apostrophe") }
|
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(: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
|
before do
|
||||||
dossier.procedure.closed_mail = email_template
|
dossier.procedure.received_mail = email_template
|
||||||
end
|
end
|
||||||
|
|
||||||
subject(:mail) { described_class.send_accepte_notification(dossier) }
|
subject(:mail) { described_class.send_accepte_notification(dossier) }
|
||||||
|
|
|
@ -265,7 +265,7 @@ describe DossierRebaseConcern do
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
let(:types_de_champ_private) { [{ type: :text, stable_id: 11 }] }
|
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(:types_de_champ) { procedure.active_revision.types_de_champ }
|
||||||
|
|
||||||
let(:text_type_de_champ) { types_de_champ.find { _1.stable_id == 1 } }
|
let(:text_type_de_champ) { types_de_champ.find { _1.stable_id == 1 } }
|
||||||
|
|
|
@ -1008,6 +1008,7 @@ describe Dossier, type: :model do
|
||||||
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
|
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(NotificationMailer).to have_received(:send_accepte_notification).with(dossier) }
|
||||||
it { expect(dossier.attestation).to eq(attestation) }
|
it { expect(dossier.attestation).to eq(attestation) }
|
||||||
|
it { expect(dossier.commentaires.count).to eq(1) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#accepter_automatiquement!' do
|
describe '#accepter_automatiquement!' do
|
||||||
|
@ -1061,6 +1062,7 @@ describe Dossier, type: :model do
|
||||||
expect(last_operation.automatic_operation?).to be_truthy
|
expect(last_operation.automatic_operation?).to be_truthy
|
||||||
expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier)
|
expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier)
|
||||||
expect(subject.attestation).to eq(attestation)
|
expect(subject.attestation).to eq(attestation)
|
||||||
|
expect(dossier.commentaires.count).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
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['operation']).to eq('passer_en_instruction') }
|
||||||
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
|
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(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
|
||||||
|
it { expect(dossier.commentaires.count).to eq(1) }
|
||||||
|
|
||||||
it "resolve pending correction" do
|
it "resolve pending correction" do
|
||||||
expect(dossier.pending_correction?).to be_falsey
|
expect(dossier.pending_correction?).to be_falsey
|
||||||
expect(correction.reload.resolved_at).to be_present
|
expect(correction.reload.resolved_at).to be_present
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe '#passer_automatiquement_en_instruction!' do
|
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['operation']).to eq('passer_en_instruction')
|
||||||
expect(operation_serialized['dossier_id']).to eq(dossier.id)
|
expect(operation_serialized['dossier_id']).to eq(dossier.id)
|
||||||
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
|
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
|
||||||
|
expect(dossier.commentaires.count).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue