Merge pull request #1802 from SocialGouv/issue-1494

Warn a user before accepting a dossier if fields of the attestation are empty
This commit is contained in:
gregoirenovel 2018-04-06 16:19:21 +02:00 committed by GitHub
commit f2f7fae6c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 0 deletions

View file

@ -27,4 +27,21 @@
font-size: 11px;
margin-bottom: $default-spacer * 2;
}
.warning {
background-color: $orange-bg;
padding: $default-spacer;
margin-bottom: $default-spacer * 2;
border-radius: 3px;
h4 {
margin-top: $default-spacer;
font-weight: bold;
}
ul {
list-style: disc;
padding-left: $default-spacer * 1.5;
}
}
}

View file

@ -17,6 +17,20 @@ class AttestationTemplate < ApplicationRecord
Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier))
end
def unspecified_champs_for_dossier(dossier)
all_champs_with_libelle_index = (dossier.champs + dossier.champs_private)
.reduce({}) do |acc, champ|
acc[champ.libelle] = champ
acc
end
used_tags.map do |used_tag|
corresponding_champ = all_champs_with_libelle_index[used_tag]
corresponding_champ.value.blank? ? corresponding_champ : nil
end.compact
end
def dup
result = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
@ -33,6 +47,14 @@ class AttestationTemplate < ApplicationRecord
private
def used_tags
delimiters_regex = /--(?<capture>((?!--).)*)--/
[title, body]
.map { |str| str.scan(delimiters_regex) }
.flatten
end
def logo_signature_file_size
%i[logo signature]
.select { |file_name| send(file_name).present? }

View file

@ -283,6 +283,16 @@ class Dossier < ApplicationRecord
end
end
def unspecified_attestation_champs
attestation_template = procedure.attestation_template
if attestation_template.present? && attestation_template.activated?
attestation_template.unspecified_champs_for_dossier(self)
else
[]
end
end
def build_attestation
if procedure.attestation_template.present? && procedure.attestation_template.activated?
procedure.attestation_template.attestation_for(self)

View file

@ -8,6 +8,25 @@
- if title == 'Accepter'
%p.help
L'acceptation du dossier envoie automatiquement une attestation à l'usager.
- unspecified_attestation_champs = dossier.unspecified_attestation_champs
- if unspecified_attestation_champs.present?
.warning
Attention, les valeurs suivantes n'ont pas été renseignées mais sont nécessaires pour pouvoir envoyer une attestation valide :
- unspecified_champs, unspecified_annotations_privees = unspecified_attestation_champs.partition(&:private)
- if unspecified_champs.present?
%h4 Champs de la demande
%ul
- unspecified_attestation_champs.each do |unspecified_champ|
%li= unspecified_champ.libelle
- if unspecified_annotations_privees.present?
%h4 Annotations privées
%ul
- unspecified_annotations_privees.each do |unspecified_annotations_privee|
%li= unspecified_annotations_privee.libelle
.text-right
%span.button{ onclick: 'DS.motivationCancel();' } Annuler
= button_tag 'Valider la décision', name: :process_action, value: process_action, class: 'button primary', title: title, data: { confirm: confirm }

View file

@ -643,6 +643,64 @@ describe Dossier do
end
end
describe "#unspecified_attestation_champs" do
let(:procedure) { create(:procedure, attestation_template: attestation_template) }
let(:dossier) { create(:dossier, procedure: procedure, state: :en_instruction) }
subject { dossier.unspecified_attestation_champs.map(&:libelle) }
context "without attestation template" do
let(:attestation_template) { nil }
it { is_expected.to eq([]) }
end
context "with attestation template" do
# Test all combinations:
# - with tag specified and unspecified
# - with tag in body and tag in title
# - with tag correponsing to a champ and an annotation privée
# - with a dash in the champ libelle / tag
let(:title) { "voici --specified champ-in-title-- un --unspecified champ-in-title-- beau --specified annotation privée-in-title-- titre --unspecified annotation privée-in-title-- non" }
let(:body) { "voici --specified champ-in-body-- un --unspecified champ-in-body-- beau --specified annotation privée-in-body-- body --unspecified annotation privée-in-body-- non ?" }
let(:attestation_template) { create(:attestation_template, title: title, body: body, activated: activated) }
context "which is disabled" do
let(:activated) { false }
it { is_expected.to eq([]) }
end
context "wich is enabled" do
let(:activated) { true }
let!(:tdc_1) { create(:type_de_champ, libelle: "specified champ-in-title", procedure: procedure) }
let!(:tdc_2) { create(:type_de_champ, libelle: "unspecified champ-in-title", procedure: procedure) }
let!(:tdc_3) { create(:type_de_champ, libelle: "specified champ-in-body", procedure: procedure) }
let!(:tdc_4) { create(:type_de_champ, libelle: "unspecified champ-in-body", procedure: procedure) }
let!(:tdc_5) { create(:type_de_champ, private: true, libelle: "specified annotation privée-in-title", procedure: procedure) }
let!(:tdc_6) { create(:type_de_champ, private: true, libelle: "unspecified annotation privée-in-title", procedure: procedure) }
let!(:tdc_7) { create(:type_de_champ, private: true, libelle: "specified annotation privée-in-body", procedure: procedure) }
let!(:tdc_8) { create(:type_de_champ, private: true, libelle: "unspecified annotation privée-in-body", procedure: procedure) }
before do
(dossier.champs + dossier.champs_private)
.select { |c| c.libelle.match?(/^specified/) }
.each { |c| c.update_attribute(:value, "specified") }
end
it do
is_expected.to eq([
"unspecified champ-in-title",
"unspecified annotation privée-in-title",
"unspecified champ-in-body",
"unspecified annotation privée-in-body"
])
end
end
end
end
describe '#build_attestation' do
let(:attestation_template) { nil }
let(:procedure) { create(:procedure, attestation_template: attestation_template) }