Skip to content

Commit

Permalink
allow to set default field values via api
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBTurchyn committed Aug 11, 2023
1 parent d875477 commit c57148e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
54 changes: 47 additions & 7 deletions app/controllers/api/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -13,28 +16,65 @@ def create
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
12 changes: 10 additions & 2 deletions app/controllers/api/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,5 +27,9 @@ def template_params
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
32 changes: 30 additions & 2 deletions app/views/api_settings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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@') %>" }
]
}
Expand All @@ -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>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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
Expand Down
1 change: 1 addition & 0 deletions lib/submissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def create_from_submitters(template:, user:, submissions_attrs:, source:, send_e

submission.submitters.new(email: submitter_attrs[:email],
sent_at: send_email ? Time.current : nil,
values: submitter_attrs[:values] || {},
uuid:)
end

Expand Down

0 comments on commit c57148e

Please sign in to comment.