adjust create submission resp
This commit is contained in:
@@ -2,38 +2,44 @@
|
||||
|
||||
module Submitters
|
||||
module SerializeForApi
|
||||
SERIALIZE_PARAMS = {
|
||||
only: %i[id slug uuid name email phone completed_at external_id
|
||||
submission_id metadata opened_at sent_at created_at updated_at],
|
||||
methods: %i[status application_key]
|
||||
}.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter, with_template: false, with_events: false)
|
||||
def call(submitter, with_template: false, with_events: false, with_documents: true, with_urls: false)
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [submitter],
|
||||
associations: [documents_attachments: :blob, attachments_attachments: :blob]
|
||||
associations: if with_documents
|
||||
[documents_attachments: :blob, attachments_attachments: :blob]
|
||||
else
|
||||
[attachments_attachments: :blob]
|
||||
end
|
||||
).call
|
||||
|
||||
values = SerializeForWebhook.build_values_array(submitter)
|
||||
documents = SerializeForWebhook.build_documents_array(submitter)
|
||||
additional_attrs = {}
|
||||
|
||||
submitter_name = (submitter.submission.template_submitters ||
|
||||
submitter.submission.template.submitters).find { |e| e['uuid'] == submitter.uuid }['name']
|
||||
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')
|
||||
|
||||
serialize_params = {
|
||||
include: {},
|
||||
only: %i[id slug uuid name email phone completed_at external_id
|
||||
submission_id metadata opened_at sent_at created_at updated_at],
|
||||
methods: %i[status application_key]
|
||||
}
|
||||
additional_attrs['role'] =
|
||||
(submitter.submission.template_submitters ||
|
||||
submitter.submission.template.submitters).find { |e| e['uuid'] == submitter.uuid }['name']
|
||||
|
||||
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] }
|
||||
if with_urls
|
||||
additional_attrs['embed_src'] =
|
||||
Rails.application.routes.url_helpers.submit_form_url(slug: submitter.slug, **Docuseal.default_url_options)
|
||||
end
|
||||
|
||||
submitter.as_json(serialize_params)
|
||||
.merge('values' => values,
|
||||
'documents' => documents,
|
||||
'role' => submitter_name)
|
||||
include_params = {}
|
||||
include_params[:template] = { only: %i[id name created_at updated_at] } if with_template
|
||||
include_params[:submission_events] = { only: %i[id submitter_id event_type event_timestamp] } if with_events
|
||||
|
||||
submitter.as_json(SERIALIZE_PARAMS.merge(include: include_params)).merge(additional_attrs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,6 +2,12 @@
|
||||
|
||||
module Submitters
|
||||
module SerializeForWebhook
|
||||
SERIALIZE_PARAMS = {
|
||||
methods: %i[status application_key],
|
||||
only: %i[id submission_id email phone name ua ip sent_at opened_at
|
||||
completed_at created_at updated_at external_id metadata]
|
||||
}.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter)
|
||||
@@ -16,10 +22,7 @@ module Submitters
|
||||
submitter_name = (submitter.submission.template_submitters ||
|
||||
submitter.submission.template.submitters).find { |e| e['uuid'] == submitter.uuid }['name']
|
||||
|
||||
submitter.as_json(methods: %i[status application_key],
|
||||
only: %i[id submission_id email phone name ua ip sent_at opened_at
|
||||
completed_at created_at updated_at external_id metadata])
|
||||
.except('uuid', 'values', 'slug')
|
||||
submitter.as_json(SERIALIZE_PARAMS)
|
||||
.merge('role' => submitter_name,
|
||||
'preferences' => submitter.preferences.except('default_values'),
|
||||
'values' => values,
|
||||
|
||||
Reference in New Issue
Block a user