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]
|
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)
|
|
|
|
|
.submitters.new(uuid: @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?
|
|
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
@submitter = Submitter.where(submission: @template.submissions.where(archived_at: nil))
|
2023-12-03 00:06:03 +02:00
|
|
|
.order(id: :desc)
|
2023-10-21 13:06:59 +03:00
|
|
|
.then { |rel| params[:resubmit].present? ? rel.where(completed_at: nil) : rel }
|
|
|
|
|
.find_or_initialize_by(**submitter_params.compact_blank)
|
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
|
2023-12-03 00:06:03 +02:00
|
|
|
if @template.submitters.to_a.size > 1 && @submitter.new_record?
|
|
|
|
|
@error_message = 'Not found'
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-12-03 00:06:03 +02:00
|
|
|
return render :show
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assign_submission_attributes(@submitter, @template) if @submitter.new_record?
|
2023-06-13 00:29:07 +03:00
|
|
|
|
2024-05-17 13:28:10 +03:00
|
|
|
is_new_record = @submitter.new_record?
|
|
|
|
|
|
2023-06-13 00:29:07 +03:00
|
|
|
if @submitter.save
|
2024-05-17 13:28:10 +03:00
|
|
|
SendSubmissionCreatedWebhookRequestJob.perform_later(@submitter.submission) if is_new_record
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-12-03 00:06:03 +02:00
|
|
|
def assign_submission_attributes(submitter, template)
|
2023-12-29 18:54:48 +02:00
|
|
|
resubmit_submitter =
|
|
|
|
|
if params[:resubmit].present?
|
|
|
|
|
Submitter.where(submission: @template.submissions).find_by(slug: params[:resubmit])
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-03 00:06:03 +02:00
|
|
|
submitter.assign_attributes(
|
|
|
|
|
uuid: template.submitters.first['uuid'],
|
|
|
|
|
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,
|
|
|
|
|
source: :link)
|
|
|
|
|
|
|
|
|
|
submitter
|
|
|
|
|
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
|