Files
docuseal/app/controllers/api/templates_documents_controller.rb
T

46 lines
1.2 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
module Api
2023-05-31 23:19:20 +03:00
class TemplatesDocumentsController < ApiBaseController
2023-05-21 17:59:36 +03:00
def create
2023-07-12 23:56:42 +03:00
return head :unprocessable_entity if params[:blobs].blank? && params[:files].blank?
2023-05-31 23:19:20 +03:00
@template = current_account.templates.find(params[:template_id])
2023-05-21 17:59:36 +03:00
documents =
find_or_create_blobs.map do |blob|
2023-05-31 23:19:20 +03:00
document = @template.documents.create!(blob:)
2023-05-21 17:59:36 +03:00
2023-05-31 23:19:20 +03:00
Templates::ProcessDocument.call(document)
2023-05-21 17:59:36 +03:00
end
schema = documents.map do |doc|
{ attachment_uuid: doc.uuid, name: doc.filename.base }
end
render json: {
schema:,
documents: documents.as_json(
include: {
preview_images: { methods: %i[url metadata filename] }
}
)
}
end
private
def find_or_create_blobs
blobs = params[:blobs]&.map do |attrs|
ActiveStorage::Blob.find_signed(attrs[:signed_id])
end
blobs || params[:files].map do |file|
ActiveStorage::Blob.create_and_upload!(io: file.open,
filename: file.original_filename,
content_type: file.content_type)
end
end
2023-05-21 17:59:36 +03:00
end
end