diff --git a/app/controllers/admin/mails_controller.rb b/app/controllers/admin/mails_controller.rb
new file mode 100644
index 000000000..6101ac8c3
--- /dev/null
+++ b/app/controllers/admin/mails_controller.rb
@@ -0,0 +1,20 @@
+class Admin::MailsController < AdminController
+ before_action :retrieve_procedure
+
+ def index
+
+ end
+
+ def update
+ mail = current_administrateur.procedures.find(params[:procedure_id]).mail_templates.find(params[:id])
+ mail.update_attributes(update_params)
+
+ redirect_to admin_procedure_mails_path
+ end
+
+ private
+
+ def update_params
+ params.require(:mail_received).permit(:body, :object)
+ end
+end
\ No newline at end of file
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
new file mode 100644
index 000000000..232260865
--- /dev/null
+++ b/app/models/mail_received.rb
@@ -0,0 +1,18 @@
+class MailReceived < MailTemplate
+ before_save :default_values
+
+ def default_values
+ self.object ||= "[TPS] Accusé de réception pour votre dossier n°--numero_dossier--"
+ self.body ||= "Bonjour,
+
+
+ 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,
+
+
+ ---
+
+ L'équipe TPS"
+ end
+end
diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb
new file mode 100644
index 000000000..d51c8dbb8
--- /dev/null
+++ b/app/models/mail_template.rb
@@ -0,0 +1,33 @@
+class MailTemplate < ActiveRecord::Base
+ belongs_to :procedure
+
+ enum tags: {
+ numero_dossier: {
+ 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/models/procedure.rb b/app/models/procedure.rb
index 73f950cef..0bef80a6c 100644
--- a/app/models/procedure.rb
+++ b/app/models/procedure.rb
@@ -3,6 +3,8 @@ class Procedure < ActiveRecord::Base
has_many :types_de_champ, class_name: 'TypeDeChampPublic', dependent: :destroy
has_many :types_de_champ_private, dependent: :destroy
has_many :dossiers
+ has_many :mail_templates
+ has_one :mail_received
has_one :procedure_path, dependent: :destroy
@@ -15,7 +17,7 @@ class Procedure < ActiveRecord::Base
delegate :use_api_carto, to: :module_api_carto
- accepts_nested_attributes_for :types_de_champ,:reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
+ accepts_nested_attributes_for :types_de_champ, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :types_de_piece_justificative, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :module_api_carto
accepts_nested_attributes_for :types_de_champ_private
@@ -25,12 +27,18 @@ class Procedure < ActiveRecord::Base
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false
+ after_save :build_default_mails, if: Proc.new { id_changed? }
+
+ def build_default_mails
+ MailReceived.create(procedure: self)
+ end
+
def path
procedure_path.path unless procedure_path.nil?
end
def default_path
- libelle.downcase.gsub(/[^a-z0-9\-_]/,"_").gsub(/_*$/, '').gsub(/_+/, '_')
+ libelle.downcase.gsub(/[^a-z0-9\-_]/, "_").gsub(/_*$/, '').gsub(/_+/, '_')
end
def types_de_champ_ordered
@@ -79,20 +87,20 @@ class Procedure < ActiveRecord::Base
end
def clone
- procedure = self.deep_clone(include: [ :types_de_piece_justificative, :types_de_champ, :module_api_carto ])
+ procedure = self.deep_clone(include: [:types_de_piece_justificative, :types_de_champ, :module_api_carto])
procedure.archived = false
procedure.published = false
return procedure if procedure.save
end
def publish!(path)
- self.update_attributes!({ published: true, archived: false })
+ self.update_attributes!({published: true, archived: false})
ProcedurePath.create!(path: path, procedure: self, administrateur: self.administrateur)
end
def archive
self.procedure_path.destroy! if self.path
- self.update_attributes!({ archived: true })
+ self.update_attributes!({archived: true})
end
def total_dossier
diff --git a/app/views/admin/mails/index.html.haml b/app/views/admin/mails/index.html.haml
new file mode 100644
index 000000000..a910f76fe
--- /dev/null
+++ b/app/views/admin/mails/index.html.haml
@@ -0,0 +1,29 @@
+=render partial: 'admin/procedures/head', locals: {active: 'E-mails'}
+
+%h3
+ E-mail d'accusé de réception
+
+- unless @procedure.mail_received.blank?
+ = form_for @procedure.mail_received, url: {controller: 'admin/mails', action: 'update', id: @procedure.mail_received.id} do |f|
+ =f.text_field :object, {class:'form-control', style:'width: 40%'}
+ %br
+ =f.text_area :body, {class: 'form-control wysihtml5'}
+ %br
+ =f.submit 'Mettre à jour', {class:'btn btn-success', style:'float: right'}
+
+
+%table.table{style:'width: 50%'}
+ %tr
+ %th
+ Balise
+ %th
+ Description
+ - MailTemplate.tags.each do |balise|
+ %tr
+ %td.center
+ %b.text-success
+ \--
+ = balise.first
+ \--
+ %td
+ =balise.second[:description]
diff --git a/app/views/admin/procedures/_navbar.html.haml b/app/views/admin/procedures/_navbar.html.haml
index 6a382894a..f2d67d181 100644
--- a/app/views/admin/procedures/_navbar.html.haml
+++ b/app/views/admin/procedures/_navbar.html.haml
@@ -20,5 +20,8 @@
= link_to_unless(@procedure.locked?, 'Champs privés', admin_procedure_types_de_champ_private_path(@procedure)) do
= link_to('Champs privés', '#')
+%li{ class: ('active' if active == 'E-mails') }
+ = link_to('E-mails', admin_procedure_mails_path(@procedure))
+
%li{ class: ('active' if active == 'Prévisualisation'), style: 'float:right' }
= link_to('Prévisualisation', admin_procedure_previsualisation_path(@procedure), {style: 'font-style: italic;'})
\ No newline at end of file
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/config/routes.rb b/config/routes.rb
index 6445a1710..673bdbb14 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -114,6 +114,8 @@ Rails.application.routes.draw do
post '/:index/move_down' => 'pieces_justificatives#move_down', as: :move_down
end
+ resources 'mails'
+
put 'archive' => 'procedures#archive', as: :archive
put 'publish' => 'procedures#publish', as: :publish
post 'transfer' => 'procedures#transfer', as: :transfer
diff --git a/db/migrate/20160830142653_create_mail_templates_table.rb b/db/migrate/20160830142653_create_mail_templates_table.rb
new file mode 100644
index 000000000..8cc41d792
--- /dev/null
+++ b/db/migrate/20160830142653_create_mail_templates_table.rb
@@ -0,0 +1,11 @@
+class CreateMailTemplatesTable < ActiveRecord::Migration
+ def change
+ create_table :mail_templates do |t|
+ t.string :object
+ t.text :body
+ t.string :type
+ end
+
+ add_belongs_to :mail_templates, :procedure
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5cb43da5e..c377a367d 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160829114646) do
+ActiveRecord::Schema.define(version: 20160830142653) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -227,6 +227,13 @@ ActiveRecord::Schema.define(version: 20160829114646) do
t.integer "user_id"
end
+ create_table "mail_templates", force: :cascade do |t|
+ t.string "object"
+ t.text "body"
+ t.string "type"
+ t.integer "procedure_id"
+ end
+
create_table "module_api_cartos", force: :cascade do |t|
t.integer "procedure_id"
t.boolean "use_api_carto", default: false
diff --git a/spec/controllers/admin/mails_controller_spec.rb b/spec/controllers/admin/mails_controller_spec.rb
new file mode 100644
index 000000000..acb973e24
--- /dev/null
+++ b/spec/controllers/admin/mails_controller_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe Admin::MailsController, type: :controller do
+ let(:procedure) { create :procedure }
+
+ before do
+ sign_in procedure.administrateur
+ end
+
+ describe 'GET index' do
+ subject { get :index, procedure_id: procedure.id }
+
+ it { expect(subject.status).to eq 200 }
+ end
+
+ describe 'PATCH update' do
+ let(:object) { 'plop modif' }
+ let(:body) { 'plip modif' }
+
+ context 'when is mail_received id' do
+ subject { patch :update,
+ procedure_id: procedure.id,
+ id: procedure.mail_received.id,
+ mail_received: {
+ object: object,
+ body: body
+ } }
+
+ it { expect(subject).to redirect_to admin_procedure_mails_path }
+
+ describe 'values in database for mail received' do
+ before do
+ subject
+ procedure.reload
+ end
+
+ it { expect(procedure.mail_received.object).to eq object }
+ it { expect(procedure.mail_received.body).to eq body }
+ end
+ end
+ end
+end
\ 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/factories/mail_received.rb b/spec/factories/mail_received.rb
new file mode 100644
index 000000000..2e6ef674c
--- /dev/null
+++ b/spec/factories/mail_received.rb
@@ -0,0 +1,7 @@
+FactoryGirl.define do
+ factory :mail_received do
+ object "Mail d'accusé de bonne reception de votre dossier"
+ body "Votre dossier est correctement reçu"
+ type 'MailReceived'
+ 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
new file mode 100644
index 000000000..dbbe349ef
--- /dev/null
+++ b/spec/models/mail_template_spec.rb
@@ -0,0 +1,68 @@
+require 'spec_helper'
+
+describe MailTemplate do
+ it { is_expected.to have_db_column(:body) }
+ it { is_expected.to have_db_column(:type) }
+
+ it { is_expected.to belong_to(:procedure) }
+
+ describe '.tags' do
+ subject { MailTemplate.tags }
+
+ it { expect(subject.size).to eq 2 }
+
+ describe 'numero_dossier' do
+ subject { super()['numero_dossier'] }
+
+ describe 'attr and description value' do
+
+ 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
diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb
index 0cf700164..4c67b4b9f 100644
--- a/spec/models/procedure_spec.rb
+++ b/spec/models/procedure_spec.rb
@@ -5,6 +5,8 @@ describe Procedure do
it { is_expected.to have_many(:types_de_piece_justificative) }
it { is_expected.to have_many(:types_de_champ) }
it { is_expected.to have_many(:dossiers) }
+ 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 belong_to(:administrateur) }
end
@@ -20,6 +22,36 @@ describe Procedure do
it { is_expected.to have_db_column(:logo_secure_token) }
it { is_expected.to have_db_column(:cerfa_flag) }
it { is_expected.to have_db_column(:published) }
+
+ describe 'mail_received' do
+ let(:procedure) { create :procedure }
+
+ before do
+ create :mail_received, procedure: procedure
+ end
+
+ it { expect(procedure.mail_received).not_to be_nil }
+ end
+
+ end
+
+ describe '#build_default_mails' do
+ subject { build :procedure }
+
+ it 'call the fonction build_default_mails' do
+ expect(subject).to receive(:build_default_mails)
+ subject.save
+ end
+
+ describe 'accessible values' do
+
+ before do
+ subject.save
+ end
+
+ it { expect(subject.mail_templates.size).to eq 1 }
+ it { expect(subject.mail_received).not_to be_nil }
+ end
end
describe 'validation' do