generate pdf export inside pieces_justificatives_service

This commit is contained in:
Christophe Robillard 2021-03-29 21:45:46 +02:00
parent c25f3c79d9
commit fba0d78153
7 changed files with 41 additions and 34 deletions

View file

@ -213,7 +213,6 @@ module Instructeurs
def telecharger_pjs def telecharger_pjs
return head(:forbidden) if !dossier.attachments_downloadable? return head(:forbidden) if !dossier.attachments_downloadable?
generate_pdf_for_instructeur_export
files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier)
zipline(files, "dossier-#{dossier.id}.zip") zipline(files, "dossier-#{dossier.id}.zip")
@ -239,12 +238,6 @@ module Instructeurs
.find(params[:dossier_id]) .find(params[:dossier_id])
end end
def generate_pdf_for_instructeur_export
@include_infos_administration = true
pdf = render_to_string(template: '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 def commentaire_params
params.require(:commentaire).permit(:body, :piece_jointe) params.require(:commentaire).permit(:body, :piece_jointe)
end end

View file

@ -1,14 +1,12 @@
class ActiveStorage::DownloadableFile class ActiveStorage::DownloadableFile
def self.create_list_from_dossier(dossier) def self.create_list_from_dossier(dossier)
pjs = PiecesJustificativesService.liste_pieces_justificatives(dossier) pjs = PiecesJustificativesService.liste_pieces_justificatives(dossier)
files = pjs.map do |piece_justificative| pjs.map do |piece_justificative|
[ [
piece_justificative, piece_justificative,
self.timestamped_filename(piece_justificative) "dossier-#{dossier.id}/#{self.timestamped_filename(piece_justificative)}"
] ]
end end
files << [dossier.pdf_export_for_instructeur, self.timestamped_filename(dossier.pdf_export_for_instructeur)]
files
end end
private private
@ -22,19 +20,23 @@ class ActiveStorage::DownloadableFile
timestamp = attachment.created_at.strftime("%d-%m-%Y-%H-%M") timestamp = attachment.created_at.strftime("%d-%m-%Y-%H-%M")
id = attachment.id % 10000 id = attachment.id % 10000
"#{folder}/#{basename}-#{timestamp}-#{id}#{extension}" [folder, "#{basename}-#{timestamp}-#{id}#{extension}"].join
end end
def self.folder(attachment) def self.folder(attachment)
if attachment.name == 'pdf_export_for_instructeur'
return ''
end
case attachment.record_type case attachment.record_type
when 'Dossier' when 'Dossier'
'dossier' 'dossier/'
when 'DossierOperationLog', 'BillSignature' when 'DossierOperationLog', 'BillSignature'
'horodatage' 'horodatage/'
when 'Commentaire' when 'Commentaire'
'messagerie' 'messagerie/'
else else
'pieces_justificatives' 'pieces_justificatives/'
end end
end end

View file

@ -1,10 +1,11 @@
class PiecesJustificativesService class PiecesJustificativesService
def self.liste_pieces_justificatives(dossier) def self.liste_pieces_justificatives(dossier)
dossier_export = generate_dossier_export(dossier)
pjs_champs = pjs_for_champs(dossier) pjs_champs = pjs_for_champs(dossier)
pjs_commentaires = pjs_for_commentaires(dossier) pjs_commentaires = pjs_for_commentaires(dossier)
pjs_dossier = pjs_for_dossier(dossier) pjs_dossier = pjs_for_dossier(dossier)
(pjs_champs + pjs_commentaires + pjs_dossier) ([dossier_export] + pjs_champs + pjs_commentaires + pjs_dossier)
.filter(&:attached?) .filter(&:attached?)
end end
@ -43,6 +44,17 @@ class PiecesJustificativesService
private private
def self.generate_dossier_export(dossier)
pdf = ApplicationController
.render(template: 'dossiers/show', formats: [:pdf],
assigns: {
include_infos_administration: true,
dossier: dossier
})
dossier.pdf_export_for_instructeur.attach(io: StringIO.open(pdf), filename: "export-#{dossier.id}.pdf", content_type: 'application/pdf')
dossier.pdf_export_for_instructeur
end
def self.pjs_for_champs(dossier) def self.pjs_for_champs(dossier)
allowed_champs = dossier.champs + dossier.champs_private allowed_champs = dossier.champs + dossier.champs_private

View file

@ -712,13 +712,6 @@ describe Instructeurs::DossiersController, type: :controller do
dossier_id: dossier.id dossier_id: dossier.id
} }
end end
context 'when zip download is disabled through flipflop' do
it 'is forbidden' do
subject
expect(response).to have_http_status(:forbidden)
end
end
end end
describe "#delete_dossier" do describe "#delete_dossier" do

View file

@ -12,6 +12,8 @@ feature 'Inviting an expert:', js: true do
context 'as an Instructeur' do context 'as an Instructeur' do
scenario 'I can invite an expert' do scenario 'I can invite an expert' do
allow(ClamavService).to receive(:safe_file?).and_return(true)
# assign instructeur to linked dossier # assign instructeur to linked dossier
instructeur.assign_to_procedure(linked_dossier.procedure) instructeur.assign_to_procedure(linked_dossier.procedure)

View file

@ -165,10 +165,10 @@ feature 'Instructing a dossier:', js: true do
expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip" expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip"
expect(files.size).to be 3 expect(files.size).to be 3
expect(files[0].filename.include?('piece_justificative_0')).to be_truthy expect(files[0].filename.include?('export')).to be_truthy
expect(files[0].uncompressed_size).to be File.size(path) expect(files[1].filename.include?('piece_justificative_0')).to be_truthy
expect(files[1].filename.include?('horodatage/operation')).to be_truthy expect(files[1].uncompressed_size).to be File.size(path)
expect(files[2].filename.include?('dossier/export')).to be_truthy expect(files[2].filename.include?('horodatage/operation')).to be_truthy
end end
scenario 'A instructeur can download an archive containing several identical attachments' do scenario 'A instructeur can download an archive containing several identical attachments' do
@ -180,13 +180,13 @@ feature 'Instructing a dossier:', js: true do
expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip" expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip"
expect(files.size).to be 4 expect(files.size).to be 4
expect(files[0].filename.include?('piece_justificative_0')).to be_truthy expect(files[0].filename.include?('export')).to be_truthy
expect(files[1].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[2].filename.include?('piece_justificative_0')).to be_truthy
expect(files[0].uncompressed_size).to be File.size(path) expect(files[1].filename).not_to eq files[2].filename
expect(files[1].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[2].uncompressed_size).to be File.size(path)
expect(files[3].filename.include?('dossier/export')).to be_truthy expect(files[3].filename.include?('horodatage/operation')).to be_truthy
end end
before { DownloadHelpers.clear_downloads } before { DownloadHelpers.clear_downloads }

View file

@ -16,7 +16,12 @@ describe PiecesJustificativesService do
# to be exported # to be exported
it 'ensures no titre identite is given' do it 'ensures no titre identite is given' do
expect(champ_identite.piece_justificative_file).to be_attached expect(champ_identite.piece_justificative_file).to be_attached
expect(subject).to eq([]) expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy
end
it 'returns export pdf of the dossier' do
expect(champ_identite.piece_justificative_file).to be_attached
expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_truthy
end end
end end
end end