expire download links
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Accounts
|
||||
LINK_EXPIRES_AT = 40.minutes
|
||||
|
||||
module_function
|
||||
|
||||
def create_duplicate(account)
|
||||
@@ -185,4 +187,11 @@ module Accounts
|
||||
rescue TZInfo::InvalidTimezoneIdentifier
|
||||
'UTC'
|
||||
end
|
||||
|
||||
def link_expires_at(account)
|
||||
return if AccountConfig.find_or_initialize_by(account: account,
|
||||
key: AccountConfig::DOWNLOAD_LINKS_EXPIRE_KEY).value == false
|
||||
|
||||
LINK_EXPIRES_AT.from_now
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,6 +102,7 @@ module ReplaceEmailVariables
|
||||
return unless submitter
|
||||
|
||||
value = submitter.try(field_name)
|
||||
expires_at = nil
|
||||
|
||||
if value_name
|
||||
field = (submission.template_fields || submission.template.fields).find { |e| e['name'] == value_name }
|
||||
@@ -114,7 +115,11 @@ module ReplaceEmailVariables
|
||||
|
||||
attachment = submitter.attachments.find { |e| e.uuid == attachment_uuid }
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob) if attachment
|
||||
if attachment
|
||||
expires_at ||= Accounts.link_expires_at(Account.new(id: submission.account_id))
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:)
|
||||
end
|
||||
else
|
||||
value[field&.dig('uuid')]
|
||||
end
|
||||
|
||||
@@ -391,8 +391,8 @@ module Submissions
|
||||
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 = r.submissions_preview_url(submission.slug, **Docuseal.default_url_options)
|
||||
|
||||
{ link:, text: "#{attachment.filename}\n", style: :link }
|
||||
end,
|
||||
@@ -489,6 +489,10 @@ module Submissions
|
||||
padding: [5, 0, 0, 8],
|
||||
position: :float, text_align: :left)
|
||||
end
|
||||
|
||||
def r
|
||||
Rails.application.routes.url_helpers
|
||||
end
|
||||
# rubocop:enable Metrics
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,8 +6,8 @@ module Submissions
|
||||
|
||||
module_function
|
||||
|
||||
def call(submissions, format: :csv)
|
||||
rows = build_table_rows(submissions)
|
||||
def call(submissions, format: :csv, expires_at: nil)
|
||||
rows = build_table_rows(submissions, expires_at:)
|
||||
|
||||
if format.to_sym == :csv
|
||||
rows_to_csv(rows)
|
||||
@@ -57,7 +57,7 @@ module Submissions
|
||||
headers.map { |key| row.find { |e| e[:name] == key }&.dig(:value) }
|
||||
end
|
||||
|
||||
def build_table_rows(submissions)
|
||||
def build_table_rows(submissions, expires_at: nil)
|
||||
submissions.preload(submitters: [attachments_attachments: :blob, documents_attachments: :blob])
|
||||
.find_each.map do |submission|
|
||||
submission_data = []
|
||||
@@ -69,7 +69,7 @@ module Submissions
|
||||
|
||||
submission_data += build_submission_data(submitter, submitter_name, submitters_count)
|
||||
|
||||
submission_data += submitter_formatted_fields(submitter).map do |field|
|
||||
submission_data += submitter_formatted_fields(submitter, expires_at:).map do |field|
|
||||
{
|
||||
name: column_name(field[:name], submitter_name, submitters_count),
|
||||
value: field[:value]
|
||||
@@ -81,7 +81,7 @@ module Submissions
|
||||
submission_data += submitter.documents.map.with_index(1) do |attachment, index|
|
||||
{
|
||||
name: "#{I18n.t('document')} #{index}",
|
||||
value: ActiveStorage::Blob.proxy_url(attachment.blob)
|
||||
value: ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:)
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -123,7 +123,7 @@ module Submissions
|
||||
submitters_count > 1 ? "#{submitter_name} - #{name}" : name
|
||||
end
|
||||
|
||||
def submitter_formatted_fields(submitter)
|
||||
def submitter_formatted_fields(submitter, expires_at: nil)
|
||||
fields = submitter.submission.template_fields || submitter.submission.template.fields
|
||||
|
||||
template_fields = fields.select { |f| f['submitter_uuid'] == submitter.uuid }
|
||||
@@ -142,11 +142,13 @@ module Submissions
|
||||
value =
|
||||
if template_field_type.in?(%w[image signature])
|
||||
attachment = attachments_index[submitter_value]
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob) if attachment
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:) if attachment
|
||||
elsif template_field_type == 'file'
|
||||
Array.wrap(submitter_value).compact_blank.filter_map do |e|
|
||||
attachment = attachments_index[e]
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob) if attachment
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:) if attachment
|
||||
end
|
||||
else
|
||||
submitter_value
|
||||
|
||||
@@ -201,13 +201,15 @@ module Submissions
|
||||
|
||||
attachments_data_cache = {}
|
||||
|
||||
return pdfs_index if submitter.submission.template_fields.blank?
|
||||
submission = submitter.submission
|
||||
|
||||
with_headings = find_last_submitter(submitter.submission, submitter:).blank? if with_headings.nil?
|
||||
return pdfs_index if submission.template_fields.blank?
|
||||
|
||||
with_headings = find_last_submitter(submission, submitter:).blank? if with_headings.nil?
|
||||
|
||||
locale = submitter.metadata.fetch('lang', account.locale)
|
||||
|
||||
submitter.submission.template_fields.each do |field|
|
||||
submission.template_fields.each do |field|
|
||||
next if field['type'] == 'heading' && !with_headings
|
||||
next if field['submitter_uuid'] != submitter.uuid && field['type'] != 'heading'
|
||||
|
||||
@@ -476,7 +478,7 @@ module Submissions
|
||||
height_diff - (height_diff.zero? ? diff : 0)
|
||||
],
|
||||
A: { Type: :Action, S: :URI,
|
||||
URI: ActiveStorage::Blob.proxy_url(attachment.blob) }
|
||||
URI: r.submissions_preview_url(submission.slug, **Docuseal.default_url_options) }
|
||||
}
|
||||
)
|
||||
|
||||
@@ -877,7 +879,7 @@ module Submissions
|
||||
end
|
||||
end
|
||||
|
||||
def h
|
||||
def r
|
||||
Rails.application.routes.url_helpers
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,6 @@ module Submissions
|
||||
module SerializeForApi
|
||||
SERIALIZE_PARAMS = {
|
||||
only: %i[id name slug source submitters_order expire_at created_at updated_at archived_at],
|
||||
methods: %i[audit_log_url combined_document_url],
|
||||
include: {
|
||||
submitters: { only: %i[id] },
|
||||
template: { only: %i[id name external_id created_at updated_at],
|
||||
@@ -15,11 +14,13 @@ module Submissions
|
||||
|
||||
module_function
|
||||
|
||||
def call(submission, submitters = nil, params = {}, with_events: true, with_documents: true, with_values: true)
|
||||
def call(submission, submitters = nil, params = {}, with_events: true, with_documents: true, with_values: true,
|
||||
expires_at: Accounts.link_expires_at(Account.new(id: submission.account_id)))
|
||||
submitters ||= submission.submitters.preload(documents_attachments: :blob, attachments_attachments: :blob)
|
||||
|
||||
serialized_submitters = submitters.map do |submitter|
|
||||
Submitters::SerializeForApi.call(submitter, with_documents:, with_events: false, with_values:, params:)
|
||||
Submitters::SerializeForApi.call(submitter, with_documents:, with_events: false, with_values:, params:,
|
||||
expires_at:)
|
||||
end
|
||||
|
||||
json = submission.as_json(SERIALIZE_PARAMS)
|
||||
@@ -30,8 +31,6 @@ module Submissions
|
||||
json['submission_events'] = Submitters::SerializeForApi.serialize_events(submission.submission_events)
|
||||
end
|
||||
|
||||
json['combined_document_url'] ||= maybe_build_combined_url(submitters, submission, params)
|
||||
|
||||
if submitters.all?(&:completed_at?)
|
||||
last_submitter = submitters.max_by(&:completed_at)
|
||||
|
||||
@@ -39,10 +38,16 @@ module Submissions
|
||||
json['documents'] = serialized_submitters.find { |e| e['id'] == last_submitter.id }['documents']
|
||||
end
|
||||
|
||||
json['audit_log_url'] = submission.audit_log_url(expires_at:)
|
||||
json['combined_document_url'] = submission.combined_document_url(expires_at:)
|
||||
json['combined_document_url'] ||= maybe_build_combined_url(submitters, submission, params, expires_at:)
|
||||
|
||||
json['status'] = 'completed'
|
||||
json['completed_at'] = last_submitter.completed_at.as_json
|
||||
else
|
||||
json['documents'] = [] if with_documents
|
||||
json['audit_log_url'] = nil
|
||||
json['combined_document_url'] = nil
|
||||
json['status'] = build_status(submission, submitters)
|
||||
json['completed_at'] = nil
|
||||
end
|
||||
@@ -60,7 +65,7 @@ module Submissions
|
||||
end
|
||||
end
|
||||
|
||||
def maybe_build_combined_url(submitters, submission, params)
|
||||
def maybe_build_combined_url(submitters, submission, params, expires_at: nil)
|
||||
return unless submitters.all?(&:completed_at?)
|
||||
|
||||
attachment = submission.combined_document_attachment
|
||||
@@ -71,7 +76,7 @@ module Submissions
|
||||
attachment = Submissions::GenerateCombinedAttachment.call(submitter)
|
||||
end
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob) if attachment
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:) if attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ module Submitters
|
||||
module_function
|
||||
|
||||
def call(submitter, with_template: false, with_events: false, with_documents: true, with_urls: false,
|
||||
with_values: true, params: {})
|
||||
with_values: true, params: {}, expires_at: Accounts.link_expires_at(Account.new(id: submitter.account_id)))
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter],
|
||||
associations: if with_documents
|
||||
@@ -24,15 +24,19 @@ module Submitters
|
||||
additional_attrs = {}
|
||||
|
||||
if params[:include].to_s.include?('fields')
|
||||
additional_attrs['fields'] = SerializeForWebhook.build_fields_array(submitter)
|
||||
additional_attrs['fields'] = SerializeForWebhook.build_fields_array(submitter, expires_at:)
|
||||
end
|
||||
|
||||
if with_template
|
||||
additional_attrs['template'] = submitter.submission.template.as_json(only: %i[id name created_at updated_at])
|
||||
end
|
||||
|
||||
additional_attrs['values'] = SerializeForWebhook.build_values_array(submitter) if with_values
|
||||
additional_attrs['documents'] = SerializeForWebhook.build_documents_array(submitter) if with_documents
|
||||
additional_attrs['values'] = SerializeForWebhook.build_values_array(submitter, expires_at:) if with_values
|
||||
|
||||
if with_documents
|
||||
additional_attrs['documents'] = SerializeForWebhook.build_documents_array(submitter, expires_at:)
|
||||
end
|
||||
|
||||
additional_attrs['preferences'] = submitter.preferences.except('default_values')
|
||||
additional_attrs['submission_events'] = serialize_events(submitter.submission_events) if with_events
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@ module Submitters
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter)
|
||||
def call(submitter, expires_at: Accounts.link_expires_at(Account.new(id: submitter.account_id)))
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter], associations: [documents_attachments: :blob, attachments_attachments: :blob]
|
||||
).call
|
||||
|
||||
values = build_values_array(submitter)
|
||||
documents = build_documents_array(submitter)
|
||||
values = build_values_array(submitter, expires_at:)
|
||||
documents = build_documents_array(submitter, expires_at:)
|
||||
|
||||
submission = submitter.submission
|
||||
|
||||
@@ -32,7 +32,7 @@ module Submitters
|
||||
'preferences' => submitter.preferences.except('default_values'),
|
||||
'values' => values,
|
||||
'documents' => documents,
|
||||
'audit_log_url' => submitter.submission.audit_log_url,
|
||||
'audit_log_url' => submitter.submission.audit_log_url(expires_at:),
|
||||
'submission_url' => r.submissions_preview_url(submission.slug, **Docuseal.default_url_options),
|
||||
'template' => submission.template.as_json(
|
||||
only: %i[id name external_id created_at updated_at],
|
||||
@@ -40,15 +40,15 @@ module Submitters
|
||||
),
|
||||
'submission' => {
|
||||
'id' => submission.id,
|
||||
'audit_log_url' => submission.audit_log_url,
|
||||
'combined_document_url' => submission.combined_document_url,
|
||||
'audit_log_url' => submission.audit_log_url(expires_at:),
|
||||
'combined_document_url' => submission.combined_document_url(expires_at:),
|
||||
'status' => build_submission_status(submission),
|
||||
'url' => r.submissions_preview_url(submission.slug, **Docuseal.default_url_options),
|
||||
'created_at' => submission.created_at.as_json
|
||||
})
|
||||
end
|
||||
|
||||
def build_values_array(submitter)
|
||||
def build_values_array(submitter, expires_at: nil)
|
||||
fields = submitter.submission.template_fields.presence || submitter.submission&.template&.fields || []
|
||||
attachments_index = submitter.attachments.index_by(&:uuid)
|
||||
submitter_field_counters = Hash.new { 0 }
|
||||
@@ -64,13 +64,13 @@ module Submitters
|
||||
|
||||
next if !submitter.values.key?(field['uuid']) && !submitter.completed_at?
|
||||
|
||||
value = fetch_field_value(field, submitter.values[field['uuid']], attachments_index)
|
||||
value = fetch_field_value(field, submitter.values[field['uuid']], attachments_index, expires_at:)
|
||||
|
||||
{ 'field' => field_name, 'value' => value }
|
||||
end
|
||||
end
|
||||
|
||||
def build_fields_array(submitter)
|
||||
def build_fields_array(submitter, expires_at: nil)
|
||||
fields = submitter.submission.template_fields.presence || submitter.submission&.template&.fields || []
|
||||
attachments_index = submitter.attachments.index_by(&:uuid)
|
||||
submitter_field_counters = Hash.new { 0 }
|
||||
@@ -86,7 +86,7 @@ module Submitters
|
||||
|
||||
next if !submitter.values.key?(field['uuid']) && !submitter.completed_at?
|
||||
|
||||
value = fetch_field_value(field, submitter.values[field['uuid']], attachments_index)
|
||||
value = fetch_field_value(field, submitter.values[field['uuid']], attachments_index, expires_at:)
|
||||
|
||||
{ 'name' => field_name, 'uuid' => field['uuid'], 'value' => value, 'readonly' => field['readonly'] == true }
|
||||
end
|
||||
@@ -104,26 +104,26 @@ module Submitters
|
||||
end
|
||||
end
|
||||
|
||||
def build_documents_array(submitter)
|
||||
def build_documents_array(submitter, expires_at: nil)
|
||||
submitter.documents.map do |attachment|
|
||||
{ 'name' => attachment.filename.base, 'url' => rails_storage_proxy_url(attachment) }
|
||||
{ 'name' => attachment.filename.base, 'url' => rails_storage_proxy_url(attachment, expires_at:) }
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_field_value(field, value, attachments_index)
|
||||
def fetch_field_value(field, value, attachments_index, expires_at: nil)
|
||||
if field['type'].in?(%w[image signature initials stamp payment])
|
||||
rails_storage_proxy_url(attachments_index[value])
|
||||
rails_storage_proxy_url(attachments_index[value], expires_at:)
|
||||
elsif field['type'] == 'file'
|
||||
Array.wrap(value).compact_blank.filter_map { |e| rails_storage_proxy_url(attachments_index[e]) }
|
||||
Array.wrap(value).compact_blank.filter_map { |e| rails_storage_proxy_url(attachments_index[e], expires_at:) }
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
def rails_storage_proxy_url(attachment)
|
||||
def rails_storage_proxy_url(attachment, expires_at: nil)
|
||||
return if attachment.blank?
|
||||
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob)
|
||||
ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:)
|
||||
end
|
||||
|
||||
def r
|
||||
|
||||
@@ -14,7 +14,8 @@ module Templates
|
||||
|
||||
module_function
|
||||
|
||||
def call(template, schema_documents = template.schema_documents.preload(:blob), preview_image_attachments = nil)
|
||||
def call(template, schema_documents: template.schema_documents.preload(:blob), preview_image_attachments: nil,
|
||||
expires_at: Accounts.link_expires_at(Account.new(id: template.account_id)))
|
||||
json = template.as_json(SERIALIZE_PARAMS)
|
||||
|
||||
preview_image_attachments ||=
|
||||
@@ -40,8 +41,8 @@ module Templates
|
||||
{
|
||||
'id' => attachment.id,
|
||||
'uuid' => attachment.uuid,
|
||||
'url' => ActiveStorage::Blob.proxy_url(attachment.blob),
|
||||
'preview_image_url' => first_page_blob && ActiveStorage::Blob.proxy_url(first_page_blob),
|
||||
'url' => ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:),
|
||||
'preview_image_url' => first_page_blob && ActiveStorage::Blob.proxy_url(first_page_blob, expires_at:),
|
||||
'filename' => attachment.filename
|
||||
}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user