Merge pull request #10465 from colinux/attestations-v2-prod
ETQ admin je peux activer la délivrance des attestations v2 (sous feature flag)
This commit is contained in:
commit
ccf5b255ed
42 changed files with 724 additions and 258 deletions
|
@ -2,11 +2,16 @@ class AttestationTemplate < ApplicationRecord
|
|||
include ActionView::Helpers::NumberHelper
|
||||
include TagsSubstitutionConcern
|
||||
|
||||
belongs_to :procedure, inverse_of: :attestation_template_v2
|
||||
belongs_to :procedure, inverse_of: :attestation_template
|
||||
|
||||
has_one_attached :logo
|
||||
has_one_attached :signature
|
||||
|
||||
enum state: {
|
||||
draft: 'draft',
|
||||
published: 'published'
|
||||
}
|
||||
|
||||
validates :title, tags: true, if: -> { procedure.present? && version == 1 }
|
||||
validates :body, tags: true, if: -> { procedure.present? && version == 1 }
|
||||
validates :json_body, tags: true, if: -> { procedure.present? && version == 2 }
|
||||
|
@ -67,9 +72,10 @@ class AttestationTemplate < ApplicationRecord
|
|||
}.freeze
|
||||
|
||||
def attestation_for(dossier)
|
||||
attestation = Attestation.new(title: replace_tags(title, dossier, escape: false))
|
||||
attestation = Attestation.new
|
||||
attestation.title = replace_tags(title, dossier, escape: false) if version == 1
|
||||
attestation.pdf.attach(
|
||||
io: build_pdf(dossier),
|
||||
io: StringIO.new(build_pdf(dossier)),
|
||||
filename: "attestation-dossier-#{dossier.id}.pdf",
|
||||
content_type: 'application/pdf',
|
||||
# we don't want to run virus scanner on this file
|
||||
|
@ -91,7 +97,7 @@ class AttestationTemplate < ApplicationRecord
|
|||
end
|
||||
|
||||
def dup
|
||||
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
||||
attestation_template = super
|
||||
ClonePiecesJustificativesService.clone_attachments(self, attestation_template)
|
||||
attestation_template
|
||||
end
|
||||
|
@ -179,7 +185,7 @@ class AttestationTemplate < ApplicationRecord
|
|||
|
||||
if dossier.present?
|
||||
# 2x faster this way than with `replace_tags` which would reparse text
|
||||
used_tags = tiptap.used_tags_and_libelle_for(json.deep_symbolize_keys)
|
||||
used_tags = TiptapService.used_tags_and_libelle_for(json.deep_symbolize_keys)
|
||||
substitutions = tags_substitutions(used_tags, dossier, escape: false)
|
||||
body = tiptap.to_html(json, substitutions)
|
||||
|
||||
|
@ -202,17 +208,41 @@ class AttestationTemplate < ApplicationRecord
|
|||
end
|
||||
|
||||
def used_tags
|
||||
used_tags_for(title) + used_tags_for(body)
|
||||
if version == 2
|
||||
json = json_body&.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
|
||||
end
|
||||
|
||||
def build_pdf(dossier)
|
||||
if version == 2
|
||||
build_v2_pdf(dossier)
|
||||
else
|
||||
build_v1_pdf(dossier)
|
||||
end
|
||||
end
|
||||
|
||||
def build_v1_pdf(dossier)
|
||||
attestation = render_attributes_for(dossier: dossier)
|
||||
attestation_view = ApplicationController.render(
|
||||
ApplicationController.render(
|
||||
template: 'administrateurs/attestation_templates/show',
|
||||
formats: :pdf,
|
||||
assigns: { attestation: attestation }
|
||||
)
|
||||
end
|
||||
|
||||
StringIO.new(attestation_view)
|
||||
def build_v2_pdf(dossier)
|
||||
body = render_attributes_for(dossier:).fetch(:body)
|
||||
|
||||
html = ApplicationController.render(
|
||||
template: '/administrateurs/attestation_template_v2s/show',
|
||||
formats: [:html],
|
||||
layout: 'attestation',
|
||||
assigns: { attestation_template: self, body: body }
|
||||
)
|
||||
|
||||
WeasyprintService.generate_pdf(html, { procedure_id: procedure.id, dossier_id: dossier.id })
|
||||
end
|
||||
end
|
||||
|
|
|
@ -257,7 +257,7 @@ module TagsSubstitutionConcern
|
|||
def used_type_de_champ_tags(text_or_tiptap)
|
||||
used_tags =
|
||||
if text_or_tiptap.respond_to?(:deconstruct_keys) # hash pattern matching
|
||||
TiptapService.new.used_tags_and_libelle_for(text_or_tiptap.deep_symbolize_keys)
|
||||
TiptapService.used_tags_and_libelle_for(text_or_tiptap.deep_symbolize_keys)
|
||||
else
|
||||
used_tags_and_libelle_for(text_or_tiptap.to_s)
|
||||
end
|
||||
|
|
|
@ -66,11 +66,10 @@ class ExportTemplate < ApplicationRecord
|
|||
end
|
||||
|
||||
def render_attributes_for(content_for, dossier, attachment = nil)
|
||||
tiptap = TiptapService.new
|
||||
used_tags = tiptap.used_tags_and_libelle_for(content_for.deep_symbolize_keys)
|
||||
used_tags = TiptapService.used_tags_and_libelle_for(content_for.deep_symbolize_keys)
|
||||
substitutions = tags_substitutions(used_tags, dossier, escape: false, memoize: true)
|
||||
substitutions['original-filename'] = attachment.filename.base if attachment
|
||||
tiptap.to_path(content_for.deep_symbolize_keys, substitutions)
|
||||
TiptapService.new.to_path(content_for.deep_symbolize_keys, substitutions)
|
||||
end
|
||||
|
||||
def specific_tags
|
||||
|
|
|
@ -50,9 +50,9 @@ class Procedure < ApplicationRecord
|
|||
has_one :module_api_carto, dependent: :destroy
|
||||
has_many :attestation_templates, dependent: :destroy
|
||||
has_one :attestation_template_v1, -> { AttestationTemplate.v1 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure
|
||||
has_one :attestation_template_v2, -> { AttestationTemplate.v2 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure
|
||||
has_many :attestation_templates_v2, -> { AttestationTemplate.v2 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure
|
||||
|
||||
has_one :attestation_template, -> { order(Arel.sql("CASE WHEN version = '1' THEN 0 ELSE 1 END")) }, dependent: :destroy, inverse_of: :procedure
|
||||
has_one :attestation_template, -> { published }, dependent: :destroy, inverse_of: :procedure
|
||||
|
||||
belongs_to :parent_procedure, class_name: 'Procedure', optional: true
|
||||
belongs_to :canonical_procedure, class_name: 'Procedure', optional: true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue