diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 6b10ecbeb..1298897e5 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -2,17 +2,17 @@ class PiecesJustificativesService def self.upload!(dossier, user, params) tpj_contents = dossier.types_de_piece_justificative .map { |tpj| [tpj, params["piece_justificative_#{tpj.id}"]] } - .select { |_, content| content } + .select { |_, content| content.present? } without_virus, with_virus = tpj_contents .partition { |_, content| ClamavService.safe_file?(content.path) } errors = with_virus - .map { |_, content| content.original_filename + ': Virus détecté !!' } + .map { |_, content| content.original_filename + ' : virus détecté' } errors += without_virus .map { |tpj, content| save_pj(content, dossier, tpj, user) } - .reject(&:empty?) + .compact() errors += missing_pj_error_messages(dossier) end @@ -40,7 +40,7 @@ class PiecesJustificativesService type_de_piece_justificative: tpj, user: user) - pj.save ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé" + pj.save ? nil : "le fichier #{content.original_filename} (#{pj.libelle}) n'a pas pu être sauvegardé" end def self.missing_pj_error_messages(dossier) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 922befc4e..d6ac03c87 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -31,11 +31,11 @@ describe PiecesJustificativesService do let(:hash) do { "piece_justificative_#{tpj_not_mandatory.id}" => - double(path: '', original_filename: 'file') + double(path: '', original_filename: 'filename') } end - it { expect(errors).to match(["le fichier not mandatory n'a pas pu être sauvegardé"]) } + it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) } end context 'when a virus is provided' do @@ -47,7 +47,7 @@ describe PiecesJustificativesService do } end - it { expect(errors).to match(['bad_file: Virus détecté !!']) } + it { expect(errors).to match(['bad_file : virus détecté']) } end end @@ -63,7 +63,7 @@ describe PiecesJustificativesService do # we are messing around piece_justificative # because directly doubling carrierwave params seems complicated - allow(PiecesJustificativesService).to receive(:save_pj).and_return('') + allow(PiecesJustificativesService).to receive(:save_pj).and_return(nil) piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory) expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double]) end