Merge branch 'develop' into staging

This commit is contained in:
Xavier J 2016-09-01 10:27:33 +02:00
commit 94fd350f83
18 changed files with 321 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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,
<br>
<br>
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.<br>
<br>
En vous souhaitant une bonne journée,
<br>
<br>
---
<br>
L'équipe TPS"
end
end

View file

@ -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

View file

@ -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
@ -25,6 +27,12 @@ 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

View file

@ -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]

View file

@ -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;'})

View file

@ -0,0 +1 @@
<%= MailTemplate.replace_tags @dossier.procedure.mail_received.body, @dossier %>

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -125,14 +125,27 @@ 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 }
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
describe 'POST #refuse' do
before do
dossier.refused!

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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