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
|
|
|
|
|
2024-02-07 17:19:50 +01:00
|
|
|
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers:, user_profile: archive.user_profile)
|
2022-04-04 17:45:10 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-18 15:00:58 +01:00
|
|
|
def zip_root_folder(archive)
|
2022-11-02 16:40:29 +01:00
|
|
|
zip_filename = archive.filename(@procedure)
|
|
|
|
|
|
|
|
[
|
|
|
|
File.basename(zip_filename, File.extname(zip_filename)),
|
|
|
|
archive.id
|
|
|
|
].join("-")
|
2021-08-04 15:08:14 +02:00
|
|
|
end
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|