Files
docuseal/lib/submissions/generate_audit_trail.rb
T

449 lines
17 KiB
Ruby
Raw Normal View History

2023-09-10 19:25:57 +03:00
# frozen_string_literal: true
module Submissions
module GenerateAuditTrail
FONT_SIZE = 9
TEXT_COLOR = '525252'
2024-02-16 11:34:30 +02:00
FONT_PATH = '/fonts/GoNotoKurrent-Regular.ttf'
FONT_BOLD_PATH = '/fonts/GoNotoKurrent-Bold.ttf'
2023-09-10 19:25:57 +03:00
FONT_NAME = if File.exist?(FONT_PATH)
2024-05-02 13:40:01 +03:00
'GoNotoKurrent'
2023-09-10 19:25:57 +03:00
else
'Helvetica'
end
2023-12-14 21:22:26 +02:00
CURRENCY_SYMBOLS = {
'USD' => '$',
'EUR' => '€',
'GBP' => '£'
}.freeze
2024-08-21 12:40:50 +03:00
TESTING_FOOTER = GenerateResultAttachments::TESTING_FOOTER
2024-01-15 01:45:26 +02:00
RTL_REGEXP = TextUtils::RTL_REGEXP
2024-02-16 18:59:10 +02:00
MAX_IMAGE_HEIGHT = 100
2024-01-07 14:19:21 +02:00
2024-11-23 01:45:10 +02:00
US_TIMEZONES = TimeUtils::US_TIMEZONES
2024-03-25 20:02:20 +02:00
2023-09-10 19:25:57 +03:00
module_function
# rubocop:disable Metrics
def call(submission)
2024-03-31 17:40:51 +03:00
account = submission.account
2024-05-25 20:57:36 +03:00
2025-06-03 09:52:46 +03:00
last_submitter = submission.submitters.select(&:completed_at).max_by(&:completed_at)
I18n.with_locale(last_submitter.metadata.fetch('lang', account.locale)) do
2024-10-01 13:44:57 +03:00
document = build_audit_trail(submission)
2024-05-25 20:57:36 +03:00
2024-10-01 13:44:57 +03:00
pkcs = Accounts.load_signing_pkcs(account)
tsa_url = Accounts.load_timeserver_url(account)
2024-05-25 20:57:36 +03:00
2024-10-01 13:44:57 +03:00
io = StringIO.new
2024-05-25 20:57:36 +03:00
2024-10-01 13:44:57 +03:00
document.trailer.info[:Creator] = "#{Docuseal.product_name} (#{Docuseal::PRODUCT_URL})"
2024-09-26 10:58:32 +03:00
2024-12-17 23:25:04 +02:00
if pkcs
sign_params = {
reason: sign_reason,
**Submissions::GenerateResultAttachments.build_signing_params(last_submitter, pkcs, tsa_url)
}
document.sign(io, **sign_params)
2025-04-22 15:52:03 +03:00
Submissions::GenerateResultAttachments.maybe_enable_ltv(io, sign_params)
2024-12-17 23:25:04 +02:00
else
document.write(io)
end
2024-07-09 13:15:51 +03:00
2024-10-01 13:44:57 +03:00
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io: io.tap(&:rewind), filename: "#{I18n.t('audit_log')} - #{submission.template.name}.pdf"
),
name: 'audit_trail',
record: submission
)
end
2024-05-25 20:57:36 +03:00
end
def build_audit_trail(submission)
account = submission.account
2024-11-22 10:15:51 +02:00
verify_url = Rails.application.routes.url_helpers.settings_esign_url(
**Docuseal.default_url_options, host: ENV.fetch('EMAIL_HOST', Docuseal.default_url_options[:host])
)
2024-03-25 20:02:20 +02:00
page_size =
if TimeUtils.timezone_abbr(account.timezone, Time.current.beginning_of_year).in?(US_TIMEZONES)
:Letter
else
:A4
end
2023-09-10 19:25:57 +03:00
composer = HexaPDF::Composer.new(skip_page_creation: true)
2024-05-04 11:09:48 +03:00
composer.document.task(:pdfa) if FONT_NAME == 'GoNotoKurrent'
2024-05-02 13:40:01 +03:00
composer.document.config['font.map'] = {
'Helvetica' => {
none: FONT_PATH,
bold: FONT_BOLD_PATH
},
FONT_NAME => {
none: FONT_PATH,
bold: FONT_BOLD_PATH
}
}
2024-03-15 19:02:17 +02:00
composer.document.config['font.on_missing_glyph'] =
Submissions::GenerateResultAttachments.method(:on_missing_glyph).to_proc
2023-09-10 19:25:57 +03:00
divider = HexaPDF::Layout::Box.create(
margin: [0, 0, 15, 0],
border: {
width: [1, 0, 0, 0],
color: %w[hp-gray-light]
},
height: 1
)
2024-09-30 16:27:15 +03:00
configs = submission.account.account_configs.where(key: [AccountConfig::WITH_AUDIT_VALUES_KEY,
AccountConfig::WITH_SIGNATURE_ID])
with_signature_id = configs.find { |c| c.key == AccountConfig::WITH_SIGNATURE_ID }&.value == true
with_audit_values = configs.find { |c| c.key == AccountConfig::WITH_AUDIT_VALUES_KEY }&.value != false
2024-03-25 20:02:20 +02:00
composer.page_style(:default, page_size:) do |canvas, style|
2023-09-10 19:25:57 +03:00
box = canvas.context.box(:media)
canvas.save_graphics_state do
canvas.fill_color('FAF7F5')
.rectangle(0, 0, box.width, 20)
.rectangle(0, box.height - 20, box.width, 20)
.fill
2024-07-24 20:55:35 +03:00
maybe_add_background(canvas, submission, page_size)
2023-09-10 19:25:57 +03:00
end
2024-08-21 12:40:50 +03:00
if with_signature_id || submission.account.testing?
canvas.save_graphics_state do
document_id = Digest::MD5.hexdigest(submission.slug).upcase
2024-08-27 23:36:44 +03:00
font = composer.document.fonts.add(FONT_NAME)
2024-08-21 12:40:50 +03:00
text =
if submission.account.testing?
if with_signature_id
"#{TESTING_FOOTER} | ID: #{document_id}"
else
TESTING_FOOTER
end
else
2024-10-01 13:44:57 +03:00
"#{I18n.t('document_id')}: #{document_id}"
2024-08-21 12:40:50 +03:00
end
2024-08-27 23:36:44 +03:00
text = HexaPDF::Layout::TextFragment.create(
text, font:, font_size: FONT_SIZE, underlays: [
lambda do |canv, box|
canv.fill_color('white').rectangle(-1, 0, box.width + 2, box.height).fill
end
]
)
HexaPDF::Layout::TextLayouter.new(font:, font_size: FONT_SIZE)
.fit([text], 1000, 1000)
.draw(canvas, 1, FONT_SIZE * 1.37)
2024-08-21 12:40:50 +03:00
end
end
2023-09-10 19:25:57 +03:00
style.frame = style.create_frame(canvas.context, 50)
end
2024-02-14 18:52:11 +02:00
composer.style(:base, font: FONT_NAME, font_size: FONT_SIZE, fill_color: TEXT_COLOR, line_spacing: 1)
2023-09-10 19:25:57 +03:00
composer.style(:link, fill_color: 'hp-blue-light', underline: true)
composer.new_page
composer.column(columns: 1) do |column|
2023-12-01 18:42:22 +02:00
add_logo(column, submission)
2023-09-10 19:25:57 +03:00
2024-10-01 13:44:57 +03:00
column.text(account.testing? ? I18n.t('testing_log_not_for_production_use') : I18n.t('audit_log'),
2023-09-10 19:25:57 +03:00
font_size: 16,
2024-02-14 18:52:11 +02:00
padding: [10, 0, 0, 0],
2024-02-15 22:26:05 +02:00
position: :float, text_align: :right)
2023-09-10 19:25:57 +03:00
end
composer.column(columns: 1) do |column|
2024-10-01 13:44:57 +03:00
column.text("#{I18n.t('envelope_id')}: #{submission.id}", font_size: 12,
padding: [15, 0, 8, 0],
position: :float)
2023-12-11 21:40:13 +02:00
2025-01-17 15:52:49 +02:00
if show_verify?(submission)
2024-10-01 13:44:57 +03:00
column.formatted_text([{ link: verify_url, text: I18n.t('verify'), style: :link }],
2024-02-15 22:26:05 +02:00
font_size: 9, padding: [15, 0, 10, 0], position: :float, text_align: :right)
2023-12-11 21:40:13 +02:00
end
2023-09-10 19:25:57 +03:00
end
composer.draw_box(divider)
2024-09-26 10:58:32 +03:00
last_submitter = submission.submitters.select(&:completed_at).max_by(&:completed_at)
2023-09-10 19:25:57 +03:00
documents_data = Submitters.select_attachments_for_download(last_submitter).map do |document|
2024-03-05 17:16:18 +02:00
original_documents = submission.template.documents.select do |e|
e.uuid == (document.metadata['original_uuid'] || document.uuid)
end.presence
2023-09-10 19:25:57 +03:00
original_documents ||= submission.template.documents.select do |e|
2023-10-03 02:14:27 +03:00
e.image? && submission.template_schema.any? do |item|
2023-09-10 19:25:57 +03:00
item['attachment_uuid'] == e.uuid
end
end
2023-09-10 21:38:03 +03:00
[
composer.document.layout.formatted_text_box(
2024-10-01 19:53:08 +03:00
[{ text: document.filename.to_s }]
2023-09-10 21:38:03 +03:00
),
composer.document.layout.formatted_text_box(
[
2024-10-01 13:44:57 +03:00
{ text: "#{I18n.t('original_sha256')}:\n", font: [FONT_NAME, { variant: :bold }] },
2023-09-10 21:38:03 +03:00
original_documents.map { |d| d.metadata['sha256'] || d.checksum }.join("\n"),
"\n",
2024-10-01 13:44:57 +03:00
{ text: "#{I18n.t('result_sha256')}:\n", font: [FONT_NAME, { variant: :bold }] },
2023-09-10 21:38:03 +03:00
document.metadata['sha256'] || document.checksum,
"\n",
2024-10-01 13:44:57 +03:00
{ text: "#{I18n.t('generated_at')}: ", font: [FONT_NAME, { variant: :bold }] },
2023-10-29 16:23:32 +02:00
"#{I18n.l(document.created_at.in_time_zone(account.timezone), format: :long, locale: account.locale)} " \
"#{TimeUtils.timezone_abbr(account.timezone, document.created_at)}"
2024-02-14 18:52:11 +02:00
], line_spacing: 1.3
2023-09-10 21:38:03 +03:00
)
]
2023-09-10 19:25:57 +03:00
end
if documents_data.present?
2024-02-14 18:52:11 +02:00
composer.table(documents_data, cell_style: { padding: [0, 0, 20, 0], border: { width: 0 } })
2023-09-10 19:25:57 +03:00
composer.draw_box(divider)
end
2023-09-10 19:25:57 +03:00
2023-09-12 22:36:04 +03:00
submission.template_submitters.filter_map do |item|
2023-09-10 19:25:57 +03:00
submitter = submission.submitters.find { |e| e.uuid == item['uuid'] }
2023-09-12 22:36:04 +03:00
next if submitter.blank?
2023-09-10 19:25:57 +03:00
completed_event =
submission.submission_events.find { |e| e.submitter_id == submitter.id && e.complete_form? } ||
SubmissionEvent.new
click_email_event =
submission.submission_events.find { |e| e.submitter_id == submitter.id && e.click_email? }
is_phone_verified =
2023-09-12 22:36:04 +03:00
submission.template_fields.any? do |e|
e['type'] == 'phone' && e['submitter_uuid'] == submitter.uuid && submitter.values[e['uuid']].present?
end
2023-09-10 19:25:57 +03:00
2024-12-01 00:10:07 +02:00
is_id_verified =
submission.template_fields.any? do |e|
e['type'] == 'verification' && e['submitter_uuid'] == submitter.uuid && submitter.values[e['uuid']].present?
end
2023-09-10 19:25:57 +03:00
submitter_field_counters = Hash.new { 0 }
2023-09-12 22:36:04 +03:00
info_rows = [
2023-09-10 19:25:57 +03:00
[
composer.document.layout.formatted_text_box(
[
submission.template_submitters.size > 1 && { text: "#{item['name']}\n" },
2024-05-02 13:40:01 +03:00
submitter.email && { text: "#{submitter.email}\n", font: [FONT_NAME, { variant: :bold }] },
2024-01-15 01:45:26 +02:00
submitter.name && { text: "#{TextUtils.maybe_rtl_reverse(submitter.name)}\n" },
2023-09-10 19:25:57 +03:00
submitter.phone && { text: "#{submitter.phone}\n" }
2024-02-14 18:52:11 +02:00
].compact_blank, line_spacing: 1.3, padding: [0, 20, 0, 0]
2023-09-12 22:36:04 +03:00
)
],
[
2023-09-10 19:25:57 +03:00
composer.document.layout.formatted_text_box(
[
2023-12-01 18:42:22 +02:00
submitter.email && click_email_event && {
2024-10-01 13:44:57 +03:00
text: "#{I18n.t('email_verification')}: #{I18n.t('verified')}\n"
2023-09-10 19:25:57 +03:00
},
2023-12-01 18:42:22 +02:00
submitter.phone && is_phone_verified && {
2024-10-01 13:44:57 +03:00
text: "#{I18n.t('phone_verification')}: #{I18n.t('verified')}\n"
2023-09-10 19:25:57 +03:00
},
2024-12-01 00:10:07 +02:00
is_id_verified && {
text: "#{I18n.t('identity_verification')}: #{I18n.t('verified')}\n"
},
2023-09-10 19:25:57 +03:00
completed_event.data['ip'] && { text: "IP: #{completed_event.data['ip']}\n" },
2024-10-01 13:44:57 +03:00
completed_event.data['sid'] && { text: "#{I18n.t('session_id')}: #{completed_event.data['sid']}\n" },
2023-09-10 19:25:57 +03:00
completed_event.data['ua'] && { text: "User agent: #{completed_event.data['ua']}\n" },
"\n"
2024-02-14 18:52:11 +02:00
].compact_blank, line_spacing: 1.3, padding: [10, 20, 20, 0]
2023-09-10 19:25:57 +03:00
)
2023-09-12 22:36:04 +03:00
]
]
composer.table(info_rows, cell_style: { padding: [0, 0, 0, 0], border: { width: 0 } })
2024-10-03 16:39:18 +03:00
submission.template_fields.filter_map do |field|
next if field['submitter_uuid'] != submitter.uuid
next if field['type'] == 'heading'
next if !with_audit_values && !field['type'].in?(%w[signature initials])
2023-09-10 19:25:57 +03:00
2024-10-03 16:39:18 +03:00
submitter_field_counters[field['type']] += 1
2023-09-10 19:25:57 +03:00
2024-10-03 16:39:18 +03:00
value = submitter.values[field['uuid']]
2023-09-10 19:25:57 +03:00
2024-10-03 16:39:18 +03:00
next if Array.wrap(value).compact_blank.blank?
2023-09-12 22:36:04 +03:00
2024-10-03 16:39:18 +03:00
field_name = field['title'].presence || field['name'].to_s
2024-06-04 18:33:24 +03:00
2024-10-03 16:39:18 +03:00
[
composer.formatted_text_box(
[
{
text: TextUtils.maybe_rtl_reverse(field_name).upcase.presence ||
"#{I18n.t("#{field['type']}_field")} #{submitter_field_counters[field['type']]}\n".upcase,
font_size: 6
}
].compact_blank,
text_align: field_name.to_s.match?(RTL_REGEXP) ? :right : :left,
line_spacing: 1.3, padding: [0, 0, 2, 0]
),
if field['type'].in?(%w[image signature initials stamp])
attachment = submitter.attachments.find { |a| a.uuid == value }
2024-11-01 18:39:03 +02:00
image =
begin
2025-05-19 14:06:37 +03:00
Submissions::GenerateResultAttachments.load_vips_image(attachment).autorot
2024-11-01 18:39:03 +02:00
rescue Vips::Error
next unless attachment.content_type.starts_with?('image/')
next if attachment.byte_size.zero?
raise
end
2024-10-03 16:39:18 +03:00
scale = [600.0 / image.width, 600.0 / image.height].min
resized_image = image.resize([scale, 1].min)
io = StringIO.new(resized_image.write_to_buffer('.png'))
width = field['type'] == 'initials' ? 100 : 200
height = resized_image.height * (width.to_f / resized_image.width)
if height > MAX_IMAGE_HEIGHT
width = (MAX_IMAGE_HEIGHT / height) * width
height = MAX_IMAGE_HEIGHT
end
2024-02-16 18:59:10 +02:00
2024-10-03 16:39:18 +03:00
composer.image(io, width:, height:, margin: [5, 0, 10, 0])
composer.formatted_text_box([{ text: '' }])
elsif field['type'].in?(%w[file payment])
if field['type'] == 'payment'
unit = CURRENCY_SYMBOLS[field['preferences']['currency']] || field['preferences']['currency']
2023-12-14 21:22:26 +02:00
2024-10-03 16:39:18 +03:00
price = ApplicationController.helpers.number_to_currency(field['preferences']['price'], unit:)
2023-12-14 21:22:26 +02:00
2024-10-03 16:39:18 +03:00
composer.formatted_text_box([{ text: "#{I18n.t('paid_price', price:)}\n" }],
padding: [0, 0, 10, 0])
end
2023-12-14 21:22:26 +02:00
2024-10-03 16:39:18 +03:00
composer.formatted_text_box(
Array.wrap(value).map do |uuid|
attachment = submitter.attachments.find { |a| a.uuid == uuid }
link =
ActiveStorage::Blob.proxy_url(attachment.blob)
{ link:, text: "#{attachment.filename}\n", style: :link }
end,
padding: [0, 0, 10, 0]
)
elsif field['type'] == 'checkbox'
composer.formatted_text_box([{ text: value.to_s.titleize }], padding: [0, 0, 10, 0])
else
if field['type'] == 'date'
value = TimeUtils.format_date_string(value, field.dig('preferences', 'format'), account.locale)
end
2023-12-17 23:32:05 +02:00
2025-03-04 00:12:56 +02:00
value = NumberUtils.format_number(value, field.dig('preferences', 'format')) if field['type'] == 'number'
2024-05-12 11:12:07 +03:00
2024-10-03 16:39:18 +03:00
value = value.join(', ') if value.is_a?(Array)
2023-10-24 00:32:09 +03:00
2025-03-04 00:12:56 +02:00
if (mask = field.dig('preferences', 'mask').presence)
value = TextUtils.mask_value(value, mask)
end
2025-03-03 00:14:14 +02:00
2024-10-03 16:39:18 +03:00
composer.formatted_text_box([{ text: TextUtils.maybe_rtl_reverse(value.to_s.presence || 'n/a') }],
text_align: value.to_s.match?(RTL_REGEXP) ? :right : :left,
padding: [0, 0, 10, 0])
end
]
2023-09-12 22:36:04 +03:00
end
end
2023-09-10 19:25:57 +03:00
2024-10-03 16:39:18 +03:00
composer.draw_box(divider)
2024-10-01 13:44:57 +03:00
composer.text(I18n.t('event_log'), font_size: 12, padding: [10, 0, 20, 0])
2023-09-10 19:25:57 +03:00
2025-05-06 16:46:08 +03:00
events_data = submission.submission_events.sort_by(&:event_timestamp).filter_map do |event|
next if event.event_type.in?(%w[bounce_email complaint_email])
2023-09-10 19:25:57 +03:00
submitter = submission.submitters.find { |e| e.id == event.submitter_id }
2024-10-01 13:44:57 +03:00
submitter_name =
if event.event_type.include?('sms') || event.event_type.include?('phone')
event.data['phone'] || submitter.phone
else
submitter.name || submitter.email || submitter.phone
end
2024-09-12 17:31:37 +03:00
2024-10-01 13:44:57 +03:00
text =
2024-12-01 00:10:07 +02:00
if event.event_type == 'complete_verification'
I18n.t('submission_event_names.complete_verification_by_html', provider: event.data['method'],
submitter_name:)
elsif event.event_type == 'invite_party' &&
(invited_submitter = submission.submitters.find { |e| e.uuid == event.data['uuid'] }) &&
(name = submission.template_submitters.find { |e| e['uuid'] == event.data['uuid'] }&.dig('name'))
2024-10-01 13:44:57 +03:00
invited_submitter_name = [invited_submitter.name || invited_submitter.email || invited_submitter.phone,
name].join(' ')
I18n.t('submission_event_names.invite_party_by_html', invited_submitter_name:,
submitter_name:)
elsif event.event_type.include?('send_')
I18n.t("submission_event_names.#{event.event_type}_to_html", submitter_name:)
else
I18n.t("submission_event_names.#{event.event_type}_by_html", submitter_name:)
end
2024-09-12 17:31:37 +03:00
2024-10-01 19:32:19 +03:00
bold_text, normal_text = text.match(%r{<b>(.*?)</b>(.*)}).captures
2023-09-10 19:25:57 +03:00
[
2023-10-29 16:23:32 +02:00
"#{I18n.l(event.event_timestamp.in_time_zone(account.timezone), format: :long, locale: account.locale)} " \
"#{TimeUtils.timezone_abbr(account.timezone, event.event_timestamp)}",
2024-10-01 19:32:19 +03:00
composer.document.layout.formatted_text_box([{ text: bold_text, font: [FONT_NAME, { variant: :bold }] },
normal_text])
2023-09-10 19:25:57 +03:00
]
end
2024-02-14 18:52:11 +02:00
composer.table(events_data, cell_style: { padding: [0, 0, 12, 0], border: { width: 0 } }) if events_data.present?
2023-09-10 19:25:57 +03:00
2024-05-25 20:57:36 +03:00
composer.document
end
2023-09-10 19:25:57 +03:00
2024-05-25 20:57:36 +03:00
def sign_reason
2024-10-26 09:54:41 +03:00
'Signed with DocuSeal.com'
2023-09-10 19:25:57 +03:00
end
2023-10-11 21:05:42 +03:00
2024-07-24 20:55:35 +03:00
def maybe_add_background(_canvas, _submission, _page_size); end
2025-01-17 15:52:49 +02:00
def show_verify?(submission)
!submission.source.in?(%w[embed api])
end
2023-12-01 18:42:22 +02:00
def add_logo(column, _submission = nil)
2023-10-11 21:05:42 +03:00
column.image(PdfIcons.logo_io, width: 40, height: 40, position: :float)
column.formatted_text([{ text: 'DocuSeal',
2024-11-22 10:15:51 +02:00
link: Docuseal::PRODUCT_EMAIL_URL }],
2023-10-11 21:05:42 +03:00
font_size: 20,
2024-05-02 13:40:01 +03:00
font: [FONT_NAME, { variant: :bold }],
2023-10-11 21:05:42 +03:00
width: 100,
2024-02-14 18:52:11 +02:00
padding: [5, 0, 0, 8],
2024-02-15 22:26:05 +02:00
position: :float, text_align: :left)
2023-10-11 21:05:42 +03:00
end
2023-09-10 19:25:57 +03:00
# rubocop:enable Metrics
end
end