From e98f90e1578265e6df4a37923634af72b9196a7f Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Fri, 7 Jun 2024 13:19:04 +0200 Subject: [PATCH] fix(attestation): fix unspecified_attestation_champs for v2 --- app/models/attestation_template.rb | 2 +- spec/factories/attestation_template.rb | 8 +-- spec/models/dossier_spec.rb | 78 ++++++++++++++++++-------- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index a00aa30cac4..f31f539f09b 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -210,7 +210,7 @@ def signature_to_render(groupe_instructeur) def used_tags if version == 2 json = json_body&.deep_symbolize_keys - TiptapService.used_tags_and_libelle_for(json.deep_symbolize_keys) + TiptapService.used_tags_and_libelle_for(json.deep_symbolize_keys).map(&:first) else used_tags_for(title) + used_tags_for(body) end diff --git a/spec/factories/attestation_template.rb b/spec/factories/attestation_template.rb index be4c39626e5..d18770402d4 100644 --- a/spec/factories/attestation_template.rb +++ b/spec/factories/attestation_template.rb @@ -32,10 +32,10 @@ { "type" => "paragraph", "attrs" => { "textAlign" => "left" }, "content" => [{ "text" => "Dossier: n° ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }, { "type" => "paragraph", - "content" => [ - { "text" => "Nom: ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "individual_last_name", "label" => "prénom" } }, { "text" => " ", "type" => "text" }, - { "type" => "mention", "attrs" => { "id" => "individual_first_name", "label" => "nom" } }, { "text" => " ", "type" => "text" } - ] + "content" => [ + { "text" => "Nom: ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "individual_last_name", "label" => "prénom" } }, { "text" => " ", "type" => "text" }, + { "type" => "mention", "attrs" => { "id" => "individual_first_name", "label" => "nom" } }, { "text" => " ", "type" => "text" } + ] } ] } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 92f0b0944f2..76fb5414aed 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -685,8 +685,24 @@ describe "#unspecified_attestation_champs" do let(:procedure) { create(:procedure, attestation_template: attestation_template, types_de_champ_public: types_de_champ, types_de_champ_private: types_de_champ_private) } let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) } - let(:types_de_champ) { [] } - let(:types_de_champ_private) { [] } + + let(:types_de_champ) { [tdc_1, tdc_2, tdc_3, tdc_4] } + let(:types_de_champ_private) { [tdc_5, tdc_6, tdc_7, tdc_8] } + + let(:tdc_1) { { libelle: "specified champ-in-title" } } + let(:tdc_2) { { libelle: "unspecified champ-in-title" } } + let(:tdc_3) { { libelle: "specified champ-in-body" } } + let(:tdc_4) { { libelle: "unspecified champ-in-body" } } + let(:tdc_5) { { libelle: "specified annotation privée-in-title" } } + let(:tdc_6) { { libelle: "unspecified annotation privée-in-title" } } + let(:tdc_7) { { libelle: "specified annotation privée-in-body" } } + let(:tdc_8) { { libelle: "unspecified annotation privée-in-body" } } + + before do + (dossier.champs_public + dossier.champs_private) + .filter { |c| c.libelle.match?(/^specified/) } + .each { |c| c.update_attribute(:value, "specified") } + end subject { dossier.unspecified_attestation_champs.map(&:libelle) } @@ -696,11 +712,11 @@ it { is_expected.to eq([]) } end - context "with attestation template" do + context "with attestation template v1" 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 tag correponding 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 --numéro du dossier--" } 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 ?" } @@ -712,27 +728,9 @@ it { is_expected.to eq([]) } end - context "wich is enabled" do + context "which is enabled" do let(:activated) { true } - let(:types_de_champ) { [tdc_1, tdc_2, tdc_3, tdc_4] } - let(:types_de_champ_private) { [tdc_5, tdc_6, tdc_7, tdc_8] } - - let(:tdc_1) { { libelle: "specified champ-in-title" } } - let(:tdc_2) { { libelle: "unspecified champ-in-title" } } - let(:tdc_3) { { libelle: "specified champ-in-body" } } - let(:tdc_4) { { libelle: "unspecified champ-in-body" } } - let(:tdc_5) { { libelle: "specified annotation privée-in-title" } } - let(:tdc_6) { { libelle: "unspecified annotation privée-in-title" } } - let(:tdc_7) { { libelle: "specified annotation privée-in-body" } } - let(:tdc_8) { { libelle: "unspecified annotation privée-in-body" } } - - before do - (dossier.champs_public + dossier.champs_private) - .filter { |c| c.libelle.match?(/^specified/) } - .each { |c| c.update_attribute(:value, "specified") } - end - it do is_expected.to eq([ "unspecified champ-in-title", @@ -743,6 +741,40 @@ end end end + + context "with attestation template v2" do + # Test all combinations: + # - with tag specified and unspecified + # - with tag correponding to a champ and an annotation privée + let(:body) { + [ + { "type" => "mention", "attrs" => { "id" => "tdc#{procedure.types_de_champ_for_tags.find {  _1.libelle == "unspecified champ-in-body" }.stable_id}", "label" => "unspecified champ-in-body" } } + ] + } + let(:attestation_template) { build(:attestation_template, :v2) } + + before do + tdc_content = (types_de_champ + types_de_champ_private).filter_map do |tdc_config| + next if tdc_config[:libelle].include?("in-title") + + { + "type" => "mention", + "attrs" => { "id" => "tdc#{procedure.types_de_champ_for_tags.find { _1.libelle == tdc_config[:libelle] }.stable_id}", "label" => tdc_config[:libelle] } + } + end + + json_body = attestation_template.json_body["content"] + attestation_template.json_body["content"][-1]["content"].concat(tdc_content) + attestation_template.save! + end + + it do + is_expected.to eq([ + "unspecified champ-in-body", + "unspecified annotation privée-in-body" + ]) + end + end end describe '#build_attestation' do