2021-04-29 17:29:47 +02:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class ProcedureArchiveService
|
|
|
|
def initialize(procedure)
|
|
|
|
@procedure = procedure
|
|
|
|
end
|
|
|
|
|
2022-06-23 13:51:58 +02:00
|
|
|
def make_and_upload_archive(archive)
|
2022-01-13 12:35:07 +01:00
|
|
|
dossiers = Dossier.visible_by_administration
|
|
|
|
.where(groupe_instructeur: archive.groupe_instructeurs)
|
|
|
|
|
|
|
|
dossiers = if archive.time_span_type == 'everything'
|
|
|
|
dossiers.state_termine
|
2021-04-29 17:29:47 +02:00
|
|
|
else
|
2022-04-05 13:39:37 +02:00
|
|
|
dossiers.processed_in_month(archive.month)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
2022-04-04 17:45:10 +02:00
|
|
|
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers)
|
|
|
|
|
2022-04-08 17:07:54 +02:00
|
|
|
DownloadableFileService.download_and_zip(@procedure, attachments, zip_root_folder(archive)) do |zip_filepath|
|
2022-04-08 17:09:22 +02:00
|
|
|
ArchiveUploader.new(procedure: @procedure, filename: archive.filename(@procedure), filepath: zip_filepath)
|
|
|
|
.upload(archive)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.procedure_files_size(procedure)
|
|
|
|
dossiers_files_size(procedure.dossiers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dossiers_files_size(dossiers)
|
|
|
|
dossiers.map do |dossier|
|
|
|
|
liste_pieces_justificatives_for_archive(dossier).sum(&:byte_size)
|
|
|
|
end.sum
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-18 15:00:58 +01:00
|
|
|
def zip_root_folder(archive)
|
|
|
|
"procedure-#{@procedure.id}-#{archive.id}"
|
2021-08-04 15:08:14 +02:00
|
|
|
end
|
|
|
|
|
2021-04-29 17:29:47 +02:00
|
|
|
def self.attachments_from_champs_piece_justificative(champs)
|
|
|
|
champs
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }
|
|
|
|
.map(&:piece_justificative_file)
|
2021-12-13 14:35:13 +01:00
|
|
|
.filter(&:attached?)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.liste_pieces_justificatives_for_archive(dossier)
|
|
|
|
champs_blocs_repetables = dossier.champs
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
|
|
|
|
.flat_map(&:champs)
|
|
|
|
|
|
|
|
attachments_from_champs_piece_justificative(champs_blocs_repetables + dossier.champs)
|
|
|
|
end
|
|
|
|
end
|