2016-03-22 17:36:36 +01:00
|
|
|
|
class PiecesJustificativesService
|
2019-07-01 15:55:37 +02:00
|
|
|
|
def self.liste_pieces_justificatives(dossier)
|
2020-06-24 12:56:58 +02:00
|
|
|
|
pjs_champs = pjs_for_champs(dossier)
|
|
|
|
|
pjs_commentaires = pjs_for_commentaires(dossier)
|
2019-11-20 11:44:04 +01:00
|
|
|
|
|
2020-06-24 12:56:58 +02:00
|
|
|
|
(pjs_champs + pjs_commentaires)
|
|
|
|
|
.filter(&:attached?)
|
2019-07-01 15:55:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.pieces_justificatives_total_size(dossier)
|
|
|
|
|
liste_pieces_justificatives(dossier)
|
2019-11-20 11:44:04 +01:00
|
|
|
|
.sum(&:byte_size)
|
2019-07-01 15:55:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-08-27 19:55:10 +02:00
|
|
|
|
def self.serialize_types_de_champ_as_type_pj(revision)
|
|
|
|
|
tdcs = revision.types_de_champ.filter { |type_champ| type_champ.old_pj.present? }
|
2019-01-10 15:55:04 +01:00
|
|
|
|
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)
|
2019-09-12 11:26:22 +02:00
|
|
|
|
dossier.champs.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
|
2019-01-10 18:46:19 +01:00
|
|
|
|
{
|
|
|
|
|
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
|
2019-09-03 15:17:19 +02:00
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2020-06-24 12:56:58 +02:00
|
|
|
|
def self.pjs_for_champs(dossier)
|
2020-06-24 15:21:51 +02:00
|
|
|
|
allowed_champs = dossier.champs + dossier.champs_private
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
|
|
|
|
allowed_child_champs = allowed_champs
|
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
|
|
|
|
|
.flat_map(&:champs)
|
|
|
|
|
|
|
|
|
|
(allowed_champs + allowed_child_champs)
|
2019-09-12 11:26:22 +02:00
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }
|
2019-11-20 11:44:04 +01:00
|
|
|
|
.map(&:piece_justificative_file)
|
2019-09-03 15:17:19 +02:00
|
|
|
|
end
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
|
|
|
|
def self.pjs_for_commentaires(dossier)
|
|
|
|
|
dossier
|
|
|
|
|
.commentaires
|
|
|
|
|
.map(&:piece_jointe)
|
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
|
end
|