Merge pull request #1245 from betagouv/fix_1203-restrict_field_by_mail_type

Fix 1203 restrict field by mail type
This commit is contained in:
Frederic Merizen 2018-01-18 18:30:55 +01:00 committed by GitHub
commit 6a95cc6498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 242 additions and 107 deletions

View file

@ -11,6 +11,7 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE_IN_MB = 0.5 FILE_MAX_SIZE_IN_MB = 0.5
DOSSIER_STATE = 'accepte'
def attestation_for(dossier) def attestation_for(dossier)
Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier)) Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier))

View file

@ -1,8 +1,6 @@
module MailTemplateConcern module MailTemplateConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
def subject_for_dossier(dossier) def subject_for_dossier(dossier)
@ -13,10 +11,6 @@ module MailTemplateConcern
replace_tags(body, dossier) replace_tags(body, dossier)
end end
def tags(is_dossier_termine: self.class.const_get(:IS_DOSSIER_TERMINE))
super
end
module ClassMethods module ClassMethods
def default_for_procedure(procedure) def default_for_procedure(procedure)
body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME)) body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME))
@ -24,14 +18,7 @@ module MailTemplateConcern
end end
end end
private
def dossier_tags def dossier_tags
super + [{ libelle: 'lien dossier', description: '', lambda: -> (d) { users_dossier_recapitulatif_link(d) } }] TagsSubstitutionConcern::DOSSIER_TAGS + TagsSubstitutionConcern::DOSSIER_TAGS_FOR_MAIL
end
def users_dossier_recapitulatif_link(dossier)
url = users_dossier_recapitulatif_url(dossier)
link_to(url, url, target: '_blank')
end end
end end

View file

@ -1,61 +1,126 @@
module TagsSubstitutionConcern module TagsSubstitutionConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
def tags(is_dossier_termine: true) include Rails.application.routes.url_helpers
if procedure.for_individual? include ActionView::Helpers::UrlHelper
identity_tags = individual_tags
else
identity_tags = entreprise_tags + etablissement_tags
end
tags = identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags DOSSIER_TAGS = [
filter_tags(tags, is_dossier_termine)
end
private
def filter_tags(tags, is_dossier_termine)
if !is_dossier_termine
tags.reject { |tag| tag[:dossier_termine_only] }
else
tags
end
end
def procedure_type_de_champ_public_private_tags
(procedure.types_de_champ + procedure.types_de_champ_private)
.map { |tdc| { libelle: tdc.libelle, description: tdc.description } }
end
def dossier_tags
[
{ {
libelle: 'motivation', libelle: 'motivation',
description: 'Motivation facultative associée à la décision finale dacceptation, refus ou classement sans suite', description: 'Motivation facultative associée à la décision finale dacceptation, refus ou classement sans suite',
target: :motivation, target: :motivation,
dossier_termine_only: true available_for_states: Dossier::TERMINE
}, },
{ {
libelle: 'date de dépôt', libelle: 'date de dépôt',
description: 'Date du passage en construction du dossier par lusager', description: 'Date du passage en construction du dossier par lusager',
lambda: -> (d) { format_date(d.en_construction_at) } lambda: -> (d) { format_date(d.en_construction_at) },
available_for_states: Dossier::SOUMIS
}, },
{ {
libelle: 'date de passage en instruction', libelle: 'date de passage en instruction',
description: '', description: '',
lambda: -> (d) { format_date(d.en_instruction_at) } lambda: -> (d) { format_date(d.en_instruction_at) },
available_for_states: Dossier::INSTRUCTION_COMMENCEE
}, },
{ {
libelle: 'date de décision', libelle: 'date de décision',
description: 'Date de la décision dacceptation, refus, ou classement sans suite', description: 'Date de la décision dacceptation, refus, ou classement sans suite',
lambda: -> (d) { format_date(d.processed_at) }, lambda: -> (d) { format_date(d.processed_at) },
dossier_termine_only: true available_for_states: Dossier::TERMINE
}, },
{ libelle: 'libellé procédure', description: '', lambda: -> (d) { d.procedure.libelle } }, {
{ libelle: 'numéro du dossier', description: '', target: :id } libelle: 'libellé procédure',
description: '',
lambda: -> (d) { d.procedure.libelle },
available_for_states: Dossier::SOUMIS
},
{
libelle: 'numéro du dossier',
description: '',
target: :id,
available_for_states: Dossier::SOUMIS
}
] ]
DOSSIER_TAGS_FOR_MAIL = [
{
libelle: 'lien dossier',
description: '',
lambda: -> (d) { users_dossier_recapitulatif_link(d) },
available_for_states: Dossier::SOUMIS
}
]
INDIVIDUAL_TAGS = [
{
libelle: 'civilité',
description: 'M., Mme',
target: :gender,
available_for_states: Dossier::SOUMIS
},
{
libelle: 'nom',
description: "nom de l'usager",
target: :nom,
available_for_states: Dossier::SOUMIS
},
{
libelle: 'prénom',
description: "prénom de l'usager",
target: :prenom,
available_for_states: Dossier::SOUMIS
}
]
ENTREPRISE_TAGS = [
{
libelle: 'SIREN',
description: '',
target: :siren,
available_for_states: Dossier::SOUMIS
},
{
libelle: 'numéro de TVA intracommunautaire',
description: '',
target: :numero_tva_intracommunautaire,
available_for_states: Dossier::SOUMIS
},
{
libelle: 'SIRET du siège social',
description: '',
target: :siret_siege_social,
available_for_states: Dossier::SOUMIS
},
{
libelle: 'raison sociale',
description: '',
target: :raison_sociale,
available_for_states: Dossier::SOUMIS
}
]
ETABLISSEMENT_TAGS = [
{
libelle: 'adresse',
description: '',
target: :inline_adresse,
available_for_states: Dossier::SOUMIS
}
]
def tags
if procedure.for_individual?
identity_tags = INDIVIDUAL_TAGS
else
identity_tags = ENTREPRISE_TAGS + ETABLISSEMENT_TAGS
end end
filter_tags(identity_tags + dossier_tags + champ_public_tags + champ_private_tags)
end
private
def format_date(date) def format_date(date)
if date.present? if date.present?
date.localtime.strftime('%d/%m/%Y') date.localtime.strftime('%d/%m/%Y')
@ -64,25 +129,49 @@ module TagsSubstitutionConcern
end end
end end
def individual_tags def users_dossier_recapitulatif_link(dossier)
[ url = users_dossier_recapitulatif_url(dossier)
{ libelle: 'civilité', description: 'M., Mme', target: :gender }, link_to(url, url, target: '_blank')
{ libelle: 'nom', description: "nom de l'usager", target: :nom },
{ libelle: 'prénom', description: "prénom de l'usager", target: :prenom }
]
end end
def entreprise_tags def dossier_tags
[ # Overridden by MailTemplateConcern
{ libelle: 'SIREN', description: '', target: :siren }, DOSSIER_TAGS
{ libelle: 'numéro de TVA intracommunautaire', description: '', target: :numero_tva_intracommunautaire },
{ libelle: 'SIRET du siège social', description: '', target: :siret_siege_social },
{ libelle: 'raison sociale', description: '', target: :raison_sociale }
]
end end
def etablissement_tags def filter_tags(tags)
[{ libelle: 'adresse', description: '', target: :inline_adresse }] # Implementation note: emails and attestation generations are generally
# triggerred by changes to the dossiers state. The email or attestation
# is generated right after the dossier has reached its new state.
#
# DOSSIER_STATE should be equal to this new state.
#
# For instance, for an email that gets generated for the brouillon->en_construction
# transition, DOSSIER_STATE should equal 'en_construction'.
if !defined?(self.class::DOSSIER_STATE)
raise NameError.new("The class #{self.class.name} includes TagsSubstitutionConcern, it should define the DOSSIER_STATE constant but it does not", :DOSSIER_STATE)
end
tags.select { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) }
end
def champ_public_tags
types_de_champ_tags(procedure.types_de_champ, Dossier::SOUMIS)
end
def champ_private_tags
types_de_champ_tags(procedure.types_de_champ_private, Dossier::INSTRUCTION_COMMENCEE)
end
def types_de_champ_tags(types_de_champ, available_for_states)
types_de_champ.map { |tdc|
{
libelle: tdc.libelle,
description: tdc.description,
available_for_states: available_for_states
}
}
end end
def replace_tags(text, dossier) def replace_tags(text, dossier)
@ -90,18 +179,18 @@ module TagsSubstitutionConcern
return '' return ''
end end
text = replace_type_de_champ_tags(text, procedure.types_de_champ, dossier.champs) text = replace_type_de_champ_tags(text, filter_tags(champ_public_tags), dossier.champs)
text = replace_type_de_champ_tags(text, procedure.types_de_champ_private, dossier.champs_private) text = replace_type_de_champ_tags(text, filter_tags(champ_private_tags), dossier.champs_private)
tags_and_datas = [ tags_and_datas = [
[dossier_tags, dossier], [dossier_tags, dossier],
[individual_tags, dossier.individual], [INDIVIDUAL_TAGS, dossier.individual],
[entreprise_tags, dossier.entreprise], [ENTREPRISE_TAGS, dossier.entreprise],
[etablissement_tags, dossier.entreprise&.etablissement] [ETABLISSEMENT_TAGS, dossier.entreprise&.etablissement]
] ]
tags_and_datas tags_and_datas
.map { |(tags, data)| [filter_tags(tags, dossier.termine?), data] } .map { |(tags, data)| [filter_tags(tags), data] }
.inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) } .inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) }
end end
@ -121,7 +210,7 @@ module TagsSubstitutionConcern
if tag.key?(:target) if tag.key?(:target)
value = data.send(tag[:target]) value = data.send(tag[:target])
else else
value = tag[:lambda].(data) value = instance_exec(data, &tag[:lambda])
end end
replace_tag(acc, tag, value) replace_tag(acc, tag, value)
end end

View file

@ -10,6 +10,8 @@ class Dossier < ActiveRecord::Base
EN_CONSTRUCTION_OU_INSTRUCTION = %w(en_construction en_instruction) EN_CONSTRUCTION_OU_INSTRUCTION = %w(en_construction en_instruction)
TERMINE = %w(accepte refuse sans_suite) TERMINE = %w(accepte refuse sans_suite)
INSTRUCTION_COMMENCEE = TERMINE + %w(en_instruction)
SOUMIS = EN_CONSTRUCTION_OU_INSTRUCTION + TERMINE
has_one :etablissement, dependent: :destroy has_one :etablissement, dependent: :destroy
has_one :entreprise, dependent: :destroy has_one :entreprise, dependent: :destroy

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/closed_mail" TEMPLATE_NAME = "mails/closed_mail"
DISPLAYED_NAME = "Accusé d'acceptation" DISPLAYED_NAME = "Accusé d'acceptation"
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été accepté' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été accepté'
IS_DOSSIER_TERMINE = true DOSSIER_STATE = 'accepte'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/initiated_mail" TEMPLATE_NAME = "mails/initiated_mail"
DISPLAYED_NAME = 'Accusé de réception' DISPLAYED_NAME = 'Accusé de réception'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a bien été reçu' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a bien été reçu'
IS_DOSSIER_TERMINE = false DOSSIER_STATE = 'en_construction'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/received_mail" TEMPLATE_NAME = "mails/received_mail"
DISPLAYED_NAME = 'Accusé de passage en instruction' DISPLAYED_NAME = 'Accusé de passage en instruction'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- va être instruit' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- va être instruit'
IS_DOSSIER_TERMINE = false DOSSIER_STATE = 'en_instruction'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/refused_mail" TEMPLATE_NAME = "mails/refused_mail"
DISPLAYED_NAME = 'Accusé de rejet du dossier' DISPLAYED_NAME = 'Accusé de rejet du dossier'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été refusé' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été refusé'
IS_DOSSIER_TERMINE = true DOSSIER_STATE = 'refuse'
end end
end end

View file

@ -8,6 +8,6 @@ module Mails
TEMPLATE_NAME = "mails/without_continuation_mail" TEMPLATE_NAME = "mails/without_continuation_mail"
DISPLAYED_NAME = 'Accusé de classement sans suite' DISPLAYED_NAME = 'Accusé de classement sans suite'
DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été classé sans suite' DEFAULT_SUBJECT = 'Votre dossier TPS nº --numéro du dossier-- a été classé sans suite'
IS_DOSSIER_TERMINE = true DOSSIER_STATE = 'sans_suite'
end end
end end

View file

@ -4,7 +4,7 @@ describe MailTemplateConcern do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier2) { create(:dossier, procedure: procedure) } let(:dossier2) { create(:dossier, procedure: procedure) }
let(:initiated_mail) { Mails::InitiatedMail.default_for_procedure(procedure) } let(:initiated_mail) { create(:initiated_mail, procedure: procedure) }
shared_examples "can replace tokens in template" do shared_examples "can replace tokens in template" do
describe 'with no token to replace' do describe 'with no token to replace' do
@ -47,6 +47,22 @@ describe MailTemplateConcern do
it_behaves_like "can replace tokens in template" it_behaves_like "can replace tokens in template"
end end
describe 'tags' do
describe 'in initiated mail' do
it "does not treat date de passage en instruction as a tag" do
expect(initiated_mail.tags).not_to include(include({ libelle: 'date de passage en instruction' }))
end
end
describe 'in received mail' do
let(:received_mail) { create(:received_mail, procedure: procedure) }
it "treats date de passage en instruction as a tag" do
expect(received_mail.tags).to include(include({ libelle: 'date de passage en instruction' }))
end
end
end
describe '.replace_tags' do describe '.replace_tags' do
before { initiated_mail.body = "n --numéro du dossier--" } before { initiated_mail.body = "n --numéro du dossier--" }
it "avoids side effects" do it "avoids side effects" do

View file

@ -2,6 +2,7 @@ describe TagsSubstitutionConcern, type: :model do
let(:types_de_champ) { [] } let(:types_de_champ) { [] }
let(:types_de_champ_private) { [] } let(:types_de_champ_private) { [] }
let(:for_individual) { false } let(:for_individual) { false }
let(:state) { 'accepte' }
let(:procedure) do let(:procedure) do
create(:procedure, create(:procedure,
@ -16,14 +17,15 @@ describe TagsSubstitutionConcern, type: :model do
include TagsSubstitutionConcern include TagsSubstitutionConcern
public :replace_tags public :replace_tags
def initialize(p) def initialize(p, s)
@procedure = p @procedure = p
self.class.const_set(:DOSSIER_STATE, s)
end end
def procedure def procedure
@procedure @procedure
end end
end).new(procedure) end).new(procedure, state)
end end
describe 'replace_tags' do describe 'replace_tags' do
@ -119,8 +121,6 @@ describe TagsSubstitutionConcern, type: :model do
context 'when the dossier has a motivation' do context 'when the dossier has a motivation' do
let(:dossier) { create(:dossier, motivation: 'motivation') } let(:dossier) { create(:dossier, motivation: 'motivation') }
before { dossier.accepte! }
context 'and the template has some dossier tags' do context 'and the template has some dossier tags' do
let(:template) { '--motivation-- --numéro du dossier--' } let(:template) { '--motivation-- --numéro du dossier--' }
@ -131,10 +131,10 @@ describe TagsSubstitutionConcern, type: :model do
context 'when the procedure has a type de champ prive named libelleA' do context 'when the procedure has a type de champ prive named libelleA' do
let(:types_de_champ_private) { [create(:type_de_champ_private, libelle: 'libelleA')] } let(:types_de_champ_private) { [create(:type_de_champ_private, libelle: 'libelleA')] }
context 'and the are used in the template' do context 'and it is used in the template' do
let(:template) { '--libelleA--' } let(:template) { '--libelleA--' }
context 'and its value in the dossier are not nil' do context 'and its value in the dossier is not nil' do
before { dossier.champs_private.first.update_attributes(value: 'libelle1') } before { dossier.champs_private.first.update_attributes(value: 'libelle1') }
it { is_expected.to eq('libelle1') } it { is_expected.to eq('libelle1') }
@ -142,6 +142,28 @@ describe TagsSubstitutionConcern, type: :model do
end end
end end
context 'when the dossier is en construction' do
let(:state) { 'en_construction' }
let(:template) { '--libelleA--' }
context 'champs privés are not valid tags' do
# The dossier just transitionned from brouillon to en construction,
# so champs private are not valid tags yet
let(:types_de_champ_private) { [create(:type_de_champ_private, libelle: 'libelleA')] }
it { is_expected.to eq('--libelleA--') }
end
context 'champs publics are valid tags' do
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'libelleA')] }
before { dossier.champs.first.update_attributes(value: 'libelle1') }
it { is_expected.to eq('libelle1') }
end
end
context 'when the procedure has 2 types de champ date and datetime' do context 'when the procedure has 2 types de champ date and datetime' do
let(:types_de_champ) do let(:types_de_champ) do
[ [
@ -173,7 +195,6 @@ describe TagsSubstitutionConcern, type: :model do
context "when using a date tag" do context "when using a date tag" do
before do before do
dossier.accepte!
dossier.en_construction_at = DateTime.new(2001, 2, 3) dossier.en_construction_at = DateTime.new(2001, 2, 3)
dossier.en_instruction_at = DateTime.new(2004, 5, 6) dossier.en_instruction_at = DateTime.new(2004, 5, 6)
dossier.processed_at = DateTime.new(2007, 8, 9) dossier.processed_at = DateTime.new(2007, 8, 9)
@ -237,6 +258,7 @@ describe TagsSubstitutionConcern, type: :model do
context 'when generating a document for a dossier that is not termine' do context 'when generating a document for a dossier that is not termine' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
let(:template) { '--motivation-- --date de décision--' } let(:template) { '--motivation-- --date de décision--' }
let(:state) { 'en_instruction' }
subject { template_concern.replace_tags(template, dossier) } subject { template_concern.replace_tags(template, dossier) }
@ -247,18 +269,36 @@ describe TagsSubstitutionConcern, type: :model do
end end
describe 'tags' do describe 'tags' do
context 'when generating a document for a dossier terminé' do
subject { template_concern.tags } subject { template_concern.tags }
let(:types_de_champ) { [create(:type_de_champ_public, libelle: 'public')] }
let(:types_de_champ_private) { [create(:type_de_champ_private, libelle: 'privé')] }
context 'when generating a document for a dossier terminé' do
it { is_expected.to include(include({ libelle: 'motivation' })) } it { is_expected.to include(include({ libelle: 'motivation' })) }
it { is_expected.to include(include({ libelle: 'date de décision' })) } it { is_expected.to include(include({ libelle: 'date de décision' })) }
it { is_expected.to include(include({ libelle: 'public' })) }
it { is_expected.to include(include({ libelle: 'privé' })) }
end end
context 'when generating a document for a dossier that is not terminé' do context 'when generating a document for a dossier en instruction' do
subject { template_concern.tags(is_dossier_termine: false) } let(:state) { 'en_instruction' }
it { is_expected.not_to include(include({ libelle: 'motivation' })) } it { is_expected.not_to include(include({ libelle: 'motivation' })) }
it { is_expected.not_to include(include({ libelle: 'date de décision' })) } it { is_expected.not_to include(include({ libelle: 'date de décision' })) }
it { is_expected.to include(include({ libelle: 'public' })) }
it { is_expected.to include(include({ libelle: 'privé' })) }
end
context 'when generating a document for a dossier en construction' do
let(:state) { 'en_construction' }
it { is_expected.not_to include(include({ libelle: 'motivation' })) }
it { is_expected.not_to include(include({ libelle: 'date de décision' })) }
it { is_expected.not_to include(include({ libelle: 'privé' })) }
it { is_expected.to include(include({ libelle: 'public' })) }
end end
end end
end end