add builder dashboard dropzone
This commit is contained in:
committed by
Pete Matsyburka
parent
e34a763dd9
commit
97b8ac4444
@@ -4,10 +4,12 @@ module Templates
|
||||
module CloneAttachments
|
||||
module_function
|
||||
|
||||
def call(template:, original_template:, documents: [])
|
||||
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [])
|
||||
schema_uuids_replacements = {}
|
||||
|
||||
template.schema.each_with_index do |schema_item, index|
|
||||
next if excluded_attachment_uuids.include?(schema_item['attachment_uuid'])
|
||||
|
||||
new_schema_item_uuid = SecureRandom.uuid
|
||||
|
||||
schema_uuids_replacements[schema_item['attachment_uuid']] = new_schema_item_uuid
|
||||
@@ -22,17 +24,22 @@ module Templates
|
||||
next if field['areas'].blank?
|
||||
|
||||
field['areas'].each do |area|
|
||||
area['attachment_uuid'] = schema_uuids_replacements[area['attachment_uuid']]
|
||||
new_attachment_uuid = schema_uuids_replacements[area['attachment_uuid']]
|
||||
area['attachment_uuid'] = new_attachment_uuid if new_attachment_uuid
|
||||
end
|
||||
end
|
||||
|
||||
template.save!
|
||||
|
||||
original_template.schema_documents.map do |document|
|
||||
original_template.schema_documents.filter_map do |document|
|
||||
new_attachment_uuid = schema_uuids_replacements[document.uuid]
|
||||
|
||||
next unless new_attachment_uuid
|
||||
|
||||
new_document =
|
||||
ApplicationRecord.no_touching do
|
||||
template.documents_attachments.create!(
|
||||
uuid: schema_uuids_replacements[document.uuid],
|
||||
uuid: new_attachment_uuid,
|
||||
blob_id: document.blob_id
|
||||
)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Templates
|
||||
module ReplaceAttachments
|
||||
module_function
|
||||
|
||||
# rubocop:disable Metrics
|
||||
def call(template, params = {}, extract_fields: false)
|
||||
documents = Templates::CreateAttachments.call(template, params, extract_fields:)
|
||||
submitter = template.submitters.first
|
||||
|
||||
documents.each_with_index do |document, index|
|
||||
replaced_document_schema = template.schema[index]
|
||||
|
||||
template.schema[index] = { attachment_uuid: document.uuid, name: document.filename.base }
|
||||
|
||||
if replaced_document_schema
|
||||
template.fields.each do |field|
|
||||
next if field['areas'].blank?
|
||||
|
||||
field['areas'].each do |area|
|
||||
if area['attachment_uuid'] == replaced_document_schema['attachment_uuid']
|
||||
area['attachment_uuid'] = document.uuid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
next if template.fields.any? { |f| f['areas']&.any? { |a| a['attachment_uuid'] == document.uuid } }
|
||||
next unless submitter && document.metadata.dig('pdf', 'fields').present?
|
||||
|
||||
pdf_fields = document.metadata['pdf'].delete('fields').to_a
|
||||
pdf_fields.each { |f| f['submitter_uuid'] = submitter['uuid'] }
|
||||
|
||||
if index.positive? && pdf_fields.present?
|
||||
preview_document = template.schema[index - 1]
|
||||
preview_document_last_field = template.fields.reverse.find do |f|
|
||||
f['areas']&.any? do |a|
|
||||
a['attachment_uuid'] == preview_document[:attachment_uuid]
|
||||
end
|
||||
end
|
||||
|
||||
if preview_document_last_field
|
||||
last_preview_document_field_index = template.fields.find_index do |f|
|
||||
f['uuid'] == preview_document_last_field['uuid']
|
||||
end
|
||||
end
|
||||
|
||||
if last_preview_document_field_index
|
||||
template.fields.insert(index, *pdf_fields)
|
||||
else
|
||||
template.fields += pdf_fields
|
||||
end
|
||||
elsif pdf_fields.present?
|
||||
template.fields += pdf_fields
|
||||
|
||||
template.schema[index]['pending_fields'] = true
|
||||
end
|
||||
end
|
||||
|
||||
documents
|
||||
end
|
||||
# rubocop:enable Metrics
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user