mirror of https://github.com/docusealco/docuseal
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							42 lines
						
					
					
						
							1.5 KiB
						
					
					
				
			
		
		
	
	
							42 lines
						
					
					
						
							1.5 KiB
						
					
					
				# frozen_string_literal: true
 | 
						|
 | 
						|
module Templates
 | 
						|
  module SerializeForApi
 | 
						|
    SERIALIZE_PARAMS = {
 | 
						|
      methods: %i[application_key folder_name],
 | 
						|
      include: { author: { only: %i[id email first_name last_name] } }
 | 
						|
    }.freeze
 | 
						|
 | 
						|
    module_function
 | 
						|
 | 
						|
    def call(template, schema_documents = template.schema_documents.preload(:blob), preview_image_attachments = nil)
 | 
						|
      json = template.as_json(SERIALIZE_PARAMS)
 | 
						|
 | 
						|
      preview_image_attachments ||=
 | 
						|
        ActiveStorage::Attachment.joins(:blob)
 | 
						|
                                 .where(blob: { filename: '0.jpg' })
 | 
						|
                                 .where(record_id: schema_documents.map(&:id),
 | 
						|
                                        record_type: 'ActiveStorage::Attachment',
 | 
						|
                                        name: :preview_images)
 | 
						|
                                 .preload(:blob)
 | 
						|
 | 
						|
      json[:documents] = template.schema.map do |item|
 | 
						|
        attachment = schema_documents.find { |e| e.uuid == item['attachment_uuid'] }
 | 
						|
 | 
						|
        first_page_blob = preview_image_attachments.find { |e| e.record_id == attachment.id }&.blob
 | 
						|
        first_page_blob ||= attachment.preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })&.blob
 | 
						|
 | 
						|
        {
 | 
						|
          id: attachment.id,
 | 
						|
          uuid: attachment.uuid,
 | 
						|
          url: ActiveStorage::Blob.proxy_url(attachment.blob),
 | 
						|
          preview_image_url: first_page_blob && ActiveStorage::Blob.proxy_url(first_page_blob),
 | 
						|
          filename: attachment.filename
 | 
						|
        }
 | 
						|
      end
 | 
						|
 | 
						|
      json
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |