From 5846fb4417f33413b0176ccbb258173d7a89882b Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 27 May 2024 15:14:34 +0200 Subject: [PATCH 01/15] fix spec titles --- spec/services/pieces_justificatives_service_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index cef564981..3f750829b 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -323,14 +323,14 @@ describe PiecesJustificativesService do context 'given an administrateur' do let(:user_profile) { build(:administrateur) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end context 'given an instructeur' do let(:user_profile) { create(:instructeur) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end @@ -346,7 +346,7 @@ describe PiecesJustificativesService do let(:experts_procedure) { create(:experts_procedure, expert: user_profile, procedure:) } let(:avis) { create(:avis, experts_procedure:, dossier: dossier, confidentiel: true) } let(:user_profile) { create(:expert) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end @@ -370,21 +370,21 @@ describe PiecesJustificativesService do context 'given an administrateur' do let(:user_profile) { build(:administrateur) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end context 'given an instructeur' do let(:user_profile) { create(:instructeur) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end context 'given an expert' do let(:user_profile) { create(:expert) } - it "doesn't return confidentiel avis.piece_justificative_file" do + it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end end From 06cbb65d4e204542334741b3759d3038eaa72269 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 10:06:30 +0200 Subject: [PATCH 02/15] spec: simplify export template factory --- spec/factories/export_template.rb | 42 ++++++++++++------- spec/models/export_template_spec.rb | 2 +- .../pieces_justificatives_service_spec.rb | 2 +- .../services/procedure_export_service_spec.rb | 2 +- .../procedure_export_service_zip_spec.rb | 2 +- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/spec/factories/export_template.rb b/spec/factories/export_template.rb index 0f4e8d882..54ce224e5 100644 --- a/spec/factories/export_template.rb +++ b/spec/factories/export_template.rb @@ -11,24 +11,36 @@ FactoryBot.define do { "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" } - ] + "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" } + + 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 end end diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index f0602597a..529de503b 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -1,6 +1,6 @@ describe ExportTemplate do let(:groupe_instructeur) { create(:groupe_instructeur, procedure:) } - let(:export_template) { create(:export_template, groupe_instructeur:, content:) } + 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(:for_individual) { false } diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 3f750829b..bdfe28911 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -110,7 +110,7 @@ describe PiecesJustificativesService do it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) } context 'with export_template' do - let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) } + let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) } it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) } end end diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index b463675be..0c03cfd98 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -529,7 +529,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).tap(&:set_default_values) } + let(:export_template) { create(:export_template, 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 diff --git a/spec/services/procedure_export_service_zip_spec.rb b/spec/services/procedure_export_service_zip_spec.rb index 0daced35e..14e5ceaad 100644 --- a/spec/services/procedure_export_service_zip_spec.rb +++ b/spec/services/procedure_export_service_zip_spec.rb @@ -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).tap(&:set_default_values) } + let(:export_template) { create(:export_template, 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') From ce6ebf35896fb0eac6a94c9215758f47d9413204 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 11:06:31 +0200 Subject: [PATCH 03/15] fix dossier directory for commentaire when export with export template --- app/models/export_template.rb | 4 ++-- app/services/pieces_justificatives_service.rb | 7 +++++- spec/factories/export_template.rb | 22 +++++++++++++++++++ .../pieces_justificatives_service_spec.rb | 7 ++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/models/export_template.rb b/app/models/export_template.rb index f5ed164b9..3a9bf9c5c 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -48,7 +48,7 @@ class ExportTemplate < ApplicationRecord def attachment_and_path(dossier, attachment, index: 0, row_index: nil, champ: nil) [ attachment, - path(dossier, attachment, index, row_index, champ) + path(dossier, attachment, index:, row_index:, champ:) ] end @@ -116,7 +116,7 @@ class ExportTemplate < ApplicationRecord "#{render_attributes_for(content["pdf_name"], dossier)}.pdf" end - def path(dossier, attachment, index, row_index, champ) + def path(dossier, attachment, index: 0, row_index: nil, champ: nil) if attachment.name == 'pdf_export_for_instructeur' return export_path(dossier) end diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 0fdbc2be9..8f95adc44 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -169,7 +169,12 @@ class PiecesJustificativesService .filter { |a| safe_attachment(a) } .map do |a| dossier_id = commentaire_id_dossier_id[a.record_id] - ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + if @export_template + dossier = dossiers.find { _1.id == dossier_id } + @export_template.attachment_and_path(dossier, a) + else + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end end end diff --git a/spec/factories/export_template.rb b/spec/factories/export_template.rb index 54ce224e5..d62754115 100644 --- a/spec/factories/export_template.rb +++ b/spec/factories/export_template.rb @@ -42,5 +42,27 @@ FactoryBot.define do 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 end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index bdfe28911..9d6e69fd1 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -164,6 +164,13 @@ describe PiecesJustificativesService do end it { expect(subject).to match_array(dossier.commentaires.first.piece_jointe.attachments) } + + context 'with export_template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + it 'uses specific name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/messagerie")).to be true + end + end end context 'with a pj not safe on a commentaire' do From 08c079ca0b33497c656899921a7a400eb8e276ce Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 11:10:54 +0200 Subject: [PATCH 04/15] fix dossier directory for avis when export with export template --- app/services/pieces_justificatives_service.rb | 7 ++++++- spec/services/pieces_justificatives_service_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 8f95adc44..64807d6b5 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -246,7 +246,12 @@ class PiecesJustificativesService .filter { |a| safe_attachment(a) } .map do |a| dossier_id = avis_ids_dossier_id[a.record_id] - ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + if @export_template + dossier = dossiers.find { _1.id == dossier_id } + @export_template.attachment_and_path(dossier, a) + else + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 9d6e69fd1..770d67e02 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -387,6 +387,13 @@ describe PiecesJustificativesService do it "return confidentiel avis.piece_justificative_file" do expect(subject.size).to eq(2) end + + context 'with export_template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + it 'uses specific name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/avis")).to be true + end + end end context 'given an expert' do From 4232cc98c75046e9e7171dab241541df74cac855 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 11:28:13 +0200 Subject: [PATCH 05/15] fix dossier directory for motivation when export with export template --- app/services/pieces_justificatives_service.rb | 7 ++++++- spec/services/pieces_justificatives_service_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 64807d6b5..75210b024 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -206,7 +206,12 @@ class PiecesJustificativesService .filter { |a| safe_attachment(a) } .map do |a| dossier_id = a.record_id - ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + if @export_template + dossier = dossiers.find { _1.id == dossier_id } + @export_template.attachment_and_path(dossier, a) + else + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 770d67e02..f0a6a094a 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -187,6 +187,13 @@ describe PiecesJustificativesService do let!(:witness) { create(:dossier, :with_justificatif) } it { expect(subject).to match_array(dossier.justificatif_motivation.attachment) } + + context 'with export_template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + it 'uses specific name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/dossier")).to be true + end + end end context 'with a motivation not safe' do From 2267ec98cf8e154a15b7c9a523908635aef5a60e Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 11:35:49 +0200 Subject: [PATCH 06/15] fix dossier directory for attestation when export with export template --- app/models/export_template.rb | 2 ++ app/services/pieces_justificatives_service.rb | 7 ++++++- spec/services/pieces_justificatives_service_spec.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/models/export_template.rb b/app/models/export_template.rb index 3a9bf9c5c..6555af9c9 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -128,6 +128,8 @@ class ExportTemplate < ApplicationRecord 'messagerie' when 'Avis' 'avis' + when 'Attestation' + 'pieces_justificatives' else # for attachment return attachment_path(dossier, attachment, index, row_index, champ) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 75210b024..871aa6f68 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -227,7 +227,12 @@ class PiecesJustificativesService .where(record_type: "Attestation", record_id: attestation_id_dossier_id.keys) .map do |a| dossier_id = attestation_id_dossier_id[a.record_id] - ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + if @export_template + dossier = dossiers.find { _1.id == dossier_id } + @export_template.attachment_and_path(dossier, a) + else + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index f0a6a094a..c817e9047 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -209,6 +209,16 @@ describe PiecesJustificativesService do let!(:witness) { create(:dossier, :with_attestation) } it { expect(subject).to match_array(dossier.attestation.pdf.attachment) } + it 'uses default name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template: nil).liste_documents(dossiers).map(&:second)[0].starts_with?("dossier-#{dossier.id}/pieces_justificatives")).to be true + end + + context 'with export_template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + it 'uses specific name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/pieces_justificatives")).to be true + end + end end context 'with an etablissement' do From d9f7b6d1df894d755c76131234bafc270e282ffa Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 11:40:25 +0200 Subject: [PATCH 07/15] fix dossier directory for etablissement when export with export template --- app/models/export_template.rb | 2 +- app/services/pieces_justificatives_service.rb | 7 ++++++- spec/services/pieces_justificatives_service_spec.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/export_template.rb b/app/models/export_template.rb index 6555af9c9..febadd05a 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -128,7 +128,7 @@ class ExportTemplate < ApplicationRecord 'messagerie' when 'Avis' 'avis' - when 'Attestation' + when 'Attestation', 'Etablissement' 'pieces_justificatives' else # for attachment diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 871aa6f68..92d53b088 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -195,7 +195,12 @@ class PiecesJustificativesService .where(record_type: "Etablissement", record_id: etablissement_id_dossier_id.keys) .map do |a| dossier_id = etablissement_id_dossier_id[a.record_id] - ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + if @export_template + dossier = dossiers.find { _1.id == dossier_id } + @export_template.attachment_and_path(dossier, a) + else + ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) + end end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index c817e9047..0057b0b0f 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -236,6 +236,17 @@ describe PiecesJustificativesService do end it { expect(subject).to match_array([attestation_sociale.attachment, attestation_fiscale.attachment]) } + + it 'uses default name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template: nil).liste_documents(dossiers).map(&:second)[0].starts_with?("dossier-#{dossier.id}/pieces_justificatives")).to be true + end + + context 'with export_template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + it 'uses specific name for dossier directory' do + expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/pieces_justificatives")).to be true + end + end end end From bc4deb1fc2d047ff5e18547822c50626648aadf8 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Fri, 24 May 2024 14:55:34 +0200 Subject: [PATCH 08/15] add specs to export templates controller --- .../export_templates_controller_spec.rb | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/spec/controllers/instructeurs/export_templates_controller_spec.rb b/spec/controllers/instructeurs/export_templates_controller_spec.rb index 8b8d73b82..36c9f665e 100644 --- a/spec/controllers/instructeurs/export_templates_controller_spec.rb +++ b/spec/controllers/instructeurs/export_templates_controller_spec.rb @@ -21,26 +21,45 @@ describe Instructeurs::ExportTemplatesController, type: :controller do { "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] } ] }.to_json, - "pjs" => - [ - { path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "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" - } - ] + tiptap_pj_3: { + "type" => "doc", + "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }] + }.to_json, + tiptap_pj_5: { + + "type" => "doc", + "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }] + }.to_json, + tiptap_pj_10: { + + "type" => "doc", + "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }] + }.to_json } end let(:instructeur) { create(:instructeur) } - let(:procedure) { create(:procedure, instructeurs: [instructeur]) } + let(:procedure) do + create( + :procedure, instructeurs: [instructeur], + types_de_champ_public: [ + { type: :piece_justificative, libelle: "pj1", stable_id: 3 }, + { type: :piece_justificative, libelle: "pj2", stable_id: 5 }, + { type: :piece_justificative, libelle: "pj3", stable_id: 10 } + ] + ) + end let(:groupe_instructeur) { procedure.defaut_groupe_instructeur } + describe '#new' do + let(:subject) { get :new, params: { procedure_id: procedure.id } } + + it do + subject + expect(assigns(:export_template)).to be_present + end + end + describe '#create' do let(:subject) { post :create, params: { procedure_id: procedure.id, export_template: export_template_params } } @@ -130,4 +149,19 @@ describe Instructeurs::ExportTemplatesController, type: :controller do end end end + + describe '#preview' do + render_views + + let(:export_template) { create(:export_template, groupe_instructeur:) } + + let(:subject) { get :preview, params: { procedure_id: procedure.id, id: export_template.id, export_template: export_template_params }, format: :turbo_stream } + + it '' do + dossier = create(:dossier, procedure: procedure, for_procedure_preview: true) + subject + expect(response.body).to include "DOSSIER_#{dossier.id}" + expect(response.body).to include "mon_export_#{dossier.id}.pdf" + end + end end From c0a95ab5256d9d05a2e1144c9ccc8d077fc51655 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 18:24:45 +0200 Subject: [PATCH 09/15] add specs to pieces_justificatives_service --- .../pieces_justificatives_service_spec.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 0057b0b0f..341cb33ad 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -439,7 +439,8 @@ describe PiecesJustificativesService do let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :piece_justificative }] }]) } let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) } let(:dossiers) { Dossier.where(id: dossier.id) } - subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) } + let(:export_template) { nil } + subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) } it "doesn't update dossier" do expect { subject }.not_to change { dossier.updated_at } @@ -451,11 +452,24 @@ describe PiecesJustificativesService do let!(:not_confidentiel_avis) { create(:avis, :not_confidentiel, dossier: dossier) } let!(:expert_avis) { create(:avis, :confidentiel, dossier: dossier, expert: user_profile) } - subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) } + subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) } it "includes avis not confidentiel as well as expert's avis" do expect_any_instance_of(Dossier).to receive(:avis_for_expert).with(user_profile).and_return([]) subject end + + it 'gives default name to export pdf file' do + expect(subject.first.second.starts_with?("dossier-#{dossier.id}/export-#{dossier.id}")).to eq true + end + end + + context 'with export template' do + let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) } + subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) } + + it 'gives custom name to export pdf file' do + expect(subject.first.second).to eq "DOSSIER-#{dossier.id}/export_#{dossier.id}.pdf" + end end end From d61203e57ceb03dfa47f51772c0cfc05f03b31d9 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 28 May 2024 19:14:57 +0200 Subject: [PATCH 10/15] remove dead code --- app/models/champ.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 74b5fd1e9..2cb0bb4ec 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -91,10 +91,6 @@ class Champ < ApplicationRecord parent_id.present? end - def stable_id_with_row - [row_id, stable_id].compact - end - # used for the `required` html attribute # check visibility to avoid hidden required input # which prevent the form from being sent. From acf6579aa4b45a07ca63f38f4de01d811c351a04 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 29 May 2024 11:08:46 +0200 Subject: [PATCH 11/15] add missing specs to export template --- spec/models/export_template_spec.rb | 42 +++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index 529de503b..443959bc1 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -69,6 +69,22 @@ describe ExportTemplate do 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 + } + 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" } + ] + 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({ @@ -297,21 +313,37 @@ describe ExportTemplate do end end - describe 'specific_tags' do - context 'for entreprise procedure' do - let(:for_individual) { false } + 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"] end end - context 'for individual procedure' do - let(:for_individual) { true } + 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"] + 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"] + end + end end end From 0ed166f51008d2b03989238fb406b5a1a3eccedb Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 29 May 2024 14:30:20 +0200 Subject: [PATCH 12/15] export_template feature flag scoped by procedure --- .../export_dropdown_component.html.haml | 16 ++++++++-------- .../instructeurs/procedures/exports.html.haml | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/components/dossiers/export_dropdown_component/export_dropdown_component.html.haml b/app/components/dossiers/export_dropdown_component/export_dropdown_component.html.haml index fbb499483..fd29214f2 100644 --- a/app/components/dossiers/export_dropdown_component/export_dropdown_component.html.haml +++ b/app/components/dossiers/export_dropdown_component/export_dropdown_component.html.haml @@ -15,12 +15,12 @@ = link_to download_export_path(export_format: format), role: 'menuitem', data: { turbo_method: :post, turbo: true } do = t(".everything_#{format}_html") - - if export_templates.present? - - export_templates.each do |export_template| + - if @procedure.feature_enabled?(:export_template) + - if export_templates.present? + - export_templates.each do |export_template| + - menu.with_item do + = link_to download_export_path(export_template_id: export_template.id), role: 'menuitem', data: { turbo_method: :post, turbo: true } do + = "Exporter à partir du modèle #{export_template.name}" - menu.with_item do - = link_to download_export_path(export_template_id: export_template.id), role: 'menuitem', data: { turbo_method: :post, turbo: true } do - = "Exporter à partir du modèle #{export_template.name}" - - if feature_enabled?(:export_template) - - menu.with_item do - = link_to new_instructeur_export_template_path(procedure_id: params[:procedure_id]), role: 'menuitem' do - Ajouter un modèle d'export + = link_to new_instructeur_export_template_path(procedure_id: params[:procedure_id]), role: 'menuitem' do + Ajouter un modèle d'export diff --git a/app/views/instructeurs/procedures/exports.html.haml b/app/views/instructeurs/procedures/exports.html.haml index 0986a977a..793a7d960 100644 --- a/app/views/instructeurs/procedures/exports.html.haml +++ b/app/views/instructeurs/procedures/exports.html.haml @@ -23,7 +23,7 @@ - else = t('.no_export_html', expiration_time: Export::MAX_DUREE_CONSERVATION_EXPORT.in_hours.to_i ) - - if feature_enabled?(:export_template) + - if @procedure.feature_enabled?(:export_template) %h2.fr-mb-1w.fr-mt-8w Liste des modèles d'export %p.fr-hint-text From fc90648c79e618f914f2f83989ddf411c1db12cf Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 29 May 2024 15:42:49 +0200 Subject: [PATCH 13/15] fix: regenerate export from export template --- app/components/dossiers/export_link_component.rb | 3 ++- .../export_link_component/export_link_component.html.haml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/components/dossiers/export_link_component.rb b/app/components/dossiers/export_link_component.rb index 0fe2967aa..647d08a4b 100644 --- a/app/components/dossiers/export_link_component.rb +++ b/app/components/dossiers/export_link_component.rb @@ -11,9 +11,10 @@ class Dossiers::ExportLinkComponent < ApplicationComponent @export_url = export_url end - def download_export_path(export_format:, statut:, no_progress_notification: nil) + def download_export_path(export_format:, statut:, export_template_id: nil, no_progress_notification: nil) @export_url.call(@procedure, export_format: export_format, + export_template_id:, statut: statut, no_progress_notification: no_progress_notification) end diff --git a/app/components/dossiers/export_link_component/export_link_component.html.haml b/app/components/dossiers/export_link_component/export_link_component.html.haml index 0ed8d34a2..6f217d5ae 100644 --- a/app/components/dossiers/export_link_component/export_link_component.html.haml +++ b/app/components/dossiers/export_link_component/export_link_component.html.haml @@ -14,4 +14,4 @@ = export_button(export) - if export.failed? - = button_to refresh_button_options(export)[:title], download_export_path(export_format: export.format, statut: export.statut), refresh_button_options(export) + = button_to refresh_button_options(export)[:title], download_export_path(export_template_id: export.export_template&.id, export_format: export.format, statut: export.statut), refresh_button_options(export) From 9f504dbefd7cdc5adfd349b4075ff6b5f67261d8 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 29 May 2024 16:09:13 +0200 Subject: [PATCH 14/15] precise export template source for zip exports --- .../export_link_component/export_link_component.html.haml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/components/dossiers/export_link_component/export_link_component.html.haml b/app/components/dossiers/export_link_component/export_link_component.html.haml index 6f217d5ae..32d631e33 100644 --- a/app/components/dossiers/export_link_component/export_link_component.html.haml +++ b/app/components/dossiers/export_link_component/export_link_component.html.haml @@ -7,6 +7,9 @@ = export_title(export) %span.fr-text-mention--grey.fr-mb-1w = time_info(export) + - if export.export_template + %span.fr-tag.fr-tag--sm.fr-ml-1w + = export.export_template.name .fr-ml-auto = badge(export) From ac23d5fb4138885187f44b16c2dc1c96a9d2fed2 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 4 Jun 2024 10:19:15 +0200 Subject: [PATCH 15/15] convert date with dash for export renaming --- .../concerns/tags_substitution_concern.rb | 3 ++- app/models/export_template.rb | 1 + spec/factories/export_template.rb | 20 +++++++++++++++++++ spec/models/export_template_spec.rb | 8 ++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index 373ad3018..ae899dc04 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -307,7 +307,8 @@ module TagsSubstitutionConcern def format_date(date) if date.present? - date.strftime('%d/%m/%Y') + format = defined?(self.class::FORMAT_DATE) ? self.class::FORMAT_DATE : '%d/%m/%Y' + date.strftime(format) else '' end diff --git a/app/models/export_template.rb b/app/models/export_template.rb index febadd05a..550bb57cd 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -7,6 +7,7 @@ class ExportTemplate < ApplicationRecord 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-") diff --git a/spec/factories/export_template.rb b/spec/factories/export_template.rb index d62754115..6785356af 100644 --- a/spec/factories/export_template.rb +++ b/spec/factories/export_template.rb @@ -64,5 +64,25 @@ FactoryBot.define do 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 + end + end end end diff --git a/spec/models/export_template_spec.rb b/spec/models/export_template_spec.rb index 443959bc1..d90de6744 100644 --- a/spec/models/export_template_spec.rb +++ b/spec/models/export_template_spec.rb @@ -190,6 +190,14 @@ describe ExportTemplate do 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