demarches-normaliennes/app/services/pieces_justificatives_service.rb

52 lines
1.7 KiB
Ruby
Raw Normal View History

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.present? }
without_virus, with_virus = tpj_contents
.partition { |_, content| ClamavService.safe_file?(content.path) }
errors = with_virus
2018-01-15 21:41:16 +01:00
.map { |_, content| "#{content.original_filename} : virus détecté" }
errors += without_virus
.map { |tpj, content| save_pj(content, dossier, tpj, user) }
.compact()
end
def self.upload_one!(dossier, user, params)
content = params[:piece_justificative][:content]
2018-12-18 21:29:31 +01:00
if ClamavService.safe_file?(content.path)
pj = PieceJustificative.new(content: content,
dossier: dossier,
type_de_piece_justificative: nil,
user: user)
pj.save
else
pj = PieceJustificative.new
2018-01-15 21:41:16 +01:00
pj.errors.add(:content, "#{content.original_filename} : <b>Virus détecté !!</b>")
end
pj
end
def self.save_pj(content, dossier, tpj, user)
pj = PieceJustificative.new(content: content,
dossier: dossier,
type_de_piece_justificative: tpj,
user: user)
pj.save ? nil : "le fichier #{content.original_filename} (#{pj.libelle.truncate(200)}) n'a pas pu être sauvegardé"
end
def self.missing_pj_error_messages(dossier)
mandatory_pjs = dossier.types_de_piece_justificative.select(&:mandatory)
present_pjs = dossier.pieces_justificatives.map(&:type_de_piece_justificative)
missing_pjs = mandatory_pjs - present_pjs
missing_pjs.map { |pj| "La pièce jointe #{pj.libelle.truncate(200)} doit être fournie." }
end
2017-04-04 15:27:04 +02:00
end