Files
docuseal/app/controllers/start_form_controller.rb
T

176 lines
6.2 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
2023-05-31 23:19:20 +03:00
class StartFormController < ApplicationController
layout 'form'
2023-05-21 17:59:36 +03:00
skip_before_action :authenticate_user!
2023-09-17 00:45:57 +03:00
skip_authorization_check
2023-05-21 17:59:36 +03:00
2024-01-09 01:25:11 +02:00
around_action :with_browser_locale, only: %i[show completed]
2024-11-02 09:40:17 +02:00
before_action :maybe_redirect_com, only: %i[show completed]
2025-06-01 15:16:00 +03:00
before_action :load_resubmit_submitter, only: :update
2023-05-31 23:19:20 +03:00
before_action :load_template
2025-06-01 15:16:00 +03:00
before_action :authorize_start!, only: :update
2023-05-21 17:59:36 +03:00
def show
2025-05-26 13:32:32 +03:00
raise ActionController::RoutingError, I18n.t('not_found') if @template.preferences['require_phone_2fa']
2025-04-08 15:57:19 +03:00
2025-05-26 13:32:32 +03:00
if @template.shared_link?
@submitter = @template.submissions.new(account_id: @template.account_id)
.submitters.new(account_id: @template.account_id,
uuid: (filter_undefined_submitters(@template).first ||
@template.submitters.first)['uuid'])
else
2025-06-01 12:19:13 +03:00
Rollbar.warning("Not shared template: #{@template.id}") if defined?(Rollbar)
return render :private if current_user && current_ability.can?(:read, @template)
2025-05-26 13:32:32 +03:00
raise ActionController::RoutingError, I18n.t('not_found')
end
2023-05-21 17:59:36 +03:00
end
def update
2024-08-21 09:22:59 +03:00
@submitter = find_or_initialize_submitter(@template, submitter_params)
2023-05-21 17:59:36 +03:00
2023-06-13 00:29:07 +03:00
if @submitter.completed_at?
2023-06-30 01:26:13 +03:00
redirect_to start_form_completed_path(@template.slug, email: submitter_params[:email])
2023-05-21 17:59:36 +03:00
else
2024-09-12 17:31:37 +03:00
if filter_undefined_submitters(@template).size > 1 && @submitter.new_record?
2025-03-11 15:58:32 +02:00
@error_message = multiple_submitters_error_message
2023-05-21 17:59:36 +03:00
2023-12-03 00:06:03 +02:00
return render :show
end
2024-09-08 22:37:58 +03:00
if (is_new_record = @submitter.new_record?)
assign_submission_attributes(@submitter, @template)
2023-06-13 00:29:07 +03:00
2024-09-08 22:37:58 +03:00
Submissions::AssignDefinedSubmitters.call(@submitter.submission)
2025-02-17 12:46:44 +02:00
else
@submitter.assign_attributes(ip: request.remote_ip, ua: request.user_agent)
2024-09-08 22:37:58 +03:00
end
2024-05-17 13:28:10 +03:00
2023-06-13 00:29:07 +03:00
if @submitter.save
2024-06-22 10:36:32 +03:00
if is_new_record
2025-05-12 18:23:17 +03:00
enqueue_submission_create_webhooks(@submitter)
2025-06-05 13:16:55 +03:00
SearchEntries.enqueue_reindex(@submitter)
2025-05-12 18:23:17 +03:00
if @submitter.submission.expire_at?
ProcessSubmissionExpiredJob.perform_at(@submitter.submission.expire_at,
'submission_id' => @submitter.submission_id)
end
2024-06-22 10:36:32 +03:00
end
2024-05-17 13:28:10 +03:00
2023-06-13 00:29:07 +03:00
redirect_to submit_form_path(@submitter.slug)
2023-05-21 17:59:36 +03:00
else
render :show
end
end
end
def completed
2025-06-01 18:06:17 +03:00
return redirect_to start_form_path(@template.slug) if !@template.shared_link? || @template.archived_at?
2023-08-16 22:57:59 +03:00
@submitter = Submitter.where(submission: @template.submissions)
.where.not(completed_at: nil)
.find_by!(email: params[:email])
2023-05-21 17:59:36 +03:00
end
private
2025-06-01 15:16:00 +03:00
def load_resubmit_submitter
@resubmit_submitter =
if params[:resubmit].present? && !params[:resubmit].in?([true, 'true'])
Submitter.find_by(slug: params[:resubmit])
end
end
def authorize_start!
return redirect_to start_form_path(@template.slug) if @template.archived_at?
return if @resubmit_submitter
return if @template.shared_link? || (current_user && current_ability.can?(:read, @template))
Rollbar.warning("Not shared template: #{@template.id}") if defined?(Rollbar)
redirect_to start_form_path(@template.slug)
end
2025-05-12 18:23:17 +03:00
def enqueue_submission_create_webhooks(submitter)
WebhookUrls.for_account_id(submitter.account_id, 'submission.created').each do |webhook_url|
SendSubmissionCreatedWebhookRequestJob.perform_async('submission_id' => submitter.submission_id,
'webhook_url_id' => webhook_url.id)
end
end
2024-08-21 09:22:59 +03:00
def find_or_initialize_submitter(template, submitter_params)
2025-06-01 15:16:00 +03:00
Submitter
.where(submission: template.submissions.where(expire_at: Time.current..)
.or(template.submissions.where(expire_at: nil)).where(archived_at: nil))
.order(id: :desc)
.where(declined_at: nil)
.where(external_id: nil)
.where(ip: [nil, request.remote_ip])
.then { |rel| params[:resubmit].present? || params[:selfsign].present? ? rel.where(completed_at: nil) : rel }
.find_or_initialize_by(email: submitter_params[:email], **submitter_params.compact_blank)
2024-08-21 09:22:59 +03:00
end
2023-12-03 00:06:03 +02:00
def assign_submission_attributes(submitter, template)
submitter.assign_attributes(
2024-09-10 09:44:43 +03:00
uuid: (filter_undefined_submitters(template).first || @template.submitters.first)['uuid'],
2023-12-03 00:06:03 +02:00
ip: request.remote_ip,
ua: request.user_agent,
2025-06-01 15:16:00 +03:00
values: @resubmit_submitter&.preferences&.fetch('default_values', nil) || {},
preferences: @resubmit_submitter&.preferences.presence || { 'send_email' => true },
metadata: @resubmit_submitter&.metadata.presence || {}
2023-12-03 00:06:03 +02:00
)
2023-12-29 18:54:48 +02:00
if submitter.values.present?
2025-06-01 15:16:00 +03:00
@resubmit_submitter.attachments.each do |attachment|
2023-12-29 18:54:48 +02:00
submitter.attachments << attachment.dup if submitter.values.value?(attachment.uuid)
end
end
2023-12-03 00:06:03 +02:00
submitter.submission ||= Submission.new(template:,
2024-02-03 14:39:28 +02:00
account_id: template.account_id,
2023-12-03 00:06:03 +02:00
template_submitters: template.submitters,
2025-05-10 13:03:52 +03:00
expire_at: Templates.build_default_expire_at(template),
2024-09-08 22:37:58 +03:00
submitters: [submitter],
2023-12-03 00:06:03 +02:00
source: :link)
2024-08-16 10:19:00 +03:00
submitter.account_id = submitter.submission.account_id
2023-12-03 00:06:03 +02:00
submitter
end
2024-09-08 22:37:58 +03:00
def filter_undefined_submitters(template)
2025-05-31 15:15:25 +03:00
Templates.filter_undefined_submitters(template.submitters)
2024-08-27 15:31:06 +03:00
end
2023-06-13 00:29:07 +03:00
def submitter_params
2025-06-01 15:16:00 +03:00
return current_user.slice(:email) if params[:selfsign]
return @resubmit_submitter.slice(:name, :phone, :email) if @resubmit_submitter.present?
2023-11-06 13:45:12 +02:00
params.require(:submitter).permit(:email, :phone, :name).tap do |attrs|
2023-09-17 09:27:41 +03:00
attrs[:email] = Submissions.normalize_email(attrs[:email])
end
2023-05-21 17:59:36 +03:00
end
2023-05-31 23:19:20 +03:00
def load_template
2025-06-01 15:16:00 +03:00
@template =
if @resubmit_submitter
@resubmit_submitter.template
else
Template.find_by!(slug: params[:slug] || params[:start_form_slug])
end
2023-05-21 17:59:36 +03:00
end
2025-03-11 15:58:32 +02:00
def multiple_submitters_error_message
if current_user&.account_id == @template.account_id
2025-03-18 17:33:30 +02:00
helpers.t('this_submission_has_multiple_signers_which_prevents_the_use_of_a_sharing_link_html')
2025-03-11 15:58:32 +02:00
else
I18n.t('not_found')
end
end
2023-05-21 17:59:36 +03:00
end