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]
|
2023-05-31 23:19:20 +03:00
|
|
|
before_action :load_template
|
2023-05-21 17:59:36 +03:00
|
|
|
|
|
|
|
|
def show
|
2024-02-03 14:39:28 +02:00
|
|
|
@submitter = @template.submissions.new(account_id: @template.account_id)
|
2024-09-10 09:44:43 +03:00
|
|
|
.submitters.new(uuid: (filter_undefined_submitters(@template).first ||
|
|
|
|
|
@template.submitters.first)['uuid'])
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2024-02-18 19:29:38 +02:00
|
|
|
return redirect_to start_form_path(@template.slug) if @template.archived_at?
|
|
|
|
|
|
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?
|
2024-09-18 19:47:47 +03:00
|
|
|
@error_message = I18n.t('not_found')
|
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
|
2024-11-01 15:35:02 +02:00
|
|
|
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)
|
2024-10-28 20:39:09 +02:00
|
|
|
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
|
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
|
|
|
|
|
|
2024-08-21 09:22:59 +03:00
|
|
|
def find_or_initialize_submitter(template, submitter_params)
|
|
|
|
|
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)
|
2025-01-23 11:39:43 +02:00
|
|
|
.where(external_id: nil)
|
2025-02-17 12:46:44 +02:00
|
|
|
.where(ip: [nil, request.remote_ip])
|
2024-08-21 09:22:59 +03:00
|
|
|
.then { |rel| params[:resubmit].present? ? rel.where(completed_at: nil) : rel }
|
2025-02-17 12:46:44 +02:00
|
|
|
.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)
|
2023-12-29 18:54:48 +02:00
|
|
|
resubmit_submitter =
|
2024-08-27 15:31:06 +03:00
|
|
|
(Submitter.where(submission: template.submissions).find_by(slug: params[:resubmit]) if params[:resubmit].present?)
|
2023-12-29 18:54:48 +02:00
|
|
|
|
2023-12-03 00:06:03 +02:00
|
|
|
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,
|
2023-12-29 18:54:48 +02:00
|
|
|
values: resubmit_submitter&.preferences&.fetch('default_values', nil) || {},
|
2024-02-01 23:29:27 +02:00
|
|
|
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?
|
|
|
|
|
resubmit_submitter.attachments.each do |attachment|
|
|
|
|
|
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,
|
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)
|
2024-09-12 17:31:37 +03:00
|
|
|
Templates.filter_undefined_submitters(template)
|
2024-08-27 15:31:06 +03:00
|
|
|
end
|
|
|
|
|
|
2023-06-13 00:29:07 +03:00
|
|
|
def submitter_params
|
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
|
2023-06-30 01:26:13 +03:00
|
|
|
slug = params[:slug] || params[:start_form_slug]
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
@template = Template.find_by!(slug:)
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
end
|