diff --git a/app/models/export_template.rb b/app/models/export_template.rb index 30897d081..eaa753415 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -22,6 +22,15 @@ class ExportTemplate < ApplicationRecord tiptap_content("pdf_name") end + def content_for_pj(pj) + content_for_pj_id(pj.stable_id)&.to_json + 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 attachment_and_path(dossier, attachment, index: 0, row_index: nil) [ attachment, diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index 32a3330d5..9a2a29aaf 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -61,6 +61,22 @@ describe ExportTemplate do 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"=>"dossier_number", "label"=>"numéro du dossier"}}, {"text"=>" _justif", "type"=>"text"}]} + ] + }.to_json) + end + end + describe '#attachment_and_path' do let(:dossier) { create(:dossier) } @@ -72,5 +88,51 @@ describe ExportTemplate do expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/mon_export_#{dossier.id}.pdf"]) end end + + context 'for pj' do + let(:dossier) { procedure.dossiers.first } + let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, 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")) } + + before do + dossier.champs_public << champ_pj + end + it 'returns pj and custom name for pj' do + expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/#{dossier.id}_justif.png"]) + end + end + context 'pj repetable' do + let(:procedure) do + create(:procedure_with_dossiers, :for_individual, types_de_champ_public: [{ type: :repetition, mandatory: true, children: [{ libelle: 'sub type de champ' }] }]) + end + let(:type_de_champ_repetition) do + repetition = draft.types_de_champ_public.repetition.first + repetition.update(stable_id: 3333) + repetition + end + let(:draft) { procedure.draft_revision } + let(:dossier) { procedure.dossiers.first } + + let(:type_de_champ_pj) do + draft.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:piece_justificative), + libelle: "pj repet", + stable_id: 10, + parent_stable_id: type_de_champ_repetition.stable_id + }) + end + 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")) } + + before do + dossier.champs_public << champ_pj + end + it 'rename repetable pj' do + expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/pj_repet_#{dossier.id}.png"]) + end + end end end