From 60cc4d3697f37f26025b8496a4f4828739f4929c Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 8 Mar 2021 11:35:20 +0100 Subject: [PATCH] add pdf to pjs export --- .../instructeurs/dossiers_controller.rb | 7 +++++++ app/lib/active_storage/downloadable_file.rb | 4 +++- app/models/dossier.rb | 1 + spec/factories/dossier.rb | 9 +++++++++ spec/features/instructeurs/instruction_spec.rb | 6 ++++-- spec/lib/active_storage/downloadable_file_spec.rb | 15 ++++++++------- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 9ef7a9653..078d37c95 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -210,6 +210,7 @@ module Instructeurs def telecharger_pjs return head(:forbidden) if !dossier.attachments_downloadable? + generate_pdf_for_instructeur_export files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) zipline(files, "dossier-#{dossier.id}.zip") @@ -232,6 +233,12 @@ module Instructeurs @dossier ||= current_instructeur.dossiers.find(params[:dossier_id]) end + def generate_pdf_for_instructeur_export + @include_infos_administration = true + pdf = render_to_string(file: 'dossiers/show', formats: [:pdf]) + dossier.pdf_export_for_instructeur.attach(io: StringIO.open(pdf), filename: "export-#{dossier.id}.pdf", content_type: 'application/pdf') + end + def commentaire_params params.require(:commentaire).permit(:body, :piece_jointe) end diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index 92b86784f..620f1ca0d 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -13,12 +13,14 @@ class ActiveStorage::DownloadableFile def self.create_list_from_dossier(dossier) pjs = PiecesJustificativesService.liste_pieces_justificatives(dossier) - pjs.map do |piece_justificative| + files = pjs.map do |piece_justificative| [ piece_justificative, self.timestamped_filename(piece_justificative) ] end + files << [dossier.pdf_export_for_instructeur, self.timestamped_filename(dossier.pdf_export_for_instructeur)] + files end private diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 368e4ef6e..7c1039b00 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -63,6 +63,7 @@ class Dossier < ApplicationRecord has_one :france_connect_information, through: :user has_one_attached :justificatif_motivation + has_one_attached :pdf_export_for_instructeur has_many :champs, -> { root.public_ordered }, inverse_of: :dossier, dependent: :destroy has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: :dossier, dependent: :destroy diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 29d01759b..50fe313bb 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -201,6 +201,15 @@ FactoryBot.define do end end + trait :with_pdf_export do + after(:create) do |dossier, _evaluator| + dossier.pdf_export_for_instructeur.attach( + io: StringIO.new('Hello World'), + filename: 'export.pdf' + ) + end + end + trait :with_justificatif do after(:create) do |dossier, _evaluator| dossier.justificatif_motivation.attach( diff --git a/spec/features/instructeurs/instruction_spec.rb b/spec/features/instructeurs/instruction_spec.rb index 6d111ba01..ad22c98a0 100644 --- a/spec/features/instructeurs/instruction_spec.rb +++ b/spec/features/instructeurs/instruction_spec.rb @@ -162,10 +162,11 @@ feature 'Instructing a dossier:' do files = ZipTricks::FileReader.read_zip_structure(io: File.open(DownloadHelpers.download)) expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip" - expect(files.size).to be 2 + expect(files.size).to be 3 expect(files[0].filename.include?('piece_justificative_0')).to be_truthy expect(files[0].uncompressed_size).to be File.size(path) expect(files[1].filename.include?('horodatage/operation')).to be_truthy + expect(files[2].filename.include?('dossier/export')).to be_truthy end scenario 'A instructeur can download an archive containing several identical attachments' do @@ -176,13 +177,14 @@ feature 'Instructing a dossier:' do files = ZipTricks::FileReader.read_zip_structure(io: File.open(DownloadHelpers.download)) expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip" - expect(files.size).to be 3 + expect(files.size).to be 4 expect(files[0].filename.include?('piece_justificative_0')).to be_truthy expect(files[1].filename.include?('piece_justificative_0')).to be_truthy expect(files[0].filename).not_to eq files[1].filename expect(files[0].uncompressed_size).to be File.size(path) expect(files[1].uncompressed_size).to be File.size(path) expect(files[2].filename.include?('horodatage/operation')).to be_truthy + expect(files[3].filename.include?('dossier/export')).to be_truthy end before { DownloadHelpers.clear_downloads } diff --git a/spec/lib/active_storage/downloadable_file_spec.rb b/spec/lib/active_storage/downloadable_file_spec.rb index c3675ea3d..6fc938311 100644 --- a/spec/lib/active_storage/downloadable_file_spec.rb +++ b/spec/lib/active_storage/downloadable_file_spec.rb @@ -1,11 +1,12 @@ describe ActiveStorage::DownloadableFile do - let(:dossier) { create(:dossier, :en_construction) } + let(:dossier) { create(:dossier, :en_construction, :with_pdf_export) } subject(:list) { ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) } describe 'create_list_from_dossier' do context 'when no piece_justificative is present' do - it { expect(list).to match([]) } + it { expect(list.length).to eq 1 } + it { expect(list.first[0].record_type).to eq "Dossier" } end context 'when there is a piece_justificative' do @@ -13,7 +14,7 @@ describe ActiveStorage::DownloadableFile do dossier.champs << create(:champ_piece_justificative, :with_piece_justificative_file, dossier: dossier) end - it { expect(list.length).to eq 1 } + it { expect(list.length).to eq 2 } end context 'when there is a private piece_justificative' do @@ -21,7 +22,7 @@ describe ActiveStorage::DownloadableFile do dossier.champs_private << create(:champ_piece_justificative, :with_piece_justificative_file, private: true, dossier: dossier) end - it { expect(list.length).to eq 1 } + it { expect(list.length).to eq 2 } end context 'when there is a repetition bloc' do @@ -30,7 +31,7 @@ describe ActiveStorage::DownloadableFile do end it 'should have 4 piece_justificatives' do - expect(list.size).to eq 4 + expect(list.size).to eq 5 end end @@ -39,7 +40,7 @@ describe ActiveStorage::DownloadableFile do dossier.commentaires << create(:commentaire, dossier: dossier) end - it { expect(list.length).to eq 0 } + it { expect(list.length).to eq 1 } end context 'when there is a message with an attachment' do @@ -47,7 +48,7 @@ describe ActiveStorage::DownloadableFile do dossier.commentaires << create(:commentaire, :with_file, dossier: dossier) end - it { expect(list.length).to eq 1 } + it { expect(list.length).to eq 2 } end end end