demarches-normaliennes/app/jobs/export_procedure_job.rb

33 lines
1 KiB
Ruby
Raw Normal View History

class ExportProcedureJob < ApplicationJob
def perform(procedure, instructeur, export_format)
2019-09-30 11:57:21 +02:00
dossiers = instructeur.dossiers.for_procedure(procedure)
options = { :version => 'v2', :tables => [:dossiers, :etablissements] }
2019-09-30 11:57:21 +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|
# 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
)
InstructeurMailer.download_procedure_export(instructeur, procedure).deliver_now
2019-09-30 11:57:21 +02:00
File.delete(file_path)
end
end
end