diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 76230a9d0..dde387679 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -54,6 +54,8 @@ class Backoffice::DossiersController < ApplicationController @facade.dossier.next_step! 'gestionnaire', 'receive' flash.notice = 'Dossier considéré comme reçu.' + NotificationMailer.dossier_received(@facade.dossier).deliver_now! + render 'show' end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 3b9ee8bd1..5f4303042 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -3,6 +3,10 @@ class NotificationMailer < ApplicationMailer send_mail dossier, "Nouveau commentaire pour votre dossier TPS N°#{dossier.id}" end + def dossier_received dossier + send_mail dossier, MailTemplate.replace_tags(dossier.procedure.mail_received.object, dossier) + end + def dossier_validated dossier send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été validé" end diff --git a/app/models/mail_received.rb b/app/models/mail_received.rb index b8611eea9..232260865 100644 --- a/app/models/mail_received.rb +++ b/app/models/mail_received.rb @@ -6,7 +6,7 @@ class MailReceived < MailTemplate self.body ||= "Bonjour,

- Votre administration vous confirme la bonne réception de votre dossier complet. Celui-ci sera instruit dans le délais légal déclaré par votre interlocuteur.
+ Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier-- complet. Celui-ci sera instruit dans le délais légal déclaré par votre interlocuteur.

En vous souhaitant une bonne journée,
diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb index 116570d83..d51c8dbb8 100644 --- a/app/models/mail_template.rb +++ b/app/models/mail_template.rb @@ -1,10 +1,33 @@ class MailTemplate < ActiveRecord::Base belongs_to :procedure - enum balises: { + enum tags: { numero_dossier: { - attr: 'dossier.id', description: "Permet d'afficher le numéro de dossier de l'utilisateur." + }, + libelle_procedure: { + description: "Permet d'afficher le libellé de la procédure." } } + + def self.replace_tags string, dossier + @dossier = dossier + + tags.inject(string) do |acc, tag| + acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym)) || acc + end + end + + private + + def self.replace_tag tag + case tag + when :numero_dossier + @dossier.id.to_s + when :libelle_procedure + @dossier.procedure.libelle + else + '--BALISE_NON_RECONNUE--' + end + end end diff --git a/app/views/admin/mails/index.html.haml b/app/views/admin/mails/index.html.haml index 26b258e61..a910f76fe 100644 --- a/app/views/admin/mails/index.html.haml +++ b/app/views/admin/mails/index.html.haml @@ -18,7 +18,7 @@ Balise %th Description - - MailTemplate.balises.each do |balise| + - MailTemplate.tags.each do |balise| %tr %td.center %b.text-success diff --git a/app/views/notification_mailer/dossier_received.text.erb b/app/views/notification_mailer/dossier_received.text.erb new file mode 100644 index 000000000..42db34458 --- /dev/null +++ b/app/views/notification_mailer/dossier_received.text.erb @@ -0,0 +1 @@ +<%= MailTemplate.replace_tags @dossier.procedure.mail_received.body, @dossier %> \ No newline at end of file diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index 13c22edff..7769cc888 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' describe Backoffice::DossiersController, type: :controller do before do - @request.env['HTTP_REFERER'] = TPS::Application::URL + @request.env['HTTP_REFERER'] = TPS::Application::URL end let(:dossier) { create(:dossier, :with_entreprise) } @@ -125,11 +125,24 @@ describe Backoffice::DossiersController, type: :controller do sign_in gestionnaire end - it 'change state to received' do - post :receive, dossier_id: dossier_id + subject { post :receive, dossier_id: dossier_id } - dossier.reload - expect(dossier.state).to eq('received') + context 'when it post a receive instruction' do + before do + subject + dossier.reload + end + + it 'change state to received' do + expect(dossier.state).to eq('received') + end + end + + it 'Notification email is send' do + expect(NotificationMailer).to receive(:dossier_received).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_now!) + + subject end end diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 44f5d68ea..ff411562d 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -34,4 +34,14 @@ RSpec.describe NotificationMailer, type: :mailer do it { expect(subject.body).to match("ce jour à #{dossier.updated_at}.") } it { expect(subject.subject).to eq("Votre dossier TPS N°#{dossier.id} a été déposé") } end + + describe '.dossier_received' do + let(:user) { create(:user) } + let(:dossier) { create(:dossier, user: user) } + + subject(:subject) { described_class.dossier_received(dossier) } + + it { expect(subject.subject).to eq("[TPS] Accusé de réception pour votre dossier n°#{dossier.id}") } + it { expect(subject.body).to match("Votre administration vous confirme la bonne réception de votre dossier n°#{dossier.id}") } + end end diff --git a/spec/models/mail_template_spec.rb b/spec/models/mail_template_spec.rb index 5a6d937eb..dbbe349ef 100644 --- a/spec/models/mail_template_spec.rb +++ b/spec/models/mail_template_spec.rb @@ -6,22 +6,63 @@ describe MailTemplate do it { is_expected.to belong_to(:procedure) } - describe '.balises' do - subject { MailTemplate.balises } + describe '.tags' do + subject { MailTemplate.tags } - it { expect(subject.size).to eq 1 } + it { expect(subject.size).to eq 2 } describe 'numero_dossier' do - subject { super().first } - - it { expect(subject.first).to eq 'numero_dossier' } + subject { super()['numero_dossier'] } describe 'attr and description value' do - subject { super().second } - it { expect(subject[:attr]).to eq 'dossier.id' } - it { expect(subject[:description]).to eq "Permet d'afficher le numéro de dossier de l'utilisateur." } + it { expect(subject['description']).to eq "Permet d'afficher le numéro de dossier de l'utilisateur." } + end + end + + describe 'numero_dossier' do + subject { super()['libelle_procedure'] } + + describe 'attr and description value' do + + it { expect(subject['description']).to eq "Permet d'afficher le libellé de la procédure." } end end end + + describe '.replace_tags' do + let(:dossier) { create :dossier } + let(:procedure) { dossier.procedure } + let(:mail_received) { procedure.mail_received } + + describe 'for tag --numero_dossier--' do + before do + procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier--') + end + + subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } + + it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id}" } + end + + describe 'for tag --libelle_procedure--' do + before do + procedure.mail_received.update_column(:object, '[TPS] Dossier pour la procédure --libelle_procedure--') + end + + subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } + + it { expect(subject).to eq "[TPS] Dossier pour la procédure #{procedure.libelle}" } + end + + describe 'multiple tags' do + before do + procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier-- pour la procédure --libelle_procedure-- et encore le numéro : --numero_dossier--') + end + + subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } + + it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id} pour la procédure #{procedure.libelle} et encore le numéro : #{dossier.id}" } + end + end end