New customizable mail : mail_validated, new balise --lien_dossier--

This commit is contained in:
Mathieu Magnin 2017-01-26 12:12:52 +01:00
parent 83616f77db
commit b40193e7b5
21 changed files with 134 additions and 58 deletions

View file

@ -19,6 +19,6 @@ class Admin::MailTemplatesController < AdminController
private private
def update_params def update_params
params.require(:mail_received).permit(:body, :object) params.require(:mail_template).permit(:body, :object)
end end
end end

View file

@ -5,6 +5,8 @@ class MailTemplateDecorator < Draper::Decorator
case object.type case object.type
when "MailReceived" when "MailReceived"
"E-mail d'accusé de réception" "E-mail d'accusé de réception"
when "MailValidated"
"E-mail de validation"
else else
object.type object.type
end end

View file

@ -4,11 +4,11 @@ class NotificationMailer < ApplicationMailer
end end
def dossier_received dossier def dossier_received dossier
send_mail dossier, MailTemplate.replace_tags(dossier.procedure.mail_received.object, dossier) send_mail dossier, dossier.procedure.mail_received.object_for_dossier(dossier)
end end
def dossier_validated dossier def dossier_validated dossier
send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été validé" send_mail dossier, dossier.procedure.mail_validated.object_for_dossier(dossier)
end end
def dossier_submitted dossier def dossier_submitted dossier

View file

@ -1,31 +1,45 @@
class MailTemplate < ActiveRecord::Base class MailTemplate < ActiveRecord::Base
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper
belongs_to :procedure belongs_to :procedure
enum tags: { TAGS = {
numero_dossier: { numero_dossier: {
description: "Permet d'afficher le numéro de dossier de l'utilisateur." description: "Permet d'afficher le numéro de dossier de l'utilisateur."
}, },
libelle_procedure: { lien_dossier: {
description: "Permet d'afficher le libellé de la procédure." description: "Permet d'afficher un lien vers le dossier de l'utilisateur."
} },
} libelle_procedure: {
description: "Permet d'afficher le libellé de la procédure."
}
}
def self.replace_tags string, dossier def object_for_dossier dossier
@dossier = dossier replace_tags(object, dossier)
end
tags.inject(string) do |acc, tag| def body_for_dossier dossier
acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym)) || acc replace_tags(body, dossier)
end
def replace_tags string, dossier
TAGS.inject(string) do |acc, tag|
acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym, dossier)) || acc
end end
end end
private private
def self.replace_tag tag def replace_tag tag, dossier
case tag case tag
when :numero_dossier when :numero_dossier
@dossier.id.to_s dossier.id.to_s
when :lien_dossier
link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank'
when :libelle_procedure when :libelle_procedure
@dossier.procedure.libelle dossier.procedure.libelle
else else
'--BALISE_NON_RECONNUE--' '--BALISE_NON_RECONNUE--'
end end

View file

@ -0,0 +1,18 @@
class MailValidated < MailTemplate
before_save :default_values
def default_values
self.object ||= "[TPS] Votre dossier TPS N°--numero_dossier-- a été validé"
self.body ||= "Bonjour,<br>
<br>
Votre dossier N°--numero_dossier-- est prêt à être déposé pour instruction.<br>
<br>
Afin de finaliser son dépôt, merci de vous rendre sur --lien_dossier--.,<br>
<br>
Bonne journée,<br>
---<br>
Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme.<br>
---<br>
L'équipe TPS"
end
end

View file

@ -4,7 +4,7 @@ class Procedure < ActiveRecord::Base
has_many :types_de_champ_private, dependent: :destroy has_many :types_de_champ_private, dependent: :destroy
has_many :dossiers has_many :dossiers
has_many :mail_templates has_many :mail_templates
has_one :mail_received
has_one :procedure_path, dependent: :destroy has_one :procedure_path, dependent: :destroy
@ -32,7 +32,8 @@ class Procedure < ActiveRecord::Base
after_save :build_default_mails, if: Proc.new { id_changed? } after_save :build_default_mails, if: Proc.new { id_changed? }
def build_default_mails def build_default_mails
MailReceived.create(procedure: self) mail_templates << MailReceived.create unless mail_received
mail_templates << MailValidated.create unless mail_validated
end end
def path def path
@ -109,4 +110,12 @@ class Procedure < ActiveRecord::Base
def total_dossier def total_dossier
self.dossiers.where.not(state: :draft).size self.dossiers.where.not(state: :draft).size
end end
def mail_validated
mail_templates.where(type: :MailValidated).first
end
def mail_received
mail_templates.where(type: :MailReceived).first
end
end end

View file

@ -2,7 +2,7 @@
%h3 %h3
= @mail_template.decorate.name = @mail_template.decorate.name
= simple_form_for @mail_template, url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f| = simple_form_for @mail_template.becomes(MailTemplate), url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f|
.row .row
.col-md-6 .col-md-6
= f.input :object, label: "Objet de l'email" = f.input :object, label: "Objet de l'email"
@ -19,7 +19,7 @@
Balise Balise
%th %th
Description Description
- MailTemplate.tags.each do |balise| - MailTemplate::TAGS.each do |balise|
%tr %tr
%td.center %td.center
%b.text-success %b.text-success

View file

@ -1 +1 @@
<%= MailTemplate.replace_tags(@dossier.procedure.mail_received.body, @dossier).html_safe %> <%= @dossier.procedure.mail_received.body_for_dossier(@dossier).html_safe %>

View file

@ -0,0 +1 @@
<%= @dossier.procedure.mail_validated.body_for_dossier(@dossier).html_safe %>

View file

@ -1,14 +0,0 @@
Bonjour <%= @user.email %>
Votre dossier N°<%=@dossier.id%> est prêt à être déposé pour instruction.
Afin de finaliser son dépôt, merci de vous rendre sur <%=users_dossier_recapitulatif_url(dossier_id: @dossier.id)%>
Bonne journée,
---
Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme.
---
---
L'équide TPS - tps@apientreprise.fr

View file

@ -42,6 +42,10 @@ Rails.application.configure do
# Action Mailer settings # Action Mailer settings
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Rails.application.routes.default_url_options = {
host: 'localhost:3000',
protocol: :http
}
# Config for mailcatcher https://mailcatcher.me/ # Config for mailcatcher https://mailcatcher.me/
config.action_mailer.smtp_settings = { config.action_mailer.smtp_settings = {

View file

@ -76,4 +76,8 @@ Rails.application.configure do
# Do not dump schema after migrations. # Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false config.active_record.dump_schema_after_migration = false
Rails.application.routes.default_url_options = {
protocol: :https
}
end end

View file

@ -39,6 +39,10 @@ Rails.application.configure do
config.action_mailer.delivery_method = :test config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { :host => 'localhost:3000' } config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Rails.application.routes.default_url_options = {
host: 'localhost:3000',
protocol: :http
}
# Raises error for missing translations # Raises error for missing translations
# config.action_view.raise_on_missing_translations = true # config.action_view.raise_on_missing_translations = true

View file

@ -1,5 +1,4 @@
Rails.application.routes.draw do Rails.application.routes.draw do
default_url_options protocol: :https
get "/ping" => "ping#index", :constraints => {:ip => /127.0.0.1/} get "/ping" => "ping#index", :constraints => {:ip => /127.0.0.1/}

View file

@ -0,0 +1,9 @@
class InitMailValidatedForAllProcedure < ActiveRecord::Migration[5.0]
def change
Procedure.all.each do |p|
unless p.mail_validated
p.mail_templates << MailValidated.create
end
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161227103823) do ActiveRecord::Schema.define(version: 20170125152856) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -361,6 +361,7 @@ ActiveRecord::Schema.define(version: 20161227103823) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "procedure_id" t.integer "procedure_id"
t.integer "order_place" t.integer "order_place"
t.string "lien_demarche"
end end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|

View file

@ -22,16 +22,16 @@ describe Admin::MailTemplatesController, type: :controller do
let(:object) { 'plop modif' } let(:object) { 'plop modif' }
let(:body) { 'plip modif' } let(:body) { 'plip modif' }
context 'when is mail_received id' do context 'when is mail_template id' do
subject { patch :update, subject { patch :update,
params: {procedure_id: mail_template.procedure.id, params: {procedure_id: mail_template.procedure.id,
id: mail_template.id, id: mail_template.id,
mail_received: { mail_template: {
object: object, object: object,
body: body body: body
}} } }} }
it { expect(subject).to redirect_to admin_procedure_mail_templates_path } it { expect(subject).to redirect_to admin_procedure_mail_templates_path(mail_template.procedure) }
it { it {
expect { expect {

View file

@ -7,7 +7,9 @@ describe MailTemplateDecorator do
context '#name' do context '#name' do
subject { decorator.name } subject { decorator.name }
it { is_expected.to eq decorator.type} context 'when mail_template is a MailValidated' do
it { is_expected.to eq "E-mail de validation" }
end
context 'when mail_template is a MailReceived' do context 'when mail_template is a MailReceived' do
let(:mail_template) {create :mail_template, :dossier_received} let(:mail_template) {create :mail_template, :dossier_received}

View file

@ -19,8 +19,8 @@ RSpec.describe NotificationMailer, type: :mailer do
subject(:subject) { described_class.dossier_validated(dossier) } subject(:subject) { described_class.dossier_validated(dossier) }
it { expect(subject.body).to match("Votre dossier N°#{dossier.id} est prêt à être déposé pour instruction.") } it { expect(subject.body).to match("Votre dossier N°#{dossier.id} est prêt à être déposé pour instruction.") }
it { expect(subject.body).to include("Afin de finaliser son dépôt, merci de vous rendre sur #{users_dossier_recapitulatif_url(dossier_id: dossier.id)}") } it { expect(subject.body).to include("Afin de finaliser son dépôt, merci de vous rendre sur", users_dossier_recapitulatif_url(dossier_id: dossier.id)) }
it { expect(subject.subject).to eq("Votre dossier TPS N°#{dossier.id} a été validé") } it { expect(subject.subject).to eq("[TPS] Votre dossier TPS N°#{dossier.id} a été validé") }
end end
describe ".dossier_submitted" do describe ".dossier_submitted" do

View file

@ -7,25 +7,34 @@ describe MailTemplate do
it { is_expected.to belong_to(:procedure) } it { is_expected.to belong_to(:procedure) }
describe '.tags' do describe '.tags' do
subject { MailTemplate.tags } subject { MailTemplate::TAGS }
it { expect(subject.size).to eq 2 } it { expect(subject.size).to eq 3 }
describe 'numero_dossier' do describe 'numero_dossier' do
subject { super()['numero_dossier'] } subject { super()[:numero_dossier] }
describe 'attr and description value' do describe 'attr and description value' do
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
end end
describe 'numero_dossier' do describe 'libelle_procedure' do
subject { super()['libelle_procedure'] } subject { super()[:libelle_procedure] }
describe 'attr and description value' do describe 'attr and description value' do
it { expect(subject['description']).to eq "Permet d'afficher le libellé de la procédure." } it { expect(subject[:description]).to eq "Permet d'afficher le libellé de la procédure." }
end
end
describe 'lien_dossier' do
subject { super()[:lien_dossier] }
describe 'attr and description value' do
it { expect(subject[:description]).to eq "Permet d'afficher un lien vers le dossier de l'utilisateur." }
end end
end end
end end
@ -40,7 +49,7 @@ describe MailTemplate do
procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier--') procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier--')
end end
subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } subject { procedure.mail_received.object_for_dossier dossier }
it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id}" } it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id}" }
end end
@ -50,17 +59,30 @@ describe MailTemplate do
procedure.mail_received.update_column(:object, '[TPS] Dossier pour la procédure --libelle_procedure--') procedure.mail_received.update_column(:object, '[TPS] Dossier pour la procédure --libelle_procedure--')
end end
subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } subject { procedure.mail_received.object_for_dossier dossier }
it { expect(subject).to eq "[TPS] Dossier pour la procédure #{procedure.libelle}" } it { expect(subject).to eq "[TPS] Dossier pour la procédure #{procedure.libelle}" }
end end
describe 'for tag --lien_dossier--' do
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper
before do
procedure.mail_received.update_column(:body, 'Consultez votre dossier ici --lien_dossier--')
end
subject { procedure.mail_received.body_for_dossier dossier }
it { is_expected.to eq "Consultez votre dossier ici #{link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank'}" }
end
describe 'multiple tags' do describe 'multiple tags' do
before 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--') 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 end
subject { MailTemplate.replace_tags procedure.mail_received.object, dossier } subject { procedure.mail_received.object_for_dossier dossier }
it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id} pour la procédure #{procedure.libelle} et encore le numéro : #{dossier.id}" } 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

View file

@ -6,7 +6,6 @@ describe Procedure do
it { is_expected.to have_many(:types_de_champ) } it { is_expected.to have_many(:types_de_champ) }
it { is_expected.to have_many(:dossiers) } it { is_expected.to have_many(:dossiers) }
it { is_expected.to have_many(:mail_templates) } it { is_expected.to have_many(:mail_templates) }
it { is_expected.to have_one(:mail_received) }
it { is_expected.to have_one(:module_api_carto) } it { is_expected.to have_one(:module_api_carto) }
it { is_expected.to belong_to(:administrateur) } it { is_expected.to belong_to(:administrateur) }
it { is_expected.to have_many(:preference_list_dossiers) } it { is_expected.to have_many(:preference_list_dossiers) }
@ -36,7 +35,7 @@ describe Procedure do
end end
describe '#build_default_mails' do describe '#build_default_mails' do
subject { build :procedure } subject { build :procedure, mail_templates: [] }
it 'call the fonction build_default_mails' do it 'call the fonction build_default_mails' do
expect(subject).to receive(:build_default_mails) expect(subject).to receive(:build_default_mails)
@ -49,8 +48,10 @@ describe Procedure do
subject.save subject.save
end end
it { expect(subject.mail_templates.size).to eq 1 } it { expect(subject.mail_templates.size).to eq 2 }
it { expect(subject.mail_received).not_to be_nil } it { expect(subject.mail_received).not_to be_nil }
it { expect(subject.mail_validated).not_to be_nil }
end end
end end