Merge pull request #6165 from betagouv/optim_vue_dossier_instructeur

prend en compte uniquement les pj pour estimer la taille d'un dossier
This commit is contained in:
krichtof 2021-05-04 12:59:31 +02:00 committed by GitHub
commit 4cba6e3b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 37 deletions

View file

@ -211,7 +211,7 @@ module Instructeurs
end end
def telecharger_pjs def telecharger_pjs
return head(:forbidden) if !dossier.attachments_downloadable? return head(:forbidden) if !dossier.export_and_attachments_downloadable?
files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier)

View file

@ -1,7 +1,7 @@
class ActiveStorage::DownloadableFile class ActiveStorage::DownloadableFile
def self.create_list_from_dossier(dossier) def self.create_list_from_dossier(dossier)
dossier_export = PiecesJustificativesService.generate_dossier_export(dossier) dossier_export = PiecesJustificativesService.generate_dossier_export(dossier)
pjs = [dossier_export] + PiecesJustificativesService.liste_pieces_justificatives(dossier) pjs = [dossier_export] + PiecesJustificativesService.liste_documents(dossier)
pjs.map do |piece_justificative| pjs.map do |piece_justificative|
[ [
piece_justificative, piece_justificative,

View file

@ -873,9 +873,8 @@ class Dossier < ApplicationRecord
end end
end end
def attachments_downloadable? def export_and_attachments_downloadable?
PiecesJustificativesService.liste_pieces_justificatives(self).present? \ PiecesJustificativesService.pieces_justificatives_total_size(self) < Dossier::TAILLE_MAX_ZIP
&& PiecesJustificativesService.pieces_justificatives_total_size(self) < Dossier::TAILLE_MAX_ZIP
end end
def linked_dossiers_for(instructeur_or_expert) def linked_dossiers_for(instructeur_or_expert)

View file

@ -1,5 +1,5 @@
class PiecesJustificativesService class PiecesJustificativesService
def self.liste_pieces_justificatives(dossier) def self.liste_documents(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)
@ -8,6 +8,14 @@ class PiecesJustificativesService
.filter(&:attached?) .filter(&:attached?)
end end
def self.liste_pieces_justificatives(dossier)
pjs_champs = pjs_for_champs(dossier)
pjs_commentaires = pjs_for_commentaires(dossier)
(pjs_champs + pjs_commentaires)
.filter(&:attached?)
end
def self.pieces_justificatives_total_size(dossier) def self.pieces_justificatives_total_size(dossier)
liste_pieces_justificatives(dossier) liste_pieces_justificatives(dossier)
.sum(&:byte_size) .sum(&:byte_size)

View file

@ -12,16 +12,15 @@
%li %li
= link_to "Export GeoJSON", geo_data_instructeur_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link" = link_to "Export GeoJSON", geo_data_instructeur_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link"
- if PiecesJustificativesService.liste_pieces_justificatives(dossier).present? %span.dropdown.print-menu-opener
%span.dropdown.print-menu-opener %button.button.dropdown-button.icon-only{ 'aria-expanded' => 'false', 'aria-controls' => 'print-pj-menu' }
%button.button.dropdown-button.icon-only{ 'aria-expanded' => 'false', 'aria-controls' => 'print-pj-menu' } %span.icon.attached
%span.icon.attached %ul#print-pj-menu.print-menu.dropdown-content
%ul#print-pj-menu.print-menu.dropdown-content %li
%li - if dossier.export_and_attachments_downloadable?
- if PiecesJustificativesService.pieces_justificatives_total_size(dossier) < Dossier::TAILLE_MAX_ZIP = link_to "Télécharger le dossier et toutes ses pièces jointes", telecharger_pjs_instructeur_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link"
= link_to "Télécharger le dossier et toutes ses pièces jointes", telecharger_pjs_instructeur_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link" - else
- else %p.menu-item Le téléchargement des pièces jointes est désactivé pour les dossiers de plus de #{number_to_human_size Dossier::TAILLE_MAX_ZIP}.
%p.menu-item Le téléchargement des pièces jointes est désactivé pour les dossiers de plus de #{number_to_human_size Dossier::TAILLE_MAX_ZIP}.
= render partial: "instructeurs/procedures/dossier_actions", = render partial: "instructeurs/procedures/dossier_actions",
locals: { procedure_id: dossier.procedure.id, locals: { procedure_id: dossier.procedure.id,

View file

@ -1135,30 +1135,26 @@ describe Dossier do
after { Timecop.return } after { Timecop.return }
end end
describe '#attachments_downloadable?' do describe '#export_and_attachments_downloadable?' do
let(:dossier) { create(:dossier, user: user) } let(:dossier) { create(:dossier, user: user) }
# subject { dossier.attachments_downloadable? }
context "no attachments" do context "no attachments" do
it { it {
expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([]) expect(dossier.export_and_attachments_downloadable?).to be true
expect(dossier.attachments_downloadable?).to be false
} }
end end
context "with a small attachment" do context "with a small attachment" do
it { it {
expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([Champ.new])
expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(4.megabytes) expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(4.megabytes)
expect(dossier.attachments_downloadable?).to be true expect(dossier.export_and_attachments_downloadable?).to be true
} }
end end
context "with a too large attachment" do context "with a too large attachment" do
it { it {
expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([Champ.new])
expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(100.megabytes) expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(100.megabytes)
expect(dossier.attachments_downloadable?).to be false expect(dossier.export_and_attachments_downloadable?).to be false
} }
end end
end end

View file

@ -1,20 +1,24 @@
describe PiecesJustificativesService do describe PiecesJustificativesService do
let(:procedure) { create(:procedure, :with_titre_identite) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:champ_identite) { dossier.champs.find { |c| c.type == 'Champs::TitreIdentiteChamp' } }
let(:bill_signature) do
bs = build(:bill_signature, :with_serialized, :with_signature)
bs.save(validate: false)
bs
end
before do
champ_identite
.piece_justificative_file
.attach(io: StringIO.new("toto"), filename: "toto.png", content_type: "image/png")
create(:dossier_operation_log, dossier: dossier, bill_signature: bill_signature)
end
describe '.liste_pieces_justificatives' do describe '.liste_pieces_justificatives' do
let(:procedure) { create(:procedure, :with_titre_identite) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:champ_identite) { dossier.champs.find { |c| c.type == 'Champs::TitreIdentiteChamp' } }
before do
champ_identite
.piece_justificative_file
.attach(io: StringIO.new("toto"), filename: "toto.png", content_type: "image/png")
end
subject { PiecesJustificativesService.liste_pieces_justificatives(dossier) } subject { PiecesJustificativesService.liste_pieces_justificatives(dossier) }
# titre identite is too sensitive it "doesn't return sensitive documents like titre_identite" do
# to be exported
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.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy
end end
@ -22,5 +26,22 @@ describe PiecesJustificativesService do
it "doesn't return export pdf of the dossier" do it "doesn't return export pdf of the dossier" do
expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_falsy expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_falsy
end end
it "doesn't return operation logs of the dossier" do
expect(subject.any? { |piece| piece.name == 'serialized' }).to be_falsy
end
end
describe '.liste_documents' do
subject { PiecesJustificativesService.liste_documents(dossier) }
it "doesn't return sensitive documents like titre_identite" do
expect(champ_identite.piece_justificative_file).to be_attached
expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy
end
it "returns operation logs of the dossier" do
expect(subject.any? { |piece| piece.name == 'serialized' }).to be_truthy
end
end end
end end