Files
docuseal/lib/templates/clone_attachments.rb
T

85 lines
2.6 KiB
Ruby
Raw Normal View History

2023-06-18 16:26:51 +00:00
# frozen_string_literal: true
module Templates
module CloneAttachments
module_function
2025-05-31 15:15:25 +03:00
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [], save: true)
2023-12-18 13:38:01 +02:00
schema_uuids_replacements = {}
2024-12-10 22:22:17 +02:00
template.schema.each_with_index do |schema_item, index|
2025-04-25 21:19:28 +03:00
next if excluded_attachment_uuids.include?(schema_item['attachment_uuid'])
2023-12-18 13:38:01 +02:00
new_schema_item_uuid = SecureRandom.uuid
schema_uuids_replacements[schema_item['attachment_uuid']] = new_schema_item_uuid
schema_item['attachment_uuid'] = new_schema_item_uuid
2024-12-10 17:41:27 +02:00
new_name = documents&.dig(index, 'name')
schema_item['name'] = new_name if new_name.present?
2023-12-18 13:38:01 +02:00
end
2024-12-10 22:22:17 +02:00
template.fields.each do |field|
2023-12-18 13:38:01 +02:00
next if field['areas'].blank?
field['areas'].each do |area|
2025-04-25 21:19:28 +03:00
new_attachment_uuid = schema_uuids_replacements[area['attachment_uuid']]
area['attachment_uuid'] = new_attachment_uuid if new_attachment_uuid
2023-12-18 13:38:01 +02:00
end
end
2025-05-31 15:15:25 +03:00
attachments =
original_template.schema_documents.filter_map do |document|
new_attachment_uuid = schema_uuids_replacements[document.uuid]
2023-12-18 13:38:01 +02:00
2025-05-31 15:15:25 +03:00
next unless new_attachment_uuid
2025-04-25 21:19:28 +03:00
2025-05-31 15:15:25 +03:00
new_document =
2026-02-12 09:22:08 +02:00
template.documents_attachments.new(uuid: new_attachment_uuid, blob_id: document.blob_id)
2023-06-18 16:26:51 +00:00
2026-02-12 09:22:08 +02:00
maybe_clone_dynamic_document(template, original_template, new_document, document)
2025-05-31 15:15:25 +03:00
clone_document_preview_images_attachments(document:, new_document:)
2024-07-16 01:40:38 +03:00
2025-05-31 15:15:25 +03:00
new_document
end
template.save! if save
attachments
2023-12-18 13:38:01 +02:00
end
2026-02-12 09:22:08 +02:00
def maybe_clone_dynamic_document(template, original_template, document, original_document)
schema_item = original_template.schema.find { |e| e['attachment_uuid'] == original_document.uuid }
return unless schema_item
return unless schema_item['dynamic']
dynamic_document = original_template.dynamic_documents.find { |e| e.uuid == original_document.uuid }
return unless dynamic_document
new_dynamic_document = template.dynamic_documents.new(
uuid: document.uuid,
body: dynamic_document.body,
head: dynamic_document.head
)
dynamic_document.attachments_attachments.each do |attachment|
new_dynamic_document.attachments_attachments.new(
uuid: attachment.uuid,
blob_id: attachment.blob_id
)
end
new_dynamic_document
end
2023-12-18 13:38:01 +02:00
def clone_document_preview_images_attachments(document:, new_document:)
2025-05-31 15:15:25 +03:00
document.preview_images_attachments.each do |preview_image|
new_document.preview_images_attachments.new(blob_id: preview_image.blob_id)
2023-06-18 16:26:51 +00:00
end
end
end
end