simplify export_template model and spec
This commit is contained in:
parent
977cfc87ce
commit
8906a59635
6 changed files with 99 additions and 521 deletions
|
@ -1,157 +1,66 @@
|
|||
class ExportTemplate < ApplicationRecord
|
||||
include TagsSubstitutionConcern
|
||||
|
||||
self.ignored_columns += ["content"]
|
||||
|
||||
belongs_to :groupe_instructeur
|
||||
has_one :procedure, through: :groupe_instructeur
|
||||
has_many :exports, dependent: :nullify
|
||||
|
||||
enum kind: { zip: "zip" }, _prefix: :template
|
||||
|
||||
attribute :dossier_folder, :export_item
|
||||
attribute :export_pdf, :export_item
|
||||
attribute :pjs, :export_item, array: true
|
||||
|
||||
before_validation :ensure_pjs_are_legit
|
||||
|
||||
validates_with ExportTemplateValidator
|
||||
|
||||
DOSSIER_STATE = Dossier.states.fetch(:en_construction)
|
||||
FORMAT_DATE = "%Y-%m-%d"
|
||||
|
||||
def set_default_values
|
||||
content["default_dossier_directory"] = tiptap_json("dossier-")
|
||||
content["pdf_name"] = tiptap_json("export_")
|
||||
|
||||
content["pjs"] = []
|
||||
procedure.exportables_pieces_jointes.each do |pj|
|
||||
content["pjs"] << { "stable_id" => pj.stable_id.to_s, "path" => tiptap_json("#{pj.libelle.parameterize}-") }
|
||||
end
|
||||
# when a pj has been added to a revision, it will not be present in the previous pjs
|
||||
# a default value is provided.
|
||||
def pj(tdc)
|
||||
pjs.find { _1.stable_id == tdc.stable_id } || ExportItem.default_pj(tdc)
|
||||
end
|
||||
|
||||
def tiptap_default_dossier_directory=(body)
|
||||
self.content["default_dossier_directory"] = JSON.parse(body)
|
||||
def self.default(name: nil, kind: 'zip', groupe_instructeur:)
|
||||
dossier_folder = ExportItem.default(prefix: 'dossier')
|
||||
export_pdf = ExportItem.default(prefix: 'export')
|
||||
pjs = groupe_instructeur.procedure.exportables_pieces_jointes.map { |tdc| ExportItem.default_pj(tdc) }
|
||||
|
||||
new(name:, kind:, groupe_instructeur:, dossier_folder:, export_pdf:, pjs:)
|
||||
end
|
||||
|
||||
def tiptap_default_dossier_directory
|
||||
tiptap_content("default_dossier_directory")
|
||||
end
|
||||
|
||||
def tiptap_pdf_name=(body)
|
||||
self.content["pdf_name"] = JSON.parse(body)
|
||||
end
|
||||
|
||||
def tiptap_pdf_name
|
||||
tiptap_content("pdf_name")
|
||||
end
|
||||
|
||||
def content_for_pj(pj)
|
||||
content_for_pj_id(pj.stable_id)&.to_json
|
||||
end
|
||||
|
||||
def assign_pj_names(pj_params)
|
||||
self.content["pjs"] = []
|
||||
pj_params.each do |pj_param|
|
||||
self.content["pjs"] << { stable_id: pj_param[0].delete_prefix("tiptap_pj_"), path: JSON.parse(pj_param[1]) }
|
||||
end
|
||||
end
|
||||
|
||||
def attachment_and_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
||||
[
|
||||
attachment,
|
||||
path(dossier, attachment, index:, row_index:, champ:)
|
||||
]
|
||||
end
|
||||
|
||||
def tiptap_convert(dossier, param)
|
||||
if content[param]["content"]&.first&.[]("content")
|
||||
render_attributes_for(content[param], dossier)
|
||||
end
|
||||
end
|
||||
|
||||
def tiptap_convert_pj(dossier, pj_stable_id, attachment = nil)
|
||||
if content_for_pj_id(pj_stable_id)["content"]&.first&.[]("content")
|
||||
render_attributes_for(content_for_pj_id(pj_stable_id), dossier, attachment)
|
||||
end
|
||||
end
|
||||
|
||||
def render_attributes_for(content_for, dossier, attachment = nil)
|
||||
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
|
||||
TiptapService.new.to_path(content_for.deep_symbolize_keys, substitutions)
|
||||
end
|
||||
|
||||
def specific_tags
|
||||
def tags
|
||||
tags_categorized.slice(:individual, :etablissement, :dossier).values.flatten
|
||||
end
|
||||
|
||||
def tags_for_pj
|
||||
specific_tags.push({
|
||||
def pj_tags
|
||||
tags.push(
|
||||
libelle: 'nom original du fichier',
|
||||
id: 'original-filename',
|
||||
maybe_null: false
|
||||
})
|
||||
id: 'original-filename'
|
||||
)
|
||||
end
|
||||
|
||||
def attachment_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
||||
file_path = if attachment.name == 'pdf_export_for_instructeur'
|
||||
export_pdf.path(dossier, attachment:)
|
||||
elsif attachment.record_type == 'Champ' && pj(champ.type_de_champ).enabled?
|
||||
pj(champ.type_de_champ).path(dossier, attachment:, index:, row_index:)
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
File.join(dossier_folder.path(dossier), file_path) if file_path.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tiptap_content(key)
|
||||
content[key]&.to_json
|
||||
end
|
||||
def ensure_pjs_are_legit
|
||||
legitimate_pj_stable_ids = procedure.exportables_pieces_jointes_for_all_versions.map(&:stable_id)
|
||||
|
||||
def tiptap_json(prefix)
|
||||
{
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => prefix, "type" => "text" }, { "type" => "mention", "attrs" => DOSSIER_ID_TAG.stringify_keys }] }
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def content_for_pj_id(stable_id)
|
||||
content_for_stable_id = content["pjs"].find { _1.symbolize_keys[:stable_id] == stable_id.to_s }
|
||||
content_for_stable_id.symbolize_keys.fetch(:path)
|
||||
end
|
||||
|
||||
def folder(dossier)
|
||||
render_attributes_for(content["default_dossier_directory"], dossier)
|
||||
end
|
||||
|
||||
def export_path(dossier)
|
||||
File.join(folder(dossier), export_filename(dossier))
|
||||
end
|
||||
|
||||
def export_filename(dossier)
|
||||
"#{render_attributes_for(content["pdf_name"], dossier)}.pdf"
|
||||
end
|
||||
|
||||
def path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
||||
if attachment.name == 'pdf_export_for_instructeur'
|
||||
return export_path(dossier)
|
||||
end
|
||||
|
||||
dir_path = case attachment.record_type
|
||||
when 'Dossier'
|
||||
'dossier'
|
||||
when 'Commentaire'
|
||||
'messagerie'
|
||||
when 'Avis'
|
||||
'avis'
|
||||
when 'Attestation', 'Etablissement'
|
||||
'pieces_justificatives'
|
||||
else
|
||||
# for attachment
|
||||
return attachment_path(dossier, attachment, index, row_index, champ)
|
||||
end
|
||||
|
||||
File.join(folder(dossier), dir_path, attachment.filename.to_s)
|
||||
end
|
||||
|
||||
def attachment_path(dossier, attachment, index, row_index, champ)
|
||||
stable_id = champ.stable_id
|
||||
tiptap_pj = content["pjs"].find { |pj| pj["stable_id"] == stable_id.to_s }
|
||||
if tiptap_pj
|
||||
File.join(folder(dossier), tiptap_convert_pj(dossier, stable_id, attachment) + suffix(attachment, index, row_index))
|
||||
else
|
||||
File.join(folder(dossier), "erreur_renommage", attachment.filename.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def suffix(attachment, index, row_index)
|
||||
suffix = "-#{index + 1}"
|
||||
suffix += "-#{row_index + 1}" if row_index.present?
|
||||
|
||||
suffix + attachment.filename.extension_with_delimiter
|
||||
self.pjs = pjs.filter { _1.stable_id.in?(legitimate_pj_stable_ids) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,86 +2,11 @@ FactoryBot.define do
|
|||
factory :export_template do
|
||||
name { "Mon export" }
|
||||
groupe_instructeur
|
||||
content {
|
||||
{
|
||||
"pdf_name" =>
|
||||
{
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_id", "label" => "id dossier" } }, { "text" => " .pdf", "type" => "text" }] }
|
||||
]
|
||||
},
|
||||
"default_dossier_directory" => {
|
||||
"type" => "doc",
|
||||
"content" =>
|
||||
[
|
||||
{
|
||||
"type" => "paragraph",
|
||||
"content" =>
|
||||
[
|
||||
{ "text" => "dossier_", "type" => "text" },
|
||||
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
|
||||
{ "text" => " ", "type" => "text" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
kind { "zip" }
|
||||
initialize_with { ExportTemplate.default(name:, groupe_instructeur: groupe_instructeur) }
|
||||
|
||||
to_create do |export_template, _context|
|
||||
export_template.set_default_values
|
||||
export_template.save
|
||||
end
|
||||
|
||||
trait :with_custom_content do
|
||||
to_create do |export_template, context|
|
||||
export_template.set_default_values
|
||||
export_template.content = context.content
|
||||
export_template.save
|
||||
end
|
||||
end
|
||||
|
||||
trait :with_custom_ddd_prefix do
|
||||
transient do
|
||||
ddd_prefix { 'dossier_' }
|
||||
end
|
||||
|
||||
to_create do |export_template, context|
|
||||
export_template.set_default_values
|
||||
export_template.content["default_dossier_directory"]["content"] = [
|
||||
{
|
||||
"type" => "paragraph",
|
||||
"content" =>
|
||||
[
|
||||
{ "text" => context.ddd_prefix, "type" => "text" },
|
||||
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
|
||||
{ "text" => " ", "type" => "text" }
|
||||
]
|
||||
}
|
||||
]
|
||||
export_template.save
|
||||
end
|
||||
end
|
||||
|
||||
trait :with_date_depot_for_export_pdf do
|
||||
to_create do |export_template, _|
|
||||
export_template.set_default_values
|
||||
export_template.content["pdf_name"]["content"] = [
|
||||
{
|
||||
"type" => "paragraph",
|
||||
"content" =>
|
||||
[
|
||||
{ "text" => "export_", "type" => "text" },
|
||||
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
|
||||
{ "text" => "-", "type" => "text" },
|
||||
{ "type" => "mention", "attrs" => { "id" => "dossier_depose_at", "label" => "date de dépôt" } },
|
||||
{ "text" => " ", "type" => "text" }
|
||||
]
|
||||
}
|
||||
]
|
||||
export_template.save
|
||||
trait :enabled_pjs do
|
||||
after(:build) do |export_template, _evaluator|
|
||||
export_template.pjs.each { _1.instance_variable_set('@enabled', true) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -110,9 +110,10 @@ RSpec.describe Export, type: :model do
|
|||
end
|
||||
|
||||
context 'with export template' do
|
||||
let(:export_template) { build(:export_template) }
|
||||
let(:export_template) { create(:export_template, groupe_instructeur: gi_1) }
|
||||
|
||||
it 'creates new export' do
|
||||
expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, export_template: export_template, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
|
||||
expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, export_template:, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
|
||||
.to change { Export.count }.by(1)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
describe ExportTemplate do
|
||||
let(:groupe_instructeur) { create(:groupe_instructeur, procedure:) }
|
||||
let(:export_template) { create(:export_template, :with_custom_content, groupe_instructeur:, content:) }
|
||||
let(:procedure) { create(:procedure_with_dossiers, types_de_champ_public:, for_individual:) }
|
||||
let(:dossier) { procedure.dossiers.first }
|
||||
let(:export_template) { build(:export_template, groupe_instructeur:) }
|
||||
let(:procedure) { create(:procedure, types_de_champ_public:, for_individual:) }
|
||||
let(:for_individual) { false }
|
||||
let(:types_de_champ_public) do
|
||||
[
|
||||
|
@ -10,335 +9,80 @@ describe ExportTemplate do
|
|||
{ type: :titre_identite, libelle: "CNI", mandatory: true, stable_id: 5 }
|
||||
]
|
||||
end
|
||||
let(:content) do
|
||||
{
|
||||
"pdf_name" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "mon_export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }
|
||||
]
|
||||
},
|
||||
"default_dossier_directory" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }
|
||||
]
|
||||
},
|
||||
"pjs" =>
|
||||
[
|
||||
{ path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] }] }, stable_id: "3" },
|
||||
{
|
||||
path:
|
||||
{ "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "cni_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] },
|
||||
stable_id: "5"
|
||||
},
|
||||
{
|
||||
path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "pj_repet_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] },
|
||||
stable_id: "10"
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
describe 'new' do
|
||||
let(:export_template) { build(:export_template, groupe_instructeur: groupe_instructeur) }
|
||||
describe '.default' do
|
||||
it 'set default values' do
|
||||
export_template.set_default_values
|
||||
expect(export_template.content).to eq({
|
||||
"pdf_name" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }
|
||||
]
|
||||
},
|
||||
"default_dossier_directory" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "dossier-", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }
|
||||
]
|
||||
},
|
||||
"pjs" =>
|
||||
[
|
||||
|
||||
{
|
||||
"stable_id" => "3",
|
||||
"path" => { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "justificatif-de-domicile-", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }] }
|
||||
}
|
||||
]
|
||||
})
|
||||
expect(export_template.export_pdf).to eq(ExportItem.default(prefix: "export", enabled: true))
|
||||
expect(export_template.dossier_folder).to eq(ExportItem.default(prefix: "dossier", enabled: true))
|
||||
expect(export_template.pjs).to eq([ExportItem.default(stable_id: 3, prefix: "justificatif-de-domicile", enabled: false)])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#assign_pj_names' do
|
||||
let(:pj_params) do
|
||||
{
|
||||
"tiptap_pj_1" => {
|
||||
"type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }]
|
||||
}.to_json
|
||||
}
|
||||
describe '#pj' do
|
||||
context 'when pj exists' do
|
||||
subject { export_template.pj(double(stable_id: 3)) }
|
||||
|
||||
it { is_expected.to eq(ExportItem.default(stable_id: 3, prefix: "justificatif-de-domicile", enabled: false)) }
|
||||
end
|
||||
it 'values content from pj params' do
|
||||
export_template.assign_pj_names(pj_params)
|
||||
expect(export_template.content["pjs"]).to eq [
|
||||
{ :path => { "content" => [{ "content" => [{ "text" => "avis-commission-", "type" => "text" }, { "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" }, "type" => "mention" }], "type" => "paragraph" }], "type" => "doc" }, :stable_id => "1" }
|
||||
]
|
||||
|
||||
context 'when pj does not exist' do
|
||||
subject { export_template.pj(TypeDeChamp.new(libelle: 'hi', stable_id: 10)) }
|
||||
|
||||
it { is_expected.to eq(ExportItem.default(stable_id: 10, prefix: "hi", enabled: false)) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tiptap_default_dossier_directory' do
|
||||
it 'returns tiptap_default_dossier_directory from content' do
|
||||
expect(export_template.tiptap_default_dossier_directory).to eq({
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }
|
||||
]
|
||||
}.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tiptap_pdf_name' do
|
||||
it 'returns tiptap_pdf_name from content' do
|
||||
expect(export_template.tiptap_pdf_name).to eq({
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "mon_export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }
|
||||
]
|
||||
}.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#content_for_pj' do
|
||||
let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
|
||||
let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj) }
|
||||
|
||||
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
|
||||
|
||||
it 'returns tiptap content for pj' do
|
||||
expect(export_template.content_for_pj(type_de_champ_pj)).to eq({
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] }
|
||||
]
|
||||
}.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#attachment_and_path' do
|
||||
let(:dossier) { create(:dossier) }
|
||||
describe '#attachment_path' do
|
||||
let(:dossier) { create(:dossier, :en_construction, procedure:) }
|
||||
|
||||
context 'for export pdf' do
|
||||
let(:attachment) { double("attachment") }
|
||||
let(:export_template) do
|
||||
build(:export_template, groupe_instructeur:, dossier_folder: ExportItem.default(prefix: "DOSSIER"), export_pdf: ExportItem.default(prefix: "mon_export"))
|
||||
end
|
||||
|
||||
let(:attachment) { ActiveStorage::Attachment.new(name: 'pdf_export_for_instructeur', blob: ActiveStorage::Blob.new(filename: "export.pdf")) }
|
||||
|
||||
it 'gives absolute filename for export of specific dossier' do
|
||||
allow(attachment).to receive(:name).and_return('pdf_export_for_instructeur')
|
||||
expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/mon_export_#{dossier.id}.pdf"])
|
||||
expect(export_template.attachment_path(dossier, attachment)).to eq("DOSSIER-#{dossier.id}/mon_export-#{dossier.id}.pdf")
|
||||
end
|
||||
end
|
||||
|
||||
context 'for pj' do
|
||||
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
||||
let(:champ_pj) { dossier.champs.find(&:piece_justificative?) }
|
||||
let(:champ_pj) { dossier.champs_public.first }
|
||||
let(:export_template) { create(:export_template, groupe_instructeur:, pjs: [ExportItem.default(stable_id: 3, prefix: "justif", enabled: true)]) }
|
||||
|
||||
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
|
||||
|
||||
it 'returns pj and custom name for pj' do
|
||||
expect(export_template.attachment_and_path(dossier, attachment, champ: champ_pj)).to eq([attachment, "DOSSIER_#{dossier.id}/superpj_justif-1.png"])
|
||||
end
|
||||
end
|
||||
context 'pj repetable' do
|
||||
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:) }
|
||||
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
||||
let(:draft) { procedure.draft_revision }
|
||||
let(:types_de_champ_public) do
|
||||
[
|
||||
{
|
||||
type: :repetition,
|
||||
stable_id: 3333,
|
||||
mandatory: true, children: [
|
||||
{ type: :text, libelle: 'sub type de champ' },
|
||||
{ type: :piece_justificative, stable_id: 10, libelle: 'pj repet' }
|
||||
]
|
||||
}
|
||||
]
|
||||
end
|
||||
let(:champ_pj) { dossier.champs.find(&:piece_justificative?) }
|
||||
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
|
||||
|
||||
it 'rename repetable pj' do
|
||||
expect(export_template.attachment_and_path(dossier, attachment, champ: champ_pj)).to eq([attachment, "DOSSIER_#{dossier.id}/pj_repet_#{dossier.id}-1.png"])
|
||||
expect(export_template.attachment_path(dossier, attachment, champ: champ_pj)).to eq("dossier-#{dossier.id}/justif-#{dossier.id}-01.png")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tiptap_convert' do
|
||||
it 'convert default dossier directory' do
|
||||
expect(export_template.tiptap_convert(procedure.dossiers.first, "default_dossier_directory")).to eq "DOSSIER_#{dossier.id}"
|
||||
end
|
||||
describe '#tags and #pj_tags' do
|
||||
let(:procedure) { build(:procedure, for_individual:) }
|
||||
|
||||
it 'convert pdf_name' do
|
||||
expect(export_template.tiptap_convert(procedure.dossiers.first, "pdf_name")).to eq "mon_export_#{dossier.id}"
|
||||
end
|
||||
|
||||
context 'for date' do
|
||||
let(:export_template) { create(:export_template, :with_date_depot_for_export_pdf, groupe_instructeur:) }
|
||||
let(:dossier) { create(:dossier, :en_construction, procedure:, depose_at: Date.parse("2024/03/30")) }
|
||||
it 'convert date with dash' do
|
||||
expect(export_template.tiptap_convert(dossier, "pdf_name")).to eq "export_#{dossier.id}-2024-03-30"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#tiptap_convert_pj' do
|
||||
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile' }]) }
|
||||
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
||||
let(:champ_pj) { dossier.champs.first }
|
||||
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
|
||||
|
||||
it 'convert pj' do
|
||||
attachment
|
||||
expect(export_template.tiptap_convert_pj(dossier, 3, attachment)).to eq "superpj_justif"
|
||||
end
|
||||
end
|
||||
|
||||
describe '#valid?' do
|
||||
let(:subject) { build(:export_template, groupe_instructeur:, content:) }
|
||||
let(:ddd_text) { "DoSSIER" }
|
||||
let(:mention) { { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } } }
|
||||
let(:ddd_mention) { mention }
|
||||
let(:pdf_text) { "export" }
|
||||
let(:pdf_mention) { mention }
|
||||
let(:pj_text) { "_pj" }
|
||||
let(:pj_mention) { mention }
|
||||
let(:content) do
|
||||
{
|
||||
"pdf_name" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => pdf_text, "type" => "text" }, pdf_mention] }
|
||||
]
|
||||
},
|
||||
"default_dossier_directory" => {
|
||||
"type" => "doc",
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => ddd_text, "type" => "text" }, ddd_mention] }
|
||||
]
|
||||
},
|
||||
"pjs" =>
|
||||
[
|
||||
{ path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [pj_mention, { "text" => pj_text, "type" => "text" }] }] }, stable_id: "3" }
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
context 'with valid default dossier directory' do
|
||||
it 'has no error for default_dossier_directory' do
|
||||
expect(subject.valid?).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context 'with no ddd text' do
|
||||
let(:ddd_text) { " " }
|
||||
context 'with mention' do
|
||||
let(:ddd_mention) { { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } } }
|
||||
it 'has no error for default_dossier_directory' do
|
||||
expect(subject.valid?).to be_truthy
|
||||
end
|
||||
context 'for entreprise procedure' do
|
||||
let(:for_individual) { false }
|
||||
let(:expected_tags) do
|
||||
['entreprise_siren', 'entreprise_numero_tva_intracommunautaire', 'entreprise_siret_siege_social', 'entreprise_raison_sociale', 'entreprise_adresse', 'dossier_depose_at', 'dossier_procedure_libelle', 'dossier_service_name', 'dossier_number', 'dossier_groupe_instructeur']
|
||||
end
|
||||
|
||||
context 'without numéro de dossier' do
|
||||
let(:ddd_mention) { { "type" => "mention", "attrs" => { "id" => 'dossier_service_name', "label" => "nom du service" } } }
|
||||
it "add error for tiptap_default_dossier_directory" do
|
||||
expect(subject.valid?).to be_falsey
|
||||
expect(subject.errors[:tiptap_default_dossier_directory]).to be_present
|
||||
expect(subject.errors.full_messages).to include "Le champ « Nom du répertoire » doit contenir le numéro du dossier"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with valid pdf name' do
|
||||
it 'has no error for pdf name' do
|
||||
expect(subject.valid?).to be_truthy
|
||||
expect(subject.errors[:tiptap_pdf_name]).not_to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context 'with pdf text and without mention' do
|
||||
let(:pdf_text) { "export" }
|
||||
let(:pdf_mention) { { "type" => "mention", "attrs" => {} } }
|
||||
|
||||
it "add no error" do
|
||||
expect(subject.valid?).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context 'with no pdf text' do
|
||||
let(:pdf_text) { " " }
|
||||
|
||||
context 'with mention' do
|
||||
it 'has no error for default_dossier_directory' do
|
||||
expect(subject.valid?).to be_truthy
|
||||
expect(subject.errors[:tiptap_pdf_name]).not_to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context 'without mention' do
|
||||
let(:pdf_mention) { { "type" => "mention", "attrs" => {} } }
|
||||
it "add error for pdf name" do
|
||||
expect(subject.valid?).to be_falsey
|
||||
expect(subject.errors.full_messages).to include "Le champ « Nom du dossier au format pdf » doit être rempli"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with no pj text' do
|
||||
# let!(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
|
||||
let(:pj_text) { " " }
|
||||
|
||||
context 'with mention' do
|
||||
it 'has no error for pj' do
|
||||
expect(subject.valid?).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context 'without mention' do
|
||||
let(:pj_mention) { { "type" => "mention", "attrs" => {} } }
|
||||
it "add error for pj" do
|
||||
expect(subject.valid?).to be_falsey
|
||||
expect(subject.errors.full_messages).to include "Le champ « Justificatif de domicile » doit être rempli"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'for entreprise procedure' do
|
||||
let(:for_individual) { false }
|
||||
describe 'specific_tags' do
|
||||
it do
|
||||
tags = export_template.specific_tags
|
||||
expect(tags.map { _1[:id] }).to eq ["entreprise_siren", "entreprise_numero_tva_intracommunautaire", "entreprise_siret_siege_social", "entreprise_raison_sociale", "entreprise_adresse", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"]
|
||||
expect(export_template.tags.map { _1[:id] }).to eq(expected_tags)
|
||||
expect(export_template.pj_tags.map { _1[:id] }).to eq(expected_tags + ['original-filename'])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'tags_for_pj' do
|
||||
it do
|
||||
tags = export_template.tags_for_pj
|
||||
expect(tags.map { _1[:id] }).to eq ["entreprise_siren", "entreprise_numero_tva_intracommunautaire", "entreprise_siret_siege_social", "entreprise_raison_sociale", "entreprise_adresse", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur", "original-filename"]
|
||||
context 'for individual procedure' do
|
||||
let(:for_individual) { true }
|
||||
let(:expected_tags) do
|
||||
['individual_gender', 'individual_last_name', 'individual_first_name', 'dossier_depose_at', 'dossier_procedure_libelle', 'dossier_service_name', 'dossier_number', 'dossier_groupe_instructeur']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'for individual procedure' do
|
||||
let(:for_individual) { true }
|
||||
describe 'specific_tags' do
|
||||
it do
|
||||
tags = export_template.specific_tags
|
||||
expect(tags.map { _1[:id] }).to eq ["individual_gender", "individual_last_name", "individual_first_name", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"]
|
||||
end
|
||||
end
|
||||
|
||||
describe 'tags_for_pj' do
|
||||
it do
|
||||
tags = export_template.tags_for_pj
|
||||
expect(tags.map { _1[:id] }).to eq ["individual_gender", "individual_last_name", "individual_first_name", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur", "original-filename"]
|
||||
expect(export_template.tags.map { _1[:id] }).to eq(expected_tags)
|
||||
expect(export_template.pj_tags.map { _1[:id] }).to eq(expected_tags + ['original-filename'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -450,7 +450,7 @@ describe ProcedureExportService do
|
|||
context 'with export_template' do
|
||||
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
|
||||
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template:).generate_dossiers_export(Dossier.where(id: dossier)) }
|
||||
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
||||
let(:export_template) { create(:export_template, :enabled_pjs, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
||||
before do
|
||||
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
|
||||
end
|
||||
|
@ -465,10 +465,9 @@ describe ProcedureExportService do
|
|||
structure = [
|
||||
"#{base_fn}/",
|
||||
"#{base_fn}/dossier-#{dossier.id}/",
|
||||
"#{base_fn}/dossier-#{dossier.id}/piece_justificative-#{dossier.id}-1.txt",
|
||||
"#{base_fn}/dossier-#{dossier.id}/export_#{dossier.id}.pdf"
|
||||
"#{base_fn}/dossier-#{dossier.id}/piece_justificative-#{dossier.id}-01.txt",
|
||||
"#{base_fn}/dossier-#{dossier.id}/export-#{dossier.id}.pdf"
|
||||
]
|
||||
expect(files.size).to eq(structure.size)
|
||||
expect(files.map(&:filename)).to match_array(structure)
|
||||
end
|
||||
FileUtils.remove_entry_secure('tmp.zip')
|
||||
|
|
|
@ -2,7 +2,7 @@ describe ProcedureExportService do
|
|||
let(:instructeur) { create(:instructeur) }
|
||||
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, libelle: 'pj' }, { type: :repetition, children: [{ type: :piece_justificative, libelle: 'repet_pj' }] }]) }
|
||||
let(:dossiers) { create_list(:dossier, 10, procedure: procedure) }
|
||||
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
||||
let(:export_template) { create(:export_template, :enabled_pjs, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
||||
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template) }
|
||||
|
||||
def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp')
|
||||
|
@ -49,11 +49,11 @@ describe ProcedureExportService do
|
|||
structure = [
|
||||
"export/",
|
||||
"export/dossier-#{dossier.id}/",
|
||||
"export/dossier-#{dossier.id}/export_#{dossier.id}.pdf",
|
||||
"export/dossier-#{dossier.id}/pj-#{dossier.id}-1.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-1-1.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-2-1.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-1-2.png"
|
||||
"export/dossier-#{dossier.id}/export-#{dossier.id}.pdf",
|
||||
"export/dossier-#{dossier.id}/pj-#{dossier.id}-01.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-01-01.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-02-01.png",
|
||||
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-01-02.png"
|
||||
]
|
||||
|
||||
expect(files.size).to eq(dossiers.count * 6 + 1)
|
||||
|
|
Loading…
Reference in a new issue