Merge pull request #823 from sgmap/draft-email-notification

[Fix #813] When a brouillon is created, send an email to user
This commit is contained in:
Mathieu Magnin 2017-10-20 14:26:32 +02:00 committed by GitHub
commit fa5387ca88
6 changed files with 63 additions and 1 deletions

View file

@ -21,6 +21,14 @@ class NotificationMailer < ApplicationMailer
mail(subject: @object) { |format| format.html { @body } }
end
def send_draft_notification(dossier)
vars_mailer(dossier)
@object = "Retrouvez votre brouillon pour la démarche : #{dossier.procedure.libelle}"
mail(subject: @object)
end
def new_answer(dossier)
send_mail dossier, "Nouveau message pour votre dossier TPS nº #{dossier.id}"
end

View file

@ -76,6 +76,7 @@ class Dossier < ActiveRecord::Base
after_save :build_default_champs, if: Proc.new { procedure_id_changed? }
after_save :build_default_individual, if: Proc.new { procedure.for_individual? }
after_save :send_dossier_received
after_create :send_draft_notification_email
validates :user, presence: true
@ -380,4 +381,10 @@ class Dossier < ActiveRecord::Base
NotificationMailer.send_dossier_received(id).deliver_later
end
end
def send_draft_notification_email
if brouillon?
NotificationMailer.send_draft_notification(self).deliver_now!
end
end
end

View file

@ -0,0 +1,19 @@
Bonjour,
%br
%br
Vous pouvez retrouver et compléter le brouillon que vous avez créé pour la démarche
%strong= @dossier.procedure.libelle
à l'adresse suivante :
= link_to users_dossiers_url(liste: 'brouillon'), users_dossiers_url(liste: 'brouillon'), target: '_blank'
%br
%br
Bonne journée,
%br
%br
L'équipe Téléprocédures Simplifiées
%br
%br
%br
%br
Merci de ne pas répondre à cet email. Postez directement vos questions dans votre dossier sur la plateforme.

View file

@ -2,4 +2,8 @@ class NotificationMailerPreview < ActionMailer::Preview
def send_notification
NotificationMailer.send_notification(Dossier.last, Dossier.last.procedure.initiated_mail_template)
end
def send_draft_notification
NotificationMailer.send_draft_notification(Dossier.last)
end
end

View file

@ -102,7 +102,7 @@ describe AttestationTemplate, type: :model do
let(:entreprise) { create(:entreprise, etablissement: etablissement) }
let(:types_de_champ) { [] }
let(:types_de_champ_private) { [] }
let(:dossier) { create(:dossier, procedure: procedure, individual: individual, entreprise: entreprise) }
let!(:dossier) { create(:dossier, procedure: procedure, individual: individual, entreprise: entreprise) }
let(:template_title) { 'title' }
let(:template_body) { 'body' }
let(:attestation_template) do

View file

@ -792,6 +792,30 @@ describe Dossier do
end
end
describe "#send_draft_notification_email" do
let(:procedure) { create(:procedure) }
let(:user) { create(:user) }
before do
ActionMailer::Base.deliveries.clear
end
it "send an email when the dossier is created for the very first time" do
expect { Dossier.create(procedure: procedure, state: "draft", user: user) }.to change(ActionMailer::Base.deliveries, :size).from(0).to(1)
mail = ActionMailer::Base.deliveries.last
expect(mail.subject).to eq("Retrouvez votre brouillon pour la démarche : #{procedure.libelle}")
end
it "does not send an email when the dossier is created with a non draft state" do
expect { Dossier.create(procedure: procedure, state: "initiated", user: user) }.not_to change(ActionMailer::Base.deliveries, :size)
expect { Dossier.create(procedure: procedure, state: "received", user: user) }.not_to change(ActionMailer::Base.deliveries, :size)
expect { Dossier.create(procedure: procedure, state: "closed", user: user) }.not_to change(ActionMailer::Base.deliveries, :size)
expect { Dossier.create(procedure: procedure, state: "refused", user: user) }.not_to change(ActionMailer::Base.deliveries, :size)
expect { Dossier.create(procedure: procedure, state: "without_continuation", user: user) }.not_to change(ActionMailer::Base.deliveries, :size)
end
end
describe '.build_attestation' do
let(:attestation_template) { nil }
let(:procedure) { create(:procedure, attestation_template: attestation_template) }