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 21:41:16 +01:00
|
|
|
|
.map { |_, content| "#{content.original_filename} : virus détecté" }
|
2017-03-29 21:42:52 +02:00
|
|
|
|
|
2018-12-24 18:00:32 +01: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
|
|
|
|
|
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
|
2019-01-10 13:34:47 +01:00
|
|
|
|
|
|
|
|
|
def self.types_pj_as_types_de_champ(procedure)
|
2019-05-28 17:20:07 +02:00
|
|
|
|
max_order_place = procedure.types_de_champ.pluck(:order_place).compact.max || -1
|
|
|
|
|
order_place = max_order_place + 1
|
2019-03-26 11:21:37 +01:00
|
|
|
|
|
2019-01-10 13:34:47 +01:00
|
|
|
|
types_de_champ = [
|
|
|
|
|
TypeDeChamp.new(
|
|
|
|
|
libelle: "Pièces jointes",
|
|
|
|
|
type_champ: TypeDeChamp.type_champs.fetch(:header_section),
|
|
|
|
|
order_place: order_place
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
types_de_champ += procedure.types_de_piece_justificative.map do |tpj|
|
|
|
|
|
order_place += 1
|
|
|
|
|
description = tpj.description
|
|
|
|
|
if tpj.lien_demarche.present?
|
|
|
|
|
if description.present?
|
|
|
|
|
description += "\n"
|
|
|
|
|
end
|
|
|
|
|
description += "Récupérer le formulaire vierge pour mon dossier : #{tpj.lien_demarche}"
|
|
|
|
|
end
|
|
|
|
|
TypeDeChamp.new(
|
|
|
|
|
libelle: tpj.libelle,
|
|
|
|
|
type_champ: TypeDeChamp.type_champs.fetch(:piece_justificative),
|
|
|
|
|
description: description,
|
|
|
|
|
order_place: order_place,
|
2019-01-10 13:36:38 +01:00
|
|
|
|
mandatory: tpj.mandatory,
|
|
|
|
|
old_pj: {
|
|
|
|
|
stable_id: tpj.id
|
|
|
|
|
}
|
2019-01-10 13:34:47 +01:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
if types_de_champ.count > 1
|
|
|
|
|
types_de_champ
|
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-01-10 15:55:04 +01:00
|
|
|
|
|
|
|
|
|
def self.serialize_types_de_champ_as_type_pj(procedure)
|
|
|
|
|
tdcs = procedure.types_de_champ.select { |type_champ| type_champ.old_pj.present? }
|
|
|
|
|
tdcs.map.with_index do |type_champ, order_place|
|
|
|
|
|
description = type_champ.description
|
|
|
|
|
if /^(?<original_description>.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?<lien_demarche>http.*)$/m =~ description
|
|
|
|
|
description = original_description
|
|
|
|
|
end
|
|
|
|
|
{
|
|
|
|
|
id: type_champ.old_pj[:stable_id],
|
|
|
|
|
libelle: type_champ.libelle,
|
|
|
|
|
description: description,
|
|
|
|
|
order_place: order_place,
|
|
|
|
|
lien_demarche: lien_demarche
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-01-10 18:46:19 +01:00
|
|
|
|
|
|
|
|
|
def self.serialize_champs_as_pjs(dossier)
|
|
|
|
|
dossier.champs.select { |champ| champ.type_de_champ.old_pj }.map do |champ|
|
|
|
|
|
{
|
|
|
|
|
created_at: champ.created_at&.in_time_zone('UTC'),
|
|
|
|
|
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
|
|
|
|
|
content_url: champ.for_api,
|
|
|
|
|
user: champ.dossier.user
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
|
end
|