2019-10-02 15:51:37 +02:00
|
|
|
class ExportProcedureJob < ApplicationJob
|
|
|
|
def perform(procedure, instructeur, export_format)
|
2019-09-30 11:57:21 +02:00
|
|
|
dossiers = instructeur.dossiers.for_procedure(procedure)
|
2019-10-02 15:51:37 +02:00
|
|
|
options = { :version => 'v2', :tables => [:dossiers, :etablissements] }
|
2019-09-30 11:57:21 +02:00
|
|
|
|
2019-10-02 15:51:37 +02:00
|
|
|
case export_format
|
2019-09-30 11:57:21 +02:00
|
|
|
when 'csv'
|
|
|
|
filename = procedure.export_filename(:csv)
|
|
|
|
data = procedure.to_csv(dossiers, options)
|
|
|
|
when 'xlsx'
|
|
|
|
filename = procedure.export_filename(:xlsx)
|
|
|
|
data = procedure.to_xlsx(dossiers, options)
|
|
|
|
when 'ods'
|
|
|
|
filename = procedure.export_filename(:ods)
|
|
|
|
data = procedure.to_ods(dossiers, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
file_path = File.join('/tmp/', filename)
|
|
|
|
IO.write(file_path, data)
|
|
|
|
|
|
|
|
File.open(file_path) do |io|
|
2019-10-02 15:51:37 +02:00
|
|
|
# todo: add a TTL to the uploaded file, even though it's checked for in the controller too
|
|
|
|
procedure.export_file = ActiveStorage::Blob.create_after_upload!(
|
2019-09-30 11:57:21 +02:00
|
|
|
io: io,
|
|
|
|
filename: filename
|
|
|
|
)
|
|
|
|
|
2019-10-02 15:51:37 +02:00
|
|
|
InstructeurMailer.download_procedure_export(instructeur, procedure).deliver_now
|
2019-09-30 11:57:21 +02:00
|
|
|
File.delete(file_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|