Files
docuseal/lib/templates/serialize_for_api.rb
T

80 lines
3.1 KiB
Ruby
Raw Normal View History

2024-02-24 19:00:23 +02:00
# frozen_string_literal: true
module Templates
module SerializeForApi
2024-04-27 22:43:12 +03:00
SERIALIZE_PARAMS = {
2024-08-20 15:34:18 +03:00
only: %w[
2026-07-13 19:14:52 +03:00
id name slug schema submitters fields variables_schema
preferences source created_at updated_at archived_at author_id
external_id folder_id shared_link
2024-08-20 15:34:18 +03:00
],
2024-04-27 22:43:12 +03:00
methods: %i[application_key folder_name],
include: { author: { only: %i[id email first_name last_name] } }
}.freeze
2024-02-24 19:00:23 +02:00
module_function
2026-05-27 21:25:25 +03:00
def call(template, schema_documents: template.schema_documents.preload(:blob), dynamic_documents: nil,
preview_image_attachments: nil, expires_at: Accounts.link_expires_at(Account.new(id: template.account_id)))
2024-04-27 22:43:12 +03:00
json = template.as_json(SERIALIZE_PARAMS)
2024-02-24 19:00:23 +02:00
2026-05-27 21:25:25 +03:00
dynamic_documents ||= preload_dynamic_documents(template)
2024-07-29 17:23:05 +03:00
2026-05-27 21:25:25 +03:00
preview_image_attachments ||= preload_preview_image_attachments(schema_documents, dynamic_documents)
2024-02-24 19:00:23 +02:00
2026-07-13 19:14:52 +03:00
json['variables_schema'] ||= {}
json['documents'] = build_documents_array(template, schema_documents, dynamic_documents,
preview_image_attachments, expires_at)
json
end
def build_documents_array(template, schema_documents, dynamic_documents, preview_image_attachments, expires_at)
template.schema.filter_map do |item|
2026-05-27 21:25:25 +03:00
if item['dynamic']
dynamic_document = dynamic_documents.find { |e| e.uuid == item['attachment_uuid'] }
2024-10-29 12:11:21 +02:00
2026-05-27 21:25:25 +03:00
attachment = dynamic_document.current_version&.document_attachment
2024-10-29 12:11:21 +02:00
end
2026-05-27 21:25:25 +03:00
attachment ||= schema_documents.find { |e| e.uuid == item['attachment_uuid'] }
next unless attachment
2024-02-24 19:00:23 +02:00
first_page_blob = preview_image_attachments.find { |e| e.record_id == attachment.id }&.blob
2024-09-11 16:46:33 +03:00
first_page_blob ||= attachment.preview_images.joins(:blob).find_by(blob: { filename: ['0.jpg', '0.png'] })&.blob
2024-02-24 19:00:23 +02:00
{
2025-08-23 11:28:26 +03:00
'id' => attachment.id,
'uuid' => attachment.uuid,
2025-08-31 16:40:18 +03:00
'url' => ActiveStorage::Blob.proxy_url(attachment.blob, expires_at:),
'preview_image_url' => first_page_blob && ActiveStorage::Blob.proxy_url(first_page_blob, expires_at:),
2025-08-23 11:28:26 +03:00
'filename' => attachment.filename
2024-02-24 19:00:23 +02:00
}
end
end
2026-05-27 21:25:25 +03:00
def preload_dynamic_documents(template)
return DynamicDocument.none if template.schema.none? { |item| item['dynamic'] }
template.schema_dynamic_documents
.preload(current_version: { document_attachment: :blob })
.select(:id, :uuid, :template_id, :sha1, :created_at, :updated_at)
end
def preload_preview_image_attachments(schema_documents, dynamic_documents)
record_ids =
schema_documents.map(&:id) +
dynamic_documents.filter_map { |d| d.current_version&.document_attachment&.id }
ActiveStorage::Attachment.joins(:blob)
.where(blob: { filename: ['0.jpg', '0.png'] })
.where(record_id: record_ids,
record_type: 'ActiveStorage::Attachment',
name: :preview_images)
.preload(:blob)
end
2024-02-24 19:00:23 +02:00
end
end