From b42e410da0edab27a34993af3a432243534c47fc Mon Sep 17 00:00:00 2001 From: kemar Date: Fri, 6 Apr 2018 13:09:37 +0200 Subject: [PATCH] [Fix #1494] Display a warning when accepting a dossier if needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Show the accompagnateur the unspecified demande champs and attestations privées needed for the attestation --- .../stylesheets/new_design/motivation.scss | 17 ++++++ app/models/attestation_template.rb | 22 +++++++ app/models/dossier.rb | 10 ++++ .../_state_button_motivation.html.haml | 19 ++++++ spec/models/dossier_spec.rb | 58 +++++++++++++++++++ 5 files changed, 126 insertions(+) diff --git a/app/assets/stylesheets/new_design/motivation.scss b/app/assets/stylesheets/new_design/motivation.scss index cf5d684de..52781f9b2 100644 --- a/app/assets/stylesheets/new_design/motivation.scss +++ b/app/assets/stylesheets/new_design/motivation.scss @@ -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; + } + } } diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 446b2096e..aeb980d79 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -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 = /--(?((?!--).)*)--/ + + [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? } diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 032248b79..89f564cce 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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) diff --git a/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml b/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml index 95207a121..1c4a79335 100644 --- a/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml +++ b/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml @@ -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 } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 40ea3bbd2..878a38fea 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -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) }