2021-03-16 20:47:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Reports
|
|
|
|
class DocxJob < ApplicationJob
|
2021-05-11 19:28:28 +08:00
|
|
|
extend InputSanitizeHelper
|
2021-04-14 21:45:51 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2021-03-16 20:47:23 +08:00
|
|
|
queue_as :reports
|
|
|
|
|
2021-04-23 04:28:50 +08:00
|
|
|
discard_on StandardError do |job, error|
|
2022-01-13 21:36:09 +08:00
|
|
|
report = Report.find(job.arguments.first)
|
2021-05-11 19:28:28 +08:00
|
|
|
|
2021-04-23 04:28:50 +08:00
|
|
|
ActiveRecord::Base.no_touching do
|
2021-05-11 19:28:28 +08:00
|
|
|
report.docx_error!
|
2021-04-23 04:28:50 +08:00
|
|
|
end
|
2021-05-11 19:28:28 +08:00
|
|
|
report_path =
|
|
|
|
if report.docx_file.attached?
|
|
|
|
Rails.application.routes.url_helpers
|
|
|
|
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :docx)
|
|
|
|
else
|
|
|
|
Rails.application.routes.url_helpers.reports_path(team: report.team.id)
|
|
|
|
end
|
|
|
|
user = job.arguments.second
|
|
|
|
notification = Notification.create(
|
|
|
|
type_of: :deliver_error,
|
|
|
|
title: I18n.t('projects.reports.index.generation.error_docx_notification_title'),
|
|
|
|
message: I18n.t('projects.reports.index.generation.error_notification_message',
|
|
|
|
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
|
|
|
|
team_name: sanitize_input(report.team.name))
|
|
|
|
)
|
|
|
|
notification.create_user_notification(user)
|
|
|
|
Rails.logger.error("Couldn't generate DOCX for Report with id: #{report.id}. Error:\n #{error}")
|
2021-04-23 04:28:50 +08:00
|
|
|
end
|
|
|
|
|
2021-05-21 22:35:23 +08:00
|
|
|
def perform(report_id, user, root_url)
|
|
|
|
report = Report.find(report_id)
|
2021-03-16 20:47:23 +08:00
|
|
|
file = Tempfile.new(['report', '.docx'])
|
|
|
|
begin
|
2021-06-07 16:34:58 +08:00
|
|
|
I18n.backend.date_format = user.settings[:date_format]
|
2021-03-16 20:47:23 +08:00
|
|
|
docx = Caracal::Document.new(file.path)
|
2021-05-21 22:35:23 +08:00
|
|
|
Reports::Docx.new(report, docx, user: user, scinote_url: root_url).draw
|
2021-03-16 20:47:23 +08:00
|
|
|
docx.save
|
|
|
|
report.docx_file.attach(io: file, filename: 'report.docx')
|
2021-05-11 19:28:28 +08:00
|
|
|
report.docx_ready!
|
2021-05-07 23:21:44 +08:00
|
|
|
report_path = Rails.application.routes.url_helpers
|
|
|
|
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :docx)
|
2021-04-14 21:45:51 +08:00
|
|
|
notification = Notification.create(
|
|
|
|
type_of: :deliver,
|
2021-04-15 19:12:01 +08:00
|
|
|
title: I18n.t('projects.reports.index.generation.completed_docx_notification_title'),
|
2021-04-14 21:45:51 +08:00
|
|
|
message: I18n.t('projects.reports.index.generation.completed_notification_message',
|
|
|
|
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
|
|
|
|
team_name: sanitize_input(report.team.name))
|
|
|
|
)
|
2021-05-25 21:14:50 +08:00
|
|
|
|
2021-06-09 16:58:27 +08:00
|
|
|
Reports::DocxPreviewJob.perform_now(report.id)
|
2021-04-14 21:45:51 +08:00
|
|
|
notification.create_user_notification(user)
|
2021-03-16 20:47:23 +08:00
|
|
|
ensure
|
2021-06-07 16:34:58 +08:00
|
|
|
I18n.backend.date_format = nil
|
2021-03-16 20:47:23 +08:00
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|