2023-05-31 23:19:20 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
module Api
|
|
|
|
|
class TemplatesController < ApiBaseController
|
2023-09-17 00:45:57 +03:00
|
|
|
load_and_authorize_resource :template
|
2023-08-11 18:32:33 +03:00
|
|
|
|
2023-08-04 00:40:13 +03:00
|
|
|
def index
|
2024-02-01 00:34:35 +02:00
|
|
|
templates = filter_templates(@templates, params)
|
2023-10-01 01:57:47 +03:00
|
|
|
|
2025-07-20 08:28:19 +03:00
|
|
|
templates = paginate(templates.preload(:author, folder: :parent_folder))
|
2024-02-24 19:00:23 +02:00
|
|
|
|
|
|
|
|
schema_documents =
|
|
|
|
|
ActiveStorage::Attachment.where(record_id: templates.map(&:id),
|
|
|
|
|
record_type: 'Template',
|
|
|
|
|
name: :documents,
|
|
|
|
|
uuid: templates.flat_map { |t| t.schema.pluck('attachment_uuid') })
|
|
|
|
|
.preload(:blob)
|
|
|
|
|
|
|
|
|
|
preview_image_attachments =
|
|
|
|
|
ActiveStorage::Attachment.joins(:blob)
|
2024-09-13 10:55:45 +03:00
|
|
|
.where(blob: { filename: ['0.png', '0.jpg'] })
|
2024-02-24 19:00:23 +02:00
|
|
|
.where(record_id: schema_documents.map(&:id),
|
|
|
|
|
record_type: 'ActiveStorage::Attachment',
|
|
|
|
|
name: :preview_images)
|
|
|
|
|
.preload(:blob)
|
2023-10-01 01:57:47 +03:00
|
|
|
|
2025-08-31 16:40:18 +03:00
|
|
|
expires_at = Accounts.link_expires_at(current_account)
|
|
|
|
|
|
2023-10-01 01:57:47 +03:00
|
|
|
render json: {
|
2024-02-24 19:00:23 +02:00
|
|
|
data: templates.map do |t|
|
2025-08-31 16:40:18 +03:00
|
|
|
Templates::SerializeForApi.call(t,
|
|
|
|
|
schema_documents: schema_documents.select { |e| e.record_id == t.id },
|
|
|
|
|
preview_image_attachments:,
|
|
|
|
|
expires_at:)
|
2024-02-24 19:00:23 +02:00
|
|
|
end,
|
2023-10-01 01:57:47 +03:00
|
|
|
pagination: {
|
|
|
|
|
count: templates.size,
|
|
|
|
|
next: templates.last&.id,
|
|
|
|
|
prev: templates.first&.id
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 00:40:13 +03:00
|
|
|
end
|
|
|
|
|
|
2023-08-11 18:32:33 +03:00
|
|
|
def show
|
2024-02-24 19:00:23 +02:00
|
|
|
render json: Templates::SerializeForApi.call(@template)
|
2023-08-11 18:32:33 +03:00
|
|
|
end
|
2023-05-31 23:19:20 +03:00
|
|
|
|
2023-08-11 18:32:33 +03:00
|
|
|
def update
|
2024-02-06 20:07:15 +02:00
|
|
|
if (folder_name = params[:folder_name] || params.dig(:template, :folder_name))
|
2023-10-01 01:57:47 +03:00
|
|
|
@template.folder = TemplateFolders.find_or_create_by_name(current_user, folder_name)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-25 17:36:56 +02:00
|
|
|
Array.wrap(params[:roles].presence || params.dig(:template, :roles).presence).each_with_index do |role, index|
|
|
|
|
|
if (item = @template.submitters[index])
|
|
|
|
|
item['name'] = role
|
|
|
|
|
else
|
|
|
|
|
@template.submitters << { 'name' => role, 'uuid' => SecureRandom.uuid }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-08 20:44:31 +02:00
|
|
|
archived = params.key?(:archived) ? params[:archived] : params.dig(:template, :archived)
|
|
|
|
|
|
|
|
|
|
if archived.in?([true, false])
|
|
|
|
|
@template.archived_at = archived == true ? Time.current : nil
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
@template.update!(template_params)
|
|
|
|
|
|
2025-06-05 13:16:55 +03:00
|
|
|
SearchEntries.enqueue_reindex(@template)
|
|
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
WebhookUrls.enqueue_events(@template, 'template.updated')
|
2026-03-18 08:46:19 +02:00
|
|
|
WebhookUrls.enqueue_events(@template, 'template.archived') if archived == true
|
2024-05-12 11:37:33 +03:00
|
|
|
|
2023-10-01 01:57:47 +03:00
|
|
|
render json: @template.as_json(only: %i[id updated_at])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2024-12-13 13:31:54 +02:00
|
|
|
if params[:permanently].in?(['true', true])
|
2024-01-17 00:24:21 +02:00
|
|
|
@template.destroy!
|
|
|
|
|
else
|
|
|
|
|
@template.update!(archived_at: Time.current)
|
2026-03-18 08:46:19 +02:00
|
|
|
|
|
|
|
|
WebhookUrls.enqueue_events(@template, 'template.archived')
|
2024-01-17 00:24:21 +02:00
|
|
|
end
|
2023-10-01 01:57:47 +03:00
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
render json: @template.as_json(only: %i[id archived_at])
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-02-01 00:34:35 +02:00
|
|
|
def filter_templates(templates, params)
|
2025-06-05 13:16:55 +03:00
|
|
|
templates = Templates.search(current_user, templates, params[:q])
|
2024-11-28 15:41:32 +02:00
|
|
|
templates = params[:archived].in?(['true', true]) ? templates.archived : templates.active
|
2024-02-01 00:34:35 +02:00
|
|
|
templates = templates.where(external_id: params[:application_key]) if params[:application_key].present?
|
|
|
|
|
templates = templates.where(external_id: params[:external_id]) if params[:external_id].present?
|
2025-04-15 13:22:02 +03:00
|
|
|
templates = templates.where(slug: params[:slug]) if params[:slug].present?
|
2025-06-21 14:58:20 +03:00
|
|
|
|
|
|
|
|
if params[:folder].present?
|
2025-07-20 08:28:19 +03:00
|
|
|
folders = TemplateFolders.filter_by_full_name(TemplateFolder.accessible_by(current_ability), params[:folder])
|
2025-06-21 14:58:20 +03:00
|
|
|
|
2025-07-20 08:28:19 +03:00
|
|
|
templates = templates.where(folder_id: folders.pluck(:id))
|
2025-06-21 14:58:20 +03:00
|
|
|
end
|
2024-02-01 00:34:35 +02:00
|
|
|
|
|
|
|
|
templates
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
def template_params
|
2024-04-15 19:36:16 +03:00
|
|
|
permitted_params = [
|
|
|
|
|
:name,
|
2024-08-16 01:20:54 +03:00
|
|
|
:external_id,
|
2025-05-26 13:32:32 +03:00
|
|
|
:shared_link,
|
2024-04-15 19:36:16 +03:00
|
|
|
{
|
2026-02-20 12:27:39 +02:00
|
|
|
submitters: [%i[name uuid is_requester invite_by_uuid invite_via_field_uuid
|
|
|
|
|
optional_invite_by_uuid linked_to_uuid email order]],
|
2024-04-15 19:36:16 +03:00
|
|
|
fields: [[:uuid, :submitter_uuid, :name, :type,
|
|
|
|
|
:required, :readonly, :default_value,
|
2025-07-27 10:27:12 +03:00
|
|
|
:title, :description, :prefillable,
|
2024-04-15 19:36:16 +03:00
|
|
|
{ preferences: {},
|
2025-08-23 20:11:10 +03:00
|
|
|
default_value: [],
|
2024-11-27 21:47:53 +02:00
|
|
|
conditions: [%i[field_uuid value action operation]],
|
2024-04-15 19:36:16 +03:00
|
|
|
options: [%i[value uuid]],
|
2025-08-20 18:19:23 +03:00
|
|
|
validation: %i[message pattern min max step],
|
2026-02-12 09:22:08 +02:00
|
|
|
areas: [%i[uuid x y w h cell_w attachment_uuid option_uuid page]] }]]
|
2024-04-15 19:36:16 +03:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
2024-02-06 20:07:15 +02:00
|
|
|
if params.key?(:template)
|
2024-04-15 19:36:16 +03:00
|
|
|
params.require(:template).permit(permitted_params)
|
2024-02-06 20:07:15 +02:00
|
|
|
else
|
2024-04-15 19:36:16 +03:00
|
|
|
params.permit(permitted_params)
|
2024-02-06 20:07:15 +02:00
|
|
|
end
|
2023-05-31 23:19:20 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|