simplify submissions api
This commit is contained in:
@@ -96,7 +96,8 @@ module Api
|
|||||||
def create_submissions(template, params)
|
def create_submissions(template, params)
|
||||||
is_send_email = !params[:send_email].in?(['false', false])
|
is_send_email = !params[:send_email].in?(['false', false])
|
||||||
|
|
||||||
if (emails = (params[:emails] || params[:email]).presence) && params[:submission].blank?
|
if (emails = (params[:emails] || params[:email]).presence) &&
|
||||||
|
(params[:submission].blank? && params[:submitters].blank?)
|
||||||
Submissions.create_from_emails(template:,
|
Submissions.create_from_emails(template:,
|
||||||
user: current_user,
|
user: current_user,
|
||||||
source: :api,
|
source: :api,
|
||||||
@@ -140,20 +141,27 @@ module Api
|
|||||||
end
|
end
|
||||||
|
|
||||||
def submissions_params
|
def submissions_params
|
||||||
key = params.key?(:submission) ? :submission : :submissions
|
permitted_attrs = [
|
||||||
|
:send_email, :send_sms, :bcc_completed, :completed_redirect_url,
|
||||||
|
{
|
||||||
|
message: %i[subject body],
|
||||||
|
submitters: [[:send_email, :send_sms, :completed_redirect_url, :uuid, :name, :email, :role,
|
||||||
|
:completed, :phone, :application_key,
|
||||||
|
{ values: {}, readonly_fields: [], message: %i[subject body],
|
||||||
|
fields: [%i[name default_value title description
|
||||||
|
readonly validation_pattern invalid_message]] }]]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
params.permit(
|
if params.key?(:submitters)
|
||||||
key => [
|
params.permit(*permitted_attrs)
|
||||||
[:send_email, :send_sms, :bcc_completed, :completed_redirect_url, {
|
else
|
||||||
message: %i[subject body],
|
key = params.key?(:submission) ? :submission : :submissions
|
||||||
submitters: [[:send_email, :send_sms, :completed_redirect_url, :uuid, :name, :email, :role,
|
|
||||||
:completed, :phone, :application_key,
|
params.permit(
|
||||||
{ values: {}, readonly_fields: [], message: %i[subject body],
|
key => [permitted_attrs]
|
||||||
fields: [%i[name default_value title description
|
).fetch(key, [])
|
||||||
readonly validation_pattern invalid_message]] }]]
|
end
|
||||||
}]
|
|
||||||
]
|
|
||||||
).fetch(key, [])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -35,20 +35,16 @@
|
|||||||
--header 'X-Auth-Token: <%= current_user.access_token.token %>' \
|
--header 'X-Auth-Token: <%= current_user.access_token.token %>' \
|
||||||
--data-raw '{
|
--data-raw '{
|
||||||
"template_id": <%= current_account.templates.last&.id || 1 %>,
|
"template_id": <%= current_account.templates.last&.id || 1 %>,
|
||||||
"submission": [
|
"submitters": [
|
||||||
{
|
{
|
||||||
"submitters": [
|
"name": "John Doe",
|
||||||
{
|
"role": "<%= current_account.templates.last ? current_account.templates.last.submitters.first['name'] : 'First Party' %>",
|
||||||
"name": "John Doe",
|
"email": "<%= current_user.email.sub('@', '+test@') %>",
|
||||||
"role": "<%= current_account.templates.last ? current_account.templates.last.submitters.first['name'] : 'First Party' %>",
|
"values": {
|
||||||
"email": "<%= current_user.email.sub('@', '+test@') %>",
|
"Form Text Field Name": "Default Value"
|
||||||
"values": {
|
}
|
||||||
"Form Text Field Name": "Default Value"
|
},
|
||||||
}
|
{ "role": "Second Submitter", "email": "<%= current_user.email.sub('@', '+test2@') %>" }
|
||||||
},
|
|
||||||
{ "role": "Second Submitter", "email": "<%= current_user.email.sub('@', '+test2@') %>" }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}'<% end.to_str %>
|
}'<% end.to_str %>
|
||||||
<span class="top-0 right-0 absolute">
|
<span class="top-0 right-0 absolute">
|
||||||
|
|||||||
@@ -36,8 +36,6 @@ module Params
|
|||||||
message += " in `#{@current_path}`." if @current_path.present?
|
message += " in `#{@current_path}`." if @current_path.present?
|
||||||
|
|
||||||
raise InvalidParameterError, message unless dry_run?
|
raise InvalidParameterError, message unless dry_run?
|
||||||
|
|
||||||
Rollbar.error(message) if defined?(Rollbar)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def required(params, keys, message: nil)
|
def required(params, keys, message: nil)
|
||||||
|
|||||||
@@ -2,13 +2,16 @@
|
|||||||
|
|
||||||
module Params
|
module Params
|
||||||
class SubmissionCreateValidator < BaseValidator
|
class SubmissionCreateValidator < BaseValidator
|
||||||
# rubocop:disable Metrics
|
|
||||||
def call
|
def call
|
||||||
if params[:submission].blank? && (params[:emails].present? || params[:email].present?)
|
if params[:submission].blank? && (params[:emails].present? || params[:email].present?)
|
||||||
validate_creation_from_emails(params)
|
validate_creation_from_emails(params)
|
||||||
|
elsif params[:submitters].present?
|
||||||
|
validate_creation_from_submitters(params)
|
||||||
else
|
else
|
||||||
validate_creation_from_submission(params)
|
validate_creation_from_submission(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_creation_from_emails(params)
|
def validate_creation_from_emails(params)
|
||||||
@@ -25,8 +28,56 @@ module Params
|
|||||||
|
|
||||||
required(message_params, :body)
|
required(message_params, :body)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
true
|
def validate_creation_from_submitters(params)
|
||||||
|
required(params, :template_id)
|
||||||
|
required(params, :submitters)
|
||||||
|
|
||||||
|
boolean(params, :send_email)
|
||||||
|
boolean(params, :send_sms)
|
||||||
|
type(params, :order, String)
|
||||||
|
type(params, :completed_redirect_url, String)
|
||||||
|
type(params, :bcc_completed, String)
|
||||||
|
type(params, :message, Hash)
|
||||||
|
|
||||||
|
in_path(params, :message) do |message_params|
|
||||||
|
type(message_params, :subject, String)
|
||||||
|
type(message_params, :body, String)
|
||||||
|
|
||||||
|
required(message_params, :body)
|
||||||
|
end
|
||||||
|
|
||||||
|
value_in(params, :order, %w[preserved random], allow_nil: true)
|
||||||
|
|
||||||
|
in_path_each(params, :submitters) do |submitter_params|
|
||||||
|
validate_submitter(submitter_params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_submitter(submitter_params)
|
||||||
|
required(submitter_params, %i[email phone name])
|
||||||
|
|
||||||
|
type(submitter_params, :name, String)
|
||||||
|
type(submitter_params, :email, String)
|
||||||
|
format(submitter_params, :email, /@/, message: 'email is invalid')
|
||||||
|
type(submitter_params, :phone, String)
|
||||||
|
format(submitter_params, :phone, /\A\+\d+\z/,
|
||||||
|
message: 'phone should start with +<country code> and contain only digits')
|
||||||
|
type(submitter_params, :values, Hash)
|
||||||
|
boolean(submitter_params, :send_email)
|
||||||
|
boolean(submitter_params, :send_sms)
|
||||||
|
type(submitter_params, :completed_redirect_url, String)
|
||||||
|
type(submitter_params, :fields, Array)
|
||||||
|
|
||||||
|
in_path_each(submitter_params, :fields) do |field_params|
|
||||||
|
required(field_params, :name)
|
||||||
|
|
||||||
|
type(field_params, :name, String)
|
||||||
|
type(field_params, :validation_pattern, String)
|
||||||
|
type(field_params, :invalid_message, String)
|
||||||
|
boolean(field_params, :readonly)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_creation_from_submission(params)
|
def validate_creation_from_submission(params)
|
||||||
@@ -56,32 +107,8 @@ module Params
|
|||||||
end
|
end
|
||||||
|
|
||||||
in_path_each(params, %i[submission submitters]) do |submitter_params|
|
in_path_each(params, %i[submission submitters]) do |submitter_params|
|
||||||
required(submitter_params, %i[email phone name])
|
validate_submitter(submitter_params)
|
||||||
|
|
||||||
type(submitter_params, :name, String)
|
|
||||||
type(submitter_params, :email, String)
|
|
||||||
format(submitter_params, :email, /@/, message: 'email is invalid')
|
|
||||||
type(submitter_params, :phone, String)
|
|
||||||
format(submitter_params, :phone, /\A\+\d+\z/,
|
|
||||||
message: 'phone should start with +<country code> and contain only digits')
|
|
||||||
type(submitter_params, :values, Hash)
|
|
||||||
boolean(submitter_params, :send_email)
|
|
||||||
boolean(submitter_params, :send_sms)
|
|
||||||
type(submitter_params, :completed_redirect_url, String)
|
|
||||||
type(submitter_params, :fields, Array)
|
|
||||||
|
|
||||||
in_path_each(submitter_params, %i[fields]) do |field_params|
|
|
||||||
required(field_params, :name)
|
|
||||||
|
|
||||||
type(field_params, :name, String)
|
|
||||||
type(field_params, :validation_pattern, String)
|
|
||||||
type(field_params, :invalid_message, String)
|
|
||||||
boolean(field_params, :readonly)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
# rubocop:enable Metrics
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user