2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
class SubmitFormController < ApplicationController
|
|
|
|
|
layout 'form'
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-01-14 12:11:13 +02:00
|
|
|
around_action :with_browser_locale, only: %i[show completed success]
|
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-06-15 15:55:09 +03:00
|
|
|
CONFIG_KEYS = [].freeze
|
2023-10-22 01:54:20 +03:00
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def show
|
2023-10-22 01:54:20 +03:00
|
|
|
@submitter = Submitter.find_by!(slug: params[:slug])
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-07-05 20:41:21 +03:00
|
|
|
return redirect_to submit_form_completed_path(@submitter.slug) if @submitter.completed_at?
|
2024-06-15 15:55:09 +03:00
|
|
|
return render :archived if @submitter.submission.template.archived_at? || @submitter.submission.archived_at?
|
2024-08-01 16:43:55 +03:00
|
|
|
return render :expired if @submitter.submission.expired?
|
2023-06-24 12:27:34 +03:00
|
|
|
|
2024-07-24 09:13:13 +03:00
|
|
|
Submitters.preload_with_pages(@submitter)
|
2023-10-22 01:54:20 +03:00
|
|
|
|
2023-09-22 21:50:39 +03:00
|
|
|
Submitters::MaybeUpdateDefaultValues.call(@submitter, current_user)
|
|
|
|
|
|
2024-06-11 18:19:21 +03:00
|
|
|
@attachments_index = ActiveStorage::Attachment.where(record: @submitter.submission.submitters, name: :attachments)
|
|
|
|
|
.preload(:blob).index_by(&:uuid)
|
|
|
|
|
|
2024-06-15 15:55:09 +03:00
|
|
|
@form_configs = Submitters::FormConfigs.call(@submitter, CONFIG_KEYS)
|
|
|
|
|
|
|
|
|
|
return unless @form_configs[:prefill_signature]
|
|
|
|
|
|
2024-07-24 09:13:13 +03:00
|
|
|
if (user_signature = UserConfigs.load_signature(current_user))
|
|
|
|
|
@signature_attachment = ActiveStorage::Attachment.find_or_create_by!(
|
|
|
|
|
blob_id: user_signature.blob_id,
|
|
|
|
|
name: 'attachments',
|
|
|
|
|
record: @submitter
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@signature_attachment ||=
|
2024-06-15 15:55:09 +03:00
|
|
|
Submitters::MaybeAssignDefaultBrowserSignature.call(@submitter, params, cookies, @attachments_index.values)
|
2024-06-11 18:19:21 +03:00
|
|
|
|
2024-06-15 15:55:09 +03:00
|
|
|
@attachments_index[@signature_attachment.uuid] = @signature_attachment if @signature_attachment
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2023-06-13 00:29:07 +03:00
|
|
|
submitter = Submitter.find_by!(slug: params[:slug])
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-12-05 18:18:32 +02:00
|
|
|
if submitter.completed_at?
|
|
|
|
|
return render json: { error: 'Form has been completed already.' }, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
if submitter.template.archived_at? || submitter.submission.archived_at?
|
2023-12-19 15:45:30 +02:00
|
|
|
return render json: { error: 'Form has been archived.' }, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
|
2024-08-01 16:43:55 +03:00
|
|
|
if submitter.submission.expired?
|
|
|
|
|
return render json: { error: 'Form has been expired.' }, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
|
2023-08-17 10:33:53 +03:00
|
|
|
Submitters::SubmitValues.call(submitter, params, request)
|
2023-06-26 00:46:01 +03:00
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
head :ok
|
2023-09-05 01:00:21 +03:00
|
|
|
rescue Submitters::SubmitValues::ValidationError => e
|
|
|
|
|
render json: { error: e.message }, status: :unprocessable_entity
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def completed
|
2024-04-18 16:45:02 +03:00
|
|
|
@submitter = Submitter.completed.find_by!(slug: params[:submit_form_slug])
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
2024-01-14 12:11:13 +02:00
|
|
|
|
|
|
|
|
def success; end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|