allow to set default field values via api
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
module Api
|
||||
class SubmissionsController < ApiBaseController
|
||||
UnknownFieldName = Class.new(StandardError)
|
||||
UnknownSubmitterName = Class.new(StandardError)
|
||||
|
||||
def create
|
||||
template = current_account.templates.find(params[:template_id])
|
||||
|
||||
@@ -13,28 +16,65 @@ module Api
|
||||
send_email: params[:send_email] != 'false',
|
||||
emails: params[:emails] || params[:email])
|
||||
else
|
||||
submissions_attrs = normalize_submissions_params!(submissions_params[:submission], template)
|
||||
|
||||
Submissions.create_from_submitters(template:,
|
||||
user: current_user,
|
||||
source: :api,
|
||||
send_email: params[:send_email] != 'false',
|
||||
submissions_attrs: submissions_params[:submission])
|
||||
submissions_attrs:)
|
||||
end
|
||||
|
||||
submitters = submissions.flat_map(&:submitters)
|
||||
|
||||
if params[:send_email] != 'false'
|
||||
submitters.each do |submitter|
|
||||
SubmitterMailer.invitation_email(submitter, message: params[:message]).deliver_later!
|
||||
end
|
||||
end
|
||||
send_invitation_emails(submitters) if params[:send_email] != 'false'
|
||||
|
||||
render json: submitters
|
||||
rescue UnknownFieldName, UnknownSubmitterName => e
|
||||
render json: { error: e.message }, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_invitation_emails(submitters)
|
||||
submitters.each do |submitter|
|
||||
SubmitterMailer.invitation_email(submitter, message: params[:message]).deliver_later!
|
||||
end
|
||||
end
|
||||
|
||||
def submissions_params
|
||||
params.permit(submission: [{ submitters: [%i[uuid name email]] }])
|
||||
params.permit(submission: [{ submitters: [[:uuid, :name, :email, { values: {} }]] }])
|
||||
end
|
||||
|
||||
def normalize_submissions_params!(submissions_params, template)
|
||||
submissions_params.each do |submission|
|
||||
submission[:submitters].each_with_index do |submitter, index|
|
||||
next if submitter[:values].blank?
|
||||
|
||||
submitter[:values] =
|
||||
normalize_submitter_values(template,
|
||||
submitter[:values], submitter[:name] || template.submitters[index]['name'])
|
||||
end
|
||||
end
|
||||
|
||||
submissions_params
|
||||
end
|
||||
|
||||
def normalize_submitter_values(template, values, submitter_name)
|
||||
submitter =
|
||||
template.submitters.find { |e| e['name'] == submitter_name } ||
|
||||
raise(UnknownSubmitterName, "Unknown submitter: #{submitter_name}")
|
||||
|
||||
fields = template.fields.select { |e| e['submitter_uuid'] == submitter['uuid'] }
|
||||
|
||||
fields_uuid_index = fields.index_by { |e| e['uuid'] }
|
||||
fields_name_index = fields.index_by { |e| e['name'] }
|
||||
|
||||
values.transform_keys do |key|
|
||||
next key if fields_uuid_index[key].present?
|
||||
|
||||
fields_name_index[key]&.dig('uuid') || raise(UnknownFieldName, "Unknown field: #{key}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,13 +2,17 @@
|
||||
|
||||
module Api
|
||||
class TemplatesController < ApiBaseController
|
||||
before_action :load_template, only: %i[show update]
|
||||
|
||||
def index
|
||||
render json: current_account.templates
|
||||
end
|
||||
|
||||
def update
|
||||
@template = current_account.templates.find(params[:id])
|
||||
def show
|
||||
render json: @template.as_json(include: { author: { only: %i[id email first_name last_name] } })
|
||||
end
|
||||
|
||||
def update
|
||||
@template.update!(template_params)
|
||||
|
||||
render :ok
|
||||
@@ -23,5 +27,9 @@ module Api
|
||||
fields: [[:uuid, :submitter_uuid, :name, :type, :required,
|
||||
{ options: [], areas: [%i[x y w h cell_w attachment_uuid page]] }]])
|
||||
end
|
||||
|
||||
def load_template
|
||||
@template = current_account.templates.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
<input type="checkbox">
|
||||
<div class="collapse-title text-xl font-medium">
|
||||
<div>
|
||||
Request signature, multiple submitters
|
||||
Request signature, multiple submitters with default values
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<div class="badge badge-warning badge-lg">POST</div>
|
||||
@@ -58,7 +58,13 @@
|
||||
"submission": [
|
||||
{
|
||||
"submitters": [
|
||||
{ "name": "<%= current_account.templates.last ? current_account.templates.last.submitters.first['name'] : 'First Submitter' %>", "email": "<%= current_user.email.sub('@', '+test@') %>" },
|
||||
{
|
||||
"name": "<%= current_account.templates.last ? current_account.templates.last.submitters.first['name'] : 'First Submitter' %>",
|
||||
"email": "<%= current_user.email.sub('@', '+test@') %>",
|
||||
"values": {
|
||||
"Form Text Field Name": "Default Value"
|
||||
}
|
||||
},
|
||||
{ "name": "Second Submitter", "email": "<%= current_user.email.sub('@', '+test2@') %>" }
|
||||
]
|
||||
}
|
||||
@@ -71,6 +77,28 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="collapse collapse-plus bg-base-200 px-1">
|
||||
<input type="checkbox">
|
||||
<div class="collapse-title text-xl font-medium">
|
||||
<div>
|
||||
Template details
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<div class="badge badge-info badge-lg">GET</div>
|
||||
<div class="badge badge-primary badge-lg"><%= api_template_path(':id') %></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="collapse-content" style="display: inherit">
|
||||
<div class="mockup-code overflow-hidden">
|
||||
<% text = capture do %>curl '<%= api_template_url(current_account.templates.last) %>' \
|
||||
--header 'X-Auth-Token: <%= current_user.access_token.token %>'<% end.to_str %>
|
||||
<span class="top-0 right-0 absolute">
|
||||
<%= render 'shared/clipboard_copy', icon: 'copy', text:, class: 'btn btn-ghost text-white', icon_class: 'w-6 h-6 text-white', copy_title: 'Copy', copied_title: 'Copied' %>
|
||||
</span>
|
||||
<pre data-prefix="$"><code class="overflow-hidden w-full"><%= text %></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ Rails.application.routes.draw do
|
||||
namespace :api, defaults: { format: :json } do
|
||||
resources :attachments, only: %i[create]
|
||||
resources :submissions, only: %i[create]
|
||||
resources :templates, only: %i[update index] do
|
||||
resources :templates, only: %i[update show index] do
|
||||
resources :submissions, only: %i[create]
|
||||
resources :documents, only: %i[create destroy], controller: 'templates_documents'
|
||||
end
|
||||
|
||||
@@ -38,6 +38,7 @@ module Submissions
|
||||
|
||||
submission.submitters.new(email: submitter_attrs[:email],
|
||||
sent_at: send_email ? Time.current : nil,
|
||||
values: submitter_attrs[:values] || {},
|
||||
uuid:)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user