2016-03-22 17:36:36 +01:00
|
|
|
class PiecesJustificativesService
|
2017-03-29 21:42:52 +02:00
|
|
|
def self.upload!(dossier, user, params)
|
|
|
|
tpj_contents = dossier.types_de_piece_justificative
|
2018-01-15 19:34:08 +01:00
|
|
|
.map { |tpj| [tpj, params["piece_justificative_#{tpj.id}"]] }
|
|
|
|
.select { |_, content| content.present? }
|
2017-03-29 21:42:52 +02:00
|
|
|
|
|
|
|
without_virus, with_virus = tpj_contents
|
2018-01-15 19:34:08 +01:00
|
|
|
.partition { |_, content| ClamavService.safe_file?(content.path) }
|
2017-03-29 21:42:52 +02:00
|
|
|
|
|
|
|
errors = with_virus
|
2018-01-15 19:34:08 +01:00
|
|
|
.map { |_, content| content.original_filename + ' : virus détecté' }
|
2017-03-29 21:42:52 +02:00
|
|
|
|
|
|
|
errors += without_virus
|
2018-01-15 19:34:08 +01:00
|
|
|
.map { |tpj, content| save_pj(content, dossier, tpj, user) }
|
|
|
|
.compact()
|
2016-03-22 17:36:36 +01:00
|
|
|
end
|
2016-04-20 16:51:57 +02:00
|
|
|
|
|
|
|
def self.upload_one! dossier, user, params
|
2017-03-29 21:58:26 +02:00
|
|
|
content = params[:piece_justificative][:content]
|
|
|
|
if ClamavService.safe_file? content.path
|
|
|
|
pj = PieceJustificative.new(content: content,
|
2018-01-15 19:34:08 +01:00
|
|
|
dossier: dossier,
|
|
|
|
type_de_piece_justificative: nil,
|
|
|
|
user: user)
|
2017-03-29 21:58:26 +02:00
|
|
|
|
|
|
|
pj.save
|
2016-04-20 16:51:57 +02:00
|
|
|
else
|
2017-03-29 21:58:26 +02:00
|
|
|
pj = PieceJustificative.new
|
|
|
|
pj.errors.add(:content, content.original_filename + ': <b>Virus détecté !!</b>')
|
2016-04-20 16:51:57 +02:00
|
|
|
end
|
|
|
|
|
2017-03-29 21:58:26 +02:00
|
|
|
pj
|
2016-04-20 16:51:57 +02:00
|
|
|
end
|
2017-03-29 21:42:52 +02:00
|
|
|
|
|
|
|
def self.save_pj(content, dossier, tpj, user)
|
|
|
|
pj = PieceJustificative.new(content: content,
|
2018-01-15 19:34:08 +01:00
|
|
|
dossier: dossier,
|
|
|
|
type_de_piece_justificative: tpj,
|
|
|
|
user: user)
|
2017-03-29 21:42:52 +02:00
|
|
|
|
2017-10-11 17:32:45 +02:00
|
|
|
pj.save ? nil : "le fichier #{content.original_filename} (#{pj.libelle.truncate(200)}) n'a pas pu être sauvegardé"
|
2017-03-29 21:42:52 +02:00
|
|
|
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
|
|
|
|
|
2017-10-11 17:32:45 +02:00
|
|
|
missing_pjs.map { |pj| "La pièce jointe #{pj.libelle.truncate(200)} doit être fournie." }
|
2017-03-29 21:42:52 +02:00
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|