initial implementation of async export
This commit is contained in:
parent
e66a94d7d2
commit
1af2b63ed1
6 changed files with 63 additions and 0 deletions
|
@ -205,6 +205,14 @@ module Instructeurs
|
|||
end
|
||||
end
|
||||
|
||||
def download_dossiers_mail
|
||||
options = params.permit(:format, tables: [])
|
||||
DownloadDossiersJob.perform_later(procedure, options, current_instructeur)
|
||||
|
||||
flash.notice = "Le dossier va vous être envoyé par mail"
|
||||
redirect_to procedure
|
||||
end
|
||||
|
||||
def email_notifications
|
||||
@procedure = procedure
|
||||
@assign_to = assign_to
|
||||
|
|
32
app/jobs/download_dossiers_job.rb
Normal file
32
app/jobs/download_dossiers_job.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
class DownloadDossiersJob < ApplicationJob
|
||||
def perform(procedure, options, instructeur)
|
||||
dossiers = instructeur.dossiers.for_procedure(procedure)
|
||||
format = options[:format]
|
||||
options.delete(:format)
|
||||
|
||||
case format
|
||||
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|
|
||||
blob = ActiveStorage::Blob.create_after_upload!(
|
||||
io: io,
|
||||
filename: filename
|
||||
)
|
||||
|
||||
InstructeurMailer.download_procedure(instructeur, procedure, blob).deliver_now
|
||||
File.delete(file_path)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -42,4 +42,12 @@ class InstructeurMailer < ApplicationMailer
|
|||
|
||||
mail(to: instructeur.email, subject: subject)
|
||||
end
|
||||
|
||||
def download_procedure(instructeur, procedure, blob)
|
||||
@procedure = procedure
|
||||
@lien_telechargement = url_for(blob)
|
||||
subject = "Votre export de la procédure #{procedure.id} est disponible"
|
||||
|
||||
mail(to: instructeur.email, subject: subject)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
%p
|
||||
Bonjour,
|
||||
|
||||
%p
|
||||
Vous avez demandé un export des dossiers de la procédure nº #{@procedure.id} « #{@procedure.libelle} ». Cliquez sur le lien ci-dessous pour le télécharger :
|
||||
= link_to('Télécharger les dossiers', @lien_telechargement)
|
||||
|
||||
= render partial: "layouts/mailers/signature"
|
|
@ -20,3 +20,9 @@
|
|||
= link_to "Au format .ods #{old_format_message}", procedure_dossiers_download_path(procedure, format: :ods, version: 'v1'), target: "_blank", rel: "noopener"
|
||||
%li
|
||||
= link_to "Au format .csv #{old_format_message}", procedure_dossiers_download_path(procedure, format: :csv, version: 'v1'), target: "_blank", rel: "noopener"
|
||||
%li
|
||||
= link_to "Par mail, au format .xlsx", download_dossiers_mail_instructeur_procedure_path(procedure, format: :xlsx, version: 'v2')
|
||||
%li
|
||||
= link_to "Par mail, au format .ods", download_dossiers_mail_instructeur_procedure_path(procedure, format: :ods, version: 'v2')
|
||||
%li
|
||||
= link_to "Par mail, au format .csv", download_dossiers_mail_instructeur_procedure_path(procedure, format: :csv, version: 'v2')
|
||||
|
|
|
@ -293,6 +293,7 @@ Rails.application.routes.draw do
|
|||
post 'add_filter'
|
||||
get 'remove_filter' => 'procedures#remove_filter', as: 'remove_filter'
|
||||
get 'download_dossiers'
|
||||
get 'download_dossiers_mail'
|
||||
get 'stats'
|
||||
get 'email_notifications'
|
||||
patch 'update_email_notifications'
|
||||
|
|
Loading…
Reference in a new issue