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
|
2023-09-06 18:51:34 +08:00
|
|
|
include FailedDeliveryNotifiableJob
|
2021-04-14 21:45:51 +08:00
|
|
|
|
2021-03-16 20:47:23 +08:00
|
|
|
queue_as :reports
|
|
|
|
|
2023-09-06 18:51:34 +08:00
|
|
|
def perform(report_id, user_id:, root_url:)
|
2021-05-21 22:35:23 +08:00
|
|
|
report = Report.find(report_id)
|
2023-08-11 21:31:29 +08:00
|
|
|
user = User.find(user_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)
|
2023-10-11 19:43:20 +08:00
|
|
|
|
|
|
|
DeliveryNotification.with(
|
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',
|
2023-03-14 01:00:30 +08:00
|
|
|
report_link: "<a href='#{report_path}'>#{escape_input(report.name)}</a>",
|
|
|
|
team_name: escape_input(report.team.name))
|
2023-10-12 17:06:10 +08:00
|
|
|
).deliver(user)
|
2021-05-25 21:14:50 +08:00
|
|
|
|
2021-06-09 16:58:27 +08:00
|
|
|
Reports::DocxPreviewJob.perform_now(report.id)
|
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
|
2023-09-06 18:51:34 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
raise e if report.blank?
|
|
|
|
|
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
report.docx_error!
|
|
|
|
end
|
|
|
|
Rails.logger.error("Couldn't generate DOCX for Report with id: #{report.id}. Error:\n #{e.message}")
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Overrides method from FailedDeliveryNotifiableJob concern
|
|
|
|
def failed_notification_title
|
|
|
|
I18n.t('projects.reports.index.generation.error_docx_notification_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overrides method from FailedDeliveryNotifiableJob concern
|
|
|
|
def failed_notification_message
|
|
|
|
report = Report.find_by(id: arguments.first)
|
|
|
|
return '' if report.blank?
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
I18n.t('projects.reports.index.generation.error_notification_message',
|
|
|
|
report_link: "<a href='#{report_path}'>#{escape_input(report.name)}</a>",
|
|
|
|
team_name: escape_input(report.team.name))
|
2021-03-16 20:47:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|