simplify submissions api

This commit is contained in:
Pete Matsyburka
2024-01-28 14:31:16 +02:00
parent c9a1f067ac
commit 89867fdde3
4 changed files with 85 additions and 56 deletions
-2
View File
@@ -36,8 +36,6 @@ module Params
message += " in `#{@current_path}`." if @current_path.present?
raise InvalidParameterError, message unless dry_run?
Rollbar.error(message) if defined?(Rollbar)
end
def required(params, keys, message: nil)
+54 -27
View File
@@ -2,13 +2,16 @@
module Params
class SubmissionCreateValidator < BaseValidator
# rubocop:disable Metrics
def call
if params[:submission].blank? && (params[:emails].present? || params[:email].present?)
validate_creation_from_emails(params)
elsif params[:submitters].present?
validate_creation_from_submitters(params)
else
validate_creation_from_submission(params)
end
true
end
def validate_creation_from_emails(params)
@@ -25,8 +28,56 @@ module Params
required(message_params, :body)
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
def validate_creation_from_submission(params)
@@ -56,32 +107,8 @@ module Params
end
in_path_each(params, %i[submission submitters]) do |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, %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
validate_submitter(submitter_params)
end
true
end
# rubocop:enable Metrics
end
end