one-off submission

This commit is contained in:
Pete Matsyburka
2025-05-31 15:15:25 +03:00
committed by Pete Matsyburka
parent c59d948d41
commit f03166b4ea
47 changed files with 218 additions and 145 deletions
+5 -1
View File
@@ -2,6 +2,7 @@
module ReplaceEmailVariables
TEMPLATE_NAME = /\{+template\.name\}+/i
SUBMISSION_NAME = /\{+submission\.name\}+/i
TEMPLATE_ID = /\{+template\.id\}+/i
SUBMITTER_LINK = /\{+submitter\.link\}+/i
ACCOUNT_NAME = /\{+account\.name\}+/i
@@ -31,7 +32,10 @@ module ReplaceEmailVariables
# rubocop:disable Metrics
def call(text, submitter:, tracking_event_type: 'click_email', html_escape: false, sig: nil)
text = replace(text, TEMPLATE_NAME, html_escape:) { (submitter.template || submitter.submission.template).name }
text = replace(text, TEMPLATE_NAME, html_escape:) do
(submitter.template || submitter.submission.template || submitter.submission).name
end
text = replace(text, SUBMISSION_NAME, html_escape:) { submitter.submission.name }
text = replace(text, TEMPLATE_ID, html_escape:) { submitter.template.id }
text = replace(text, SUBMITTER_ID, html_escape:) { submitter.id }
text = replace(text, SUBMITTER_SLUG, html_escape:) { submitter.slug }
+8 -6
View File
@@ -21,7 +21,7 @@ module Submissions
arel = arel.or(Arel::Table.new(:submitters)[:values].matches(term)) if search_values
if search_template
submissions = submissions.joins(:template)
submissions = submissions.left_joins(:template)
arel = arel.or(Template.arel_table[:name].lower.matches("%#{keyword.downcase}%"))
end
@@ -40,15 +40,17 @@ module Submissions
def preload_with_pages(submission)
ActiveRecord::Associations::Preloader.new(
records: [submission],
associations: [:template, { template_schema_documents: :blob }]
associations: [
submission.template_id? ? { template_schema_documents: :blob } : { documents_attachments: :blob }
]
).call
total_pages =
submission.template_schema_documents.sum { |e| e.metadata.dig('pdf', 'number_of_pages').to_i }
submission.schema_documents.sum { |e| e.metadata.dig('pdf', 'number_of_pages').to_i }
if total_pages < PRELOAD_ALL_PAGES_AMOUNT
ActiveRecord::Associations::Preloader.new(
records: submission.template_schema_documents,
records: submission.schema_documents,
associations: [:blob, { preview_images_attachments: :blob }]
).call
end
@@ -90,10 +92,10 @@ module Submissions
emails
end
def create_from_submitters(template:, user:, submissions_attrs:, source:,
def create_from_submitters(template:, user:, submissions_attrs:, source:, with_template: true,
submitters_order: DEFAULT_SUBMITTERS_ORDER, params: {})
Submissions::CreateFromSubmitters.call(
template:, user:, submissions_attrs:, source:, submitters_order:, params:
template:, user:, submissions_attrs:, source:, submitters_order:, params:, with_template:
)
end
+7 -4
View File
@@ -7,7 +7,7 @@ module Submissions
module_function
# rubocop:disable Metrics
def call(template:, user:, submissions_attrs:, source:, submitters_order:, params: {})
def call(template:, user:, submissions_attrs:, source:, submitters_order:, params: {}, with_template: true)
preferences = Submitters.normalize_preferences(user.account, user, params)
submissions = Array.wrap(submissions_attrs).filter_map do |attrs|
@@ -21,6 +21,7 @@ module Submissions
submission = template.submissions.new(created_by_user: user, source:,
account_id: user.account_id,
preferences: set_submission_preferences,
name: with_template ? attrs[:name] : (attrs[:name] || template.name),
expire_at:,
template_submitters: [], submitters_order:)
@@ -60,7 +61,7 @@ module Submissions
preferences: preferences.merge(submission_preferences))
end
maybe_set_template_fields(submission, attrs[:submitters])
maybe_set_template_fields(submission, attrs[:submitters], with_template:)
if submission.submitters.size > template.submitters.size
raise BaseError, 'Defined more signing parties than in template'
@@ -76,6 +77,8 @@ module Submissions
maybe_add_invite_submitters(submission, template)
submission.template = nil unless with_template
submission.tap(&:save!)
end
@@ -118,7 +121,7 @@ module Submissions
}.compact_blank
end
def maybe_set_template_fields(submission, submitters_attrs, default_submitter_uuid: nil)
def maybe_set_template_fields(submission, submitters_attrs, default_submitter_uuid: nil, with_template: true)
template_fields = (submission.template_fields || submission.template.fields).deep_dup
submitters = submission.template_submitters || submission.template.submitters
@@ -133,7 +136,7 @@ module Submissions
end
if template_fields != (submission.template_fields || submission.template.fields) ||
submitters_attrs.any? { |e| e[:completed].present? }
submitters_attrs.any? { |e| e[:completed].present? } || !with_template
submission.template_fields = template_fields
submission.template_schema = submission.template.schema if submission.template_schema.blank?
end
+3 -2
View File
@@ -58,7 +58,8 @@ module Submissions
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io: io.tap(&:rewind), filename: "#{I18n.t('audit_log')} - #{submission.template.name}.pdf"
io: io.tap(&:rewind), filename: "#{I18n.t('audit_log')} - " \
"#{submission.name || submission.template.name}.pdf"
),
name: 'audit_trail',
record: submission
@@ -186,7 +187,7 @@ module Submissions
last_submitter = submission.submitters.select(&:completed_at).max_by(&:completed_at)
documents_data = Submitters.select_attachments_for_download(last_submitter).map do |document|
original_documents = submission.template.documents.select do |e|
original_documents = submission.schema_documents.select do |e|
e.uuid == (document.metadata['original_uuid'] || document.uuid)
end.presence
@@ -32,7 +32,7 @@ module Submissions
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io: io.tap(&:rewind), filename: "#{submission.template.name}.pdf"
io: io.tap(&:rewind), filename: "#{submission.name || submission.template.name}.pdf"
),
name: 'combined_document',
record: submission
@@ -70,7 +70,7 @@ module Submissions
submitter:,
uuid: GenerateResultAttachments.images_pdf_uuid(original_documents.select(&:image?)),
values_hash:,
name: template.name
name: submission.name || template.name
)
ApplicationRecord.no_touching do
@@ -72,17 +72,17 @@ module Submissions
pdfs_index = generate_pdfs(submitter)
template = submitter.submission.template
account = submitter.account
submission = submitter.submission
pkcs = Accounts.load_signing_pkcs(account)
tsa_url = Accounts.load_timeserver_url(account)
image_pdfs = []
original_documents = template.documents.preload(:blob)
original_documents = submission.schema_documents.preload(:blob)
result_attachments =
submitter.submission.template_schema.filter_map do |item|
submission.template_schema.filter_map do |item|
pdf = pdfs_index[item['attachment_uuid']]
next if pdf.nil?
@@ -114,7 +114,7 @@ module Submissions
tsa_url:,
pkcs:,
uuid: images_pdf_uuid(original_documents.select(&:image?)),
name: template.name
name: submission.name || template.name
)
ApplicationRecord.no_touching do
@@ -656,14 +656,14 @@ module Submissions
Submissions::EnsureResultGenerated.call(latest_submitter) if latest_submitter
documents = latest_submitter&.documents&.preload(:blob).to_a.presence
documents ||= submission.template_schema_documents.preload(:blob)
documents ||= submission.schema_documents.preload(:blob)
attachment_uuids = Submissions.filtered_conditions_schema(submission).pluck('attachment_uuid')
attachments_index = documents.index_by { |a| a.metadata['original_uuid'] || a.uuid }
attachment_uuids.each_with_object({}) do |uuid, acc|
attachment = attachments_index[uuid]
attachment ||= submission.template_schema_documents.preload(:blob).find { |a| a.uuid == uuid }
attachment ||= submission.schema_documents.preload(:blob).find { |a| a.uuid == uuid }
next unless attachment
+1 -1
View File
@@ -3,7 +3,7 @@
module Submissions
module SerializeForApi
SERIALIZE_PARAMS = {
only: %i[id slug source submitters_order expire_at created_at updated_at archived_at],
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] },
+1 -20
View File
@@ -27,7 +27,7 @@ module Submitters
return [submitter.submission.combined_document_attachment]
end
original_documents = submitter.submission.template_schema_documents.preload(:blob)
original_documents = submitter.submission.schema_documents.preload(:blob)
is_more_than_two_images = original_documents.count(&:image?) > 1
submitter.documents.preload(:blob).reject do |attachment|
@@ -36,25 +36,6 @@ module Submitters
end
end
def preload_with_pages(submitter)
ActiveRecord::Associations::Preloader.new(
records: [submitter],
associations: [submission: [:template, { template_schema_documents: :blob }]]
).call
total_pages =
submitter.submission.template_schema_documents.sum { |e| e.metadata.dig('pdf', 'number_of_pages').to_i }
if total_pages < PRELOAD_ALL_PAGES_AMOUNT
ActiveRecord::Associations::Preloader.new(
records: submitter.submission.template_schema_documents,
associations: [:blob, { preview_images_attachments: :blob }]
).call
end
submitter
end
def create_attachment!(submitter, params)
blob =
if (file = params[:file])
+2 -2
View File
@@ -43,8 +43,8 @@ module Templates
templates.where(Template.arel_table[:name].lower.matches("%#{keyword.downcase}%"))
end
def filter_undefined_submitters(template)
template.submitters.to_a.select do |item|
def filter_undefined_submitters(template_submitters)
template_submitters.to_a.select do |item|
item['invite_by_uuid'].blank? && item['optional_invite_by_uuid'].blank? &&
item['linked_to_uuid'].blank? && item['is_requester'].blank? && item['email'].blank?
end
+16 -17
View File
@@ -4,7 +4,7 @@ module Templates
module CloneAttachments
module_function
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [])
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [], save: true)
schema_uuids_replacements = {}
template.schema.each_with_index do |schema_item, index|
@@ -29,32 +29,31 @@ module Templates
end
end
template.save!
attachments =
original_template.schema_documents.filter_map do |document|
new_attachment_uuid = schema_uuids_replacements[document.uuid]
original_template.schema_documents.filter_map do |document|
new_attachment_uuid = schema_uuids_replacements[document.uuid]
next unless new_attachment_uuid
next unless new_attachment_uuid
new_document =
ApplicationRecord.no_touching do
template.documents_attachments.create!(
new_document =
template.documents_attachments.new(
uuid: new_attachment_uuid,
blob_id: document.blob_id
)
end
clone_document_preview_images_attachments(document:, new_document:)
clone_document_preview_images_attachments(document:, new_document:)
new_document
end
new_document
end
template.save! if save
attachments
end
def clone_document_preview_images_attachments(document:, new_document:)
ApplicationRecord.no_touching do
document.preview_images_attachments.each do |preview_image|
new_document.preview_images_attachments.create!(blob_id: preview_image.blob_id)
end
document.preview_images_attachments.each do |preview_image|
new_document.preview_images_attachments.new(blob_id: preview_image.blob_id)
end
end
end
+18
View File
@@ -36,6 +36,24 @@ module Templates
attachment
end
def process(attachment, data, extract_fields: false)
if attachment.content_type == PDF_CONTENT_TYPE && extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
pdf = HexaPDF::Document.new(io: StringIO.new(data))
fields = Templates::FindAcroFields.call(pdf, attachment, data)
end
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
number_of_pages = pdf.pages.size
attachment.metadata['pdf'] ||= {}
attachment.metadata['pdf']['number_of_pages'] = number_of_pages
attachment.metadata['pdf']['fields'] = fields if fields
attachment
end
def generate_preview_image(attachment, data)
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all