add more api endpoints
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Submitters
|
||||
module SerializeForApi
|
||||
module_function
|
||||
|
||||
def call(submitter, with_template: false, with_events: false)
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter],
|
||||
associations: [documents_attachments: :blob, attachments_attachments: :blob]
|
||||
).call
|
||||
|
||||
values = SerializeForWebhook.build_values_array(submitter)
|
||||
documents = SerializeForWebhook.build_documents_array(submitter)
|
||||
|
||||
submitter_name = (submitter.submission.template_submitters ||
|
||||
submitter.submission.template.submitters).find { |e| e['uuid'] == submitter.uuid }['name']
|
||||
|
||||
serialize_params = {
|
||||
include: {},
|
||||
only: %i[id slug uuid name email phone completed_at
|
||||
opened_at sent_at created_at updated_at]
|
||||
}
|
||||
|
||||
serialize_params[:include][:template] = { only: %i[id name created_at updated_at] } if with_template
|
||||
|
||||
if with_events
|
||||
serialize_params[:include][:submission_events] =
|
||||
{ as: :events, only: %i[id submitter_id event_type event_timestamp] }
|
||||
end
|
||||
|
||||
submitter.as_json(serialize_params)
|
||||
.merge('values' => values,
|
||||
'documents' => documents,
|
||||
'role' => submitter_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,6 +5,11 @@ module Submitters
|
||||
module_function
|
||||
|
||||
def call(submitter)
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter],
|
||||
associations: [documents_attachments: :blob, attachments_attachments: :blob]
|
||||
).call
|
||||
|
||||
values = build_values_array(submitter)
|
||||
documents = build_documents_array(submitter)
|
||||
|
||||
@@ -21,7 +26,7 @@ module Submitters
|
||||
def build_values_array(submitter)
|
||||
fields_index = (submitter.submission.template_fields ||
|
||||
submitter.submission.template.fields).index_by { |e| e['uuid'] }
|
||||
attachments_index = submitter.attachments.preload(:blob).index_by(&:uuid)
|
||||
attachments_index = submitter.attachments.index_by(&:uuid)
|
||||
submitter_field_counters = Hash.new { 0 }
|
||||
|
||||
submitter.values.map do |uuid, value|
|
||||
@@ -38,7 +43,7 @@ module Submitters
|
||||
end
|
||||
|
||||
def build_documents_array(submitter)
|
||||
submitter.documents.preload(:blob).map do |attachment|
|
||||
submitter.documents.map do |attachment|
|
||||
{ name: attachment.filename.base, url: rails_storage_proxy_url(attachment) }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user