fix(attestation): fix unspecified_attestation_champs for v2

This commit is contained in:
Colin Darie 2024-06-07 13:19:04 +02:00
parent a2c0379127
commit f2669fbca8
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
3 changed files with 60 additions and 28 deletions

View file

@ -210,7 +210,7 @@ class AttestationTemplate < ApplicationRecord
def used_tags def used_tags
if version == 2 if version == 2
json = json_body&.deep_symbolize_keys 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 else
used_tags_for(title) + used_tags_for(body) used_tags_for(title) + used_tags_for(body)
end end

View file

@ -32,10 +32,10 @@ FactoryBot.define do
{ "type" => "paragraph", "attrs" => { "textAlign" => "left" }, "content" => [{ "text" => "Dossier: n° ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }, { "type" => "paragraph", "attrs" => { "textAlign" => "left" }, "content" => [{ "text" => "Dossier: n° ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] },
{ {
"type" => "paragraph", "type" => "paragraph",
"content" => [ "content" => [
{ "text" => "Nom: ", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "individual_last_name", "label" => "prénom" } }, { "text" => " ", "type" => "text" }, { "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" } { "type" => "mention", "attrs" => { "id" => "individual_first_name", "label" => "nom" } }, { "text" => " ", "type" => "text" }
] ]
} }
] ]
} }

View file

@ -685,8 +685,24 @@ describe Dossier, type: :model do
describe "#unspecified_attestation_champs" do 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(: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(: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) } subject { dossier.unspecified_attestation_champs.map(&:libelle) }
@ -696,11 +712,11 @@ describe Dossier, type: :model do
it { is_expected.to eq([]) } it { is_expected.to eq([]) }
end end
context "with attestation template" do context "with attestation template v1" do
# Test all combinations: # Test all combinations:
# - with tag specified and unspecified # - with tag specified and unspecified
# - with tag in body and tag in title # - 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 # - 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(: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 ?" } 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 @@ describe Dossier, type: :model do
it { is_expected.to eq([]) } it { is_expected.to eq([]) }
end end
context "wich is enabled" do context "which is enabled" do
let(:activated) { true } 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 it do
is_expected.to eq([ is_expected.to eq([
"unspecified champ-in-title", "unspecified champ-in-title",
@ -743,6 +741,40 @@ describe Dossier, type: :model do
end end
end 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 end
describe '#build_attestation' do describe '#build_attestation' do