add dynamic documents
This commit is contained in:
@@ -10,6 +10,7 @@ module Templates
|
||||
|
||||
template.external_id = external_id
|
||||
template.shared_link = original_template.shared_link
|
||||
template.variables_schema = original_template.variables_schema
|
||||
template.author = author
|
||||
template.name = name.presence || "#{original_template.name} (#{I18n.t('clone')})"
|
||||
|
||||
|
||||
@@ -36,11 +36,9 @@ module Templates
|
||||
next unless new_attachment_uuid
|
||||
|
||||
new_document =
|
||||
template.documents_attachments.new(
|
||||
uuid: new_attachment_uuid,
|
||||
blob_id: document.blob_id
|
||||
)
|
||||
template.documents_attachments.new(uuid: new_attachment_uuid, blob_id: document.blob_id)
|
||||
|
||||
maybe_clone_dynamic_document(template, original_template, new_document, document)
|
||||
clone_document_preview_images_attachments(document:, new_document:)
|
||||
|
||||
new_document
|
||||
@@ -51,6 +49,32 @@ module Templates
|
||||
attachments
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def clone_document_preview_images_attachments(document:, new_document:)
|
||||
document.preview_images_attachments.each do |preview_image|
|
||||
new_document.preview_images_attachments.new(blob_id: preview_image.blob_id)
|
||||
|
||||
@@ -24,10 +24,18 @@ module Templates
|
||||
|
||||
module_function
|
||||
|
||||
def call(template, params, extract_fields: false)
|
||||
extract_zip_files(params[:files].presence || params[:file]).flat_map do |file|
|
||||
handle_file_types(template, file, params, extract_fields:)
|
||||
def call(template, params, extract_fields: false, dynamic: false)
|
||||
documents = []
|
||||
dynamic_documents = []
|
||||
|
||||
extract_zip_files(params[:files].presence || params[:file]).each do |file|
|
||||
docs, dynamic_docs = handle_file_types(template, file, params, extract_fields:, dynamic:)
|
||||
|
||||
documents.push(*docs)
|
||||
dynamic_documents.push(*dynamic_docs)
|
||||
end
|
||||
|
||||
[documents, dynamic_documents]
|
||||
end
|
||||
|
||||
def handle_pdf_or_image(template, file, document_data = nil, params = {}, extract_fields: false)
|
||||
@@ -108,12 +116,12 @@ module Templates
|
||||
extracted_files
|
||||
end
|
||||
|
||||
def handle_file_types(template, file, params, extract_fields:)
|
||||
def handle_file_types(template, file, params, extract_fields:, dynamic: false)
|
||||
if file.content_type.include?('image') || file.content_type == PDF_CONTENT_TYPE
|
||||
return handle_pdf_or_image(template, file, file.read, params, extract_fields:)
|
||||
return [handle_pdf_or_image(template, file, file.read, params, extract_fields:), []]
|
||||
end
|
||||
|
||||
raise InvalidFileType, file.content_type
|
||||
raise InvalidFileType, "#{file.content_type}/#{dynamic}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ module Templates
|
||||
|
||||
# rubocop:disable Metrics
|
||||
def call(template, params = {}, extract_fields: false)
|
||||
documents = Templates::CreateAttachments.call(template, params, extract_fields:)
|
||||
documents, = Templates::CreateAttachments.call(template, params, extract_fields:)
|
||||
submitter = template.submitters.first
|
||||
|
||||
documents.each_with_index do |document, index|
|
||||
|
||||
Reference in New Issue
Block a user