add fields include option
This commit is contained in:
@@ -77,7 +77,7 @@ module Api
|
||||
|
||||
json = submissions.flat_map do |submission|
|
||||
submission.submitters.map do |s|
|
||||
Submitters::SerializeForApi.call(s, with_documents: false, with_urls: true)
|
||||
Submitters::SerializeForApi.call(s, with_documents: false, with_urls: true, params:)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ module Api
|
||||
)
|
||||
|
||||
render json: {
|
||||
data: submitters.map { |s| Submitters::SerializeForApi.call(s, with_template: true, with_events: true) },
|
||||
data: submitters.map do |s|
|
||||
Submitters::SerializeForApi.call(s, with_template: true, with_events: true, params:)
|
||||
end,
|
||||
pagination: {
|
||||
count: submitters.size,
|
||||
next: submitters.last&.id,
|
||||
@@ -35,7 +37,7 @@ module Api
|
||||
def show
|
||||
Submissions::EnsureResultGenerated.call(@submitter) if @submitter.completed_at?
|
||||
|
||||
render json: Submitters::SerializeForApi.call(@submitter, with_template: true, with_events: true)
|
||||
render json: Submitters::SerializeForApi.call(@submitter, with_template: true, with_events: true, params:)
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -73,7 +75,8 @@ module Api
|
||||
|
||||
render json: Submitters::SerializeForApi.call(@submitter, with_template: false,
|
||||
with_urls: true,
|
||||
with_events: false)
|
||||
with_events: false,
|
||||
params:)
|
||||
end
|
||||
|
||||
def submitter_params
|
||||
|
||||
@@ -21,7 +21,7 @@ module Submissions
|
||||
def call(submission, submitters = nil, params = {})
|
||||
submitters ||= submission.submitters.preload(documents_attachments: :blob, attachments_attachments: :blob)
|
||||
|
||||
serialized_submitters = submitters.map { |submitter| Submitters::SerializeForApi.call(submitter) }
|
||||
serialized_submitters = submitters.map { |submitter| Submitters::SerializeForApi.call(submitter, params:) }
|
||||
|
||||
json = submission.as_json(
|
||||
SERIALIZE_PARAMS.deep_merge(
|
||||
|
||||
@@ -10,7 +10,7 @@ module Submitters
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter, with_template: false, with_events: false, with_documents: true, with_urls: false)
|
||||
def call(submitter, with_template: false, with_events: false, with_documents: true, with_urls: false, params: {})
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter],
|
||||
associations: if with_documents
|
||||
@@ -22,6 +22,10 @@ module Submitters
|
||||
|
||||
additional_attrs = {}
|
||||
|
||||
if params[:include].to_s.include?('fields')
|
||||
additional_attrs['fields'] = SerializeForWebhook.build_fields_array(submitter)
|
||||
end
|
||||
|
||||
additional_attrs['values'] = SerializeForWebhook.build_values_array(submitter)
|
||||
additional_attrs['documents'] = SerializeForWebhook.build_documents_array(submitter) if with_documents
|
||||
additional_attrs['preferences'] = submitter.preferences.except('default_values')
|
||||
|
||||
@@ -61,6 +61,28 @@ module Submitters
|
||||
end
|
||||
end
|
||||
|
||||
def build_fields_array(submitter)
|
||||
fields = submitter.submission.template_fields.presence || submitter.submission.template.fields
|
||||
attachments_index = submitter.attachments.index_by(&:uuid)
|
||||
submitter_field_counters = Hash.new { 0 }
|
||||
|
||||
fields.filter_map do |field|
|
||||
submitter_field_counters[field['type']] += 1
|
||||
|
||||
next if field['submitter_uuid'] != submitter.uuid
|
||||
next if field['type'] == 'heading'
|
||||
|
||||
field_name =
|
||||
field['name'].presence || "#{field['type'].titleize} Field #{submitter_field_counters[field['type']]}"
|
||||
|
||||
next if !submitter.values.key?(field['uuid']) && !submitter.completed_at?
|
||||
|
||||
value = fetch_field_value(field, submitter.values[field['uuid']], attachments_index)
|
||||
|
||||
{ name: field_name, uuid: field['uuid'], value: }
|
||||
end
|
||||
end
|
||||
|
||||
def build_documents_array(submitter)
|
||||
submitter.documents.map do |attachment|
|
||||
{ name: attachment.filename.base, url: rails_storage_proxy_url(attachment) }
|
||||
|
||||
Reference in New Issue
Block a user