Files
docuseal/lib/templates.rb
T

161 lines
4.7 KiB
Ruby
Raw Normal View History

2023-05-31 23:19:20 +03:00
# frozen_string_literal: true
module Templates
2026-04-26 15:13:19 +03:00
COLOR_REGEXP = /\A(#(?:[0-9a-f]{3}|[0-9a-f]{6})|[a-z]+)\z/i
2026-05-28 10:06:47 +03:00
TEMPLATE_BUILDER_FIELDS = %i[id author_id folder_id external_id name slug
schema fields submitters variables_schema preferences
shared_link source archived_at created_at updated_at].freeze
2025-05-10 13:03:52 +03:00
EXPIRATION_DURATIONS = {
one_day: 1.day,
two_days: 2.days,
three_days: 3.days,
four_days: 4.days,
five_days: 5.days,
six_days: 6.days,
seven_days: 7.days,
eight_days: 8.days,
nine_days: 9.days,
ten_days: 10.days,
two_weeks: 14.days,
three_weeks: 21.days,
four_weeks: 28.days,
one_month: 1.month,
two_months: 2.months,
three_months: 3.months
}.with_indifferent_access.freeze
2023-06-20 22:38:38 +03:00
module_function
2023-07-26 09:58:50 +03:00
def build_field_areas_index(fields)
2023-06-20 22:38:38 +03:00
hash = {}
2023-07-26 09:58:50 +03:00
fields.each do |field|
2023-06-20 22:38:38 +03:00
(field['areas'] || []).each do |area|
hash[area['attachment_uuid']] ||= {}
acc = (hash[area['attachment_uuid']][area['page']] ||= [])
acc << [area, field]
end
end
hash
end
2023-09-15 02:11:48 +03:00
2025-08-26 12:29:51 +03:00
def maybe_assign_access(_template)
nil
end
2026-06-24 14:17:17 +03:00
def shared(current_user)
account = current_user.account
return Template.none if Docuseal.multitenant? ? !account.testing? : !account.linked_account_account
shared_account_ids = [current_user.account_id]
shared_account_ids << TemplateSharing::ALL_ID if !Docuseal.multitenant? && !account.testing?
exists_access = TemplateAccess.where(TemplateAccess.arel_table[:template_id].eq(Template.arel_table[:id]))
.select(1).arel.exists
Template.where(id: TemplateSharing.where(account_id: shared_account_ids).select(:template_id))
.where.not(exists_access)
end
2025-06-05 13:16:55 +03:00
def search(current_user, templates, keyword)
2025-06-21 22:52:10 +03:00
if Docuseal.fulltext_search?
2025-06-05 13:16:55 +03:00
fulltext_search(current_user, templates, keyword)
else
plain_search(templates, keyword)
end
end
2026-06-25 20:26:33 +03:00
def search_shared(current_user, templates, keyword)
return templates if keyword.blank?
if Docuseal.fulltext_search?
templates.where(
id: SearchEntry.where(record_type: 'Template')
.where(account_id: current_user.account.linked_account_account&.account_id)
.where(*SearchEntries.build_tsquery(keyword))
.select(:record_id)
)
else
plain_search(templates, keyword)
end
end
2025-06-05 13:16:55 +03:00
def plain_search(templates, keyword)
2023-09-15 02:11:48 +03:00
return templates if keyword.blank?
2026-02-14 11:05:05 +02:00
sanitized = ActiveRecord::Base.sanitize_sql_like(keyword.downcase)
templates.where(Template.arel_table[:name].lower.matches("%#{sanitized}%"))
2023-09-15 02:11:48 +03:00
end
2024-09-12 17:31:37 +03:00
2025-06-05 13:16:55 +03:00
def fulltext_search(current_user, templates, keyword)
return templates if keyword.blank?
templates.where(
id: SearchEntry.where(record_type: 'Template')
2026-06-25 20:26:33 +03:00
.where(account_id: current_user.account_id)
2025-06-05 13:16:55 +03:00
.where(*SearchEntries.build_tsquery(keyword))
.select(:record_id)
)
end
2025-05-31 15:15:25 +03:00
def filter_undefined_submitters(template_submitters)
template_submitters.to_a.select do |item|
2024-12-19 17:17:34 +02:00
item['invite_by_uuid'].blank? && item['optional_invite_by_uuid'].blank? &&
2026-02-20 12:27:39 +02:00
item['invite_via_field_uuid'].blank? &&
2024-12-19 17:17:34 +02:00
item['linked_to_uuid'].blank? && item['is_requester'].blank? && item['email'].blank?
2024-09-12 17:31:37 +03:00
end
end
2025-05-10 13:03:52 +03:00
def build_default_expire_at(template)
default_expire_at_duration = template.preferences['default_expire_at_duration'].presence
default_expire_at = template.preferences['default_expire_at'].presence
return if default_expire_at_duration.blank?
if default_expire_at_duration == 'specified_date' && default_expire_at.present?
Time.zone.parse(default_expire_at)
elsif EXPIRATION_DURATIONS[default_expire_at_duration]
Time.current + EXPIRATION_DURATIONS[default_expire_at_duration]
end
end
2026-05-28 10:06:47 +03:00
2026-06-15 12:05:50 +03:00
def cleanup_document_attachment(attachment)
return unless attachment.blob.content_type == 'application/pdf'
io = StringIO.new
Pdfium::Document.open_bytes(attachment.blob.download) do |doc|
doc.cleanup
doc.save(io)
end
blob = ActiveStorage::Blob.create_and_upload!(
io: io.tap(&:rewind),
filename: attachment.blob.filename.to_s,
content_type: 'application/pdf'
)
attachment.update!(blob_id: blob.id)
attachment
end
2026-05-28 10:06:47 +03:00
def serialize_for_builder(template)
data = template.as_json(only: TEMPLATE_BUILDER_FIELDS)
data['documents'] = template.schema_documents.preload(:blob, { preview_images_attachments: :blob }).as_json(
only: %i[id uuid],
methods: %i[metadata signed_key],
include: { preview_images: { only: %i[id], methods: %i[url metadata filename] } }
)
data
end
2023-05-31 23:19:20 +03:00
end