Merge pull request #2006 from betagouv/clean-notification-mailer

Clean notification mailer
This commit is contained in:
gregoirenovel 2018-05-31 13:45:47 +02:00 committed by GitHub
commit 2d2490477e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 36 deletions

View file

@ -1,17 +1,18 @@
class NotificationMailer < ApplicationMailer
default to: Proc.new { @user.email }
def new_answer(dossier)
subject = "Nouveau message pour votre dossier demarches-simplifiees.fr nº #{dossier.id}"
def send_dossier_received(dossier_id)
dossier = Dossier.find(dossier_id)
send_notification(dossier, dossier.procedure.received_mail_template)
send_mail(dossier, subject)
end
def send_draft_notification(dossier)
vars_mailer(dossier)
subject = "Retrouvez votre brouillon pour la démarche : #{dossier.procedure.libelle}"
@subject = "Retrouvez votre brouillon pour la démarche : #{dossier.procedure.libelle}"
send_mail(dossier, subject)
end
mail(subject: @subject)
def send_dossier_received(dossier)
send_notification(dossier, dossier.procedure.received_mail_template)
end
def send_initiated_notification(dossier)
@ -30,39 +31,31 @@ class NotificationMailer < ApplicationMailer
send_notification(dossier, dossier.procedure.without_continuation_mail_template)
end
def new_answer(dossier)
send_mail dossier, "Nouveau message pour votre dossier demarches-simplifiees.fr nº #{dossier.id}"
end
private
def send_notification(dossier, mail_template)
vars_mailer(dossier)
def send_mail(dossier, subject)
@dossier = dossier
email = dossier.user.email
@subject = mail_template.subject_for_dossier dossier
@body = mail_template.body_for_dossier dossier
create_commentaire_for_notification
mail(subject: @subject) { |format| format.html { @body } }
mail(subject: subject, to: email)
end
def create_commentaire_for_notification
def send_notification(dossier, mail_template)
email = dossier.user.email
subject = mail_template.subject_for_dossier(dossier)
body = mail_template.body_for_dossier(dossier)
create_commentaire_for_notification(dossier, subject, body)
mail(subject: subject, to: email) { |format| format.html { body } }
end
def create_commentaire_for_notification(dossier, subject, body)
Commentaire.create(
dossier: @dossier,
dossier: dossier,
email: I18n.t("dynamics.contact_email"),
body: ["[#{@subject}]", @body].join("<br><br>")
body: ["[#{subject}]", body].join("<br><br>")
)
end
def vars_mailer(dossier)
@dossier = dossier
@user = dossier.user
end
def send_mail(dossier, subject)
vars_mailer dossier
mail(subject: subject)
end
end

View file

@ -323,7 +323,7 @@ class Dossier < ApplicationRecord
def send_dossier_received
if saved_change_to_state? && en_instruction?
NotificationMailer.send_dossier_received(id).deliver_later
NotificationMailer.send_dossier_received(self).deliver_later
end
end

View file

@ -38,7 +38,7 @@ RSpec.describe NotificationMailer, type: :mailer do
end
describe '.send_dossier_received' do
subject { described_class.send_dossier_received(dossier.id) }
subject { described_class.send_dossier_received(dossier) }
let(:email_template) { create(:received_mail) }
before do

View file

@ -603,7 +603,7 @@ describe Dossier do
it "sends an email when the dossier becomes en_instruction" do
dossier.en_instruction!
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier.id)
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier)
end
it "does not an email when the dossier becomes accepte" do