2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class SubmissionsController < ApplicationController
|
2023-06-28 00:22:34 +03:00
|
|
|
before_action :load_template, only: %i[new create]
|
2023-09-17 00:45:57 +03:00
|
|
|
authorize_resource :template, only: %i[new create]
|
|
|
|
|
|
|
|
|
|
load_and_authorize_resource :submission, only: %i[show destroy]
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-11-22 10:15:51 +02:00
|
|
|
prepend_before_action :maybe_redirect_com, only: %i[show]
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def show
|
2024-02-25 02:00:18 +02:00
|
|
|
@submission = Submissions.preload_with_pages(@submission)
|
2023-10-22 01:54:20 +03:00
|
|
|
|
2024-10-08 14:14:24 +03:00
|
|
|
unless @submission.submitters.all?(&:completed_at?)
|
|
|
|
|
ActiveRecord::Associations::Preloader.new(
|
|
|
|
|
records: [@submission],
|
|
|
|
|
associations: [submitters: :start_form_submission_events]
|
|
|
|
|
).call
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-20 22:38:38 +03:00
|
|
|
render :show, layout: 'plain'
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
def new
|
|
|
|
|
authorize!(:new, Submission)
|
|
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
|
|
|
|
|
def create
|
2023-09-17 00:45:57 +03:00
|
|
|
authorize!(:create, Submission)
|
|
|
|
|
|
2024-04-17 16:08:58 +03:00
|
|
|
save_template_message(@template, params) if params[:save_message] == '1'
|
|
|
|
|
|
2023-11-23 20:03:52 +02:00
|
|
|
if params[:is_custom_message] != '1'
|
|
|
|
|
params.delete(:subject)
|
|
|
|
|
params.delete(:body)
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
submissions =
|
2023-06-13 00:29:07 +03:00
|
|
|
if params[:emails].present?
|
2023-08-04 00:40:13 +03:00
|
|
|
Submissions.create_from_emails(template: @template,
|
|
|
|
|
user: current_user,
|
2023-08-05 19:20:06 +03:00
|
|
|
source: :invite,
|
2023-09-03 14:05:31 +03:00
|
|
|
mark_as_sent: params[:send_email] == '1',
|
2023-11-23 20:03:52 +02:00
|
|
|
emails: params[:emails],
|
2023-12-02 23:16:38 +02:00
|
|
|
params: params.merge('send_completed_email' => true))
|
2023-06-13 00:29:07 +03:00
|
|
|
else
|
2023-08-04 00:40:13 +03:00
|
|
|
Submissions.create_from_submitters(template: @template,
|
|
|
|
|
user: current_user,
|
2023-08-05 19:20:06 +03:00
|
|
|
source: :invite,
|
2023-09-10 02:10:01 +03:00
|
|
|
submitters_order: params[:preserve_order] == '1' ? 'preserved' : 'random',
|
2023-11-23 20:03:52 +02:00
|
|
|
submissions_attrs: submissions_params[:submission].to_h.values,
|
2023-12-02 23:16:38 +02:00
|
|
|
params: params.merge('send_completed_email' => true))
|
2023-06-13 00:29:07 +03:00
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-10-28 20:39:09 +02:00
|
|
|
enqueue_submission_created_webhooks(@template, submissions)
|
2024-05-17 13:28:10 +03:00
|
|
|
|
2023-11-23 20:03:52 +02:00
|
|
|
Submissions.send_signature_requests(submissions)
|
2023-07-19 01:31:27 +03:00
|
|
|
|
2024-09-18 19:47:47 +03:00
|
|
|
redirect_to template_path(@template), notice: I18n.t('new_recipients_have_been_added')
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2024-07-20 22:21:36 +03:00
|
|
|
notice =
|
|
|
|
|
if params[:permanently].present?
|
|
|
|
|
@submission.destroy!
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-09-23 11:38:08 +03:00
|
|
|
I18n.t('submission_has_been_removed')
|
2024-07-20 22:21:36 +03:00
|
|
|
else
|
|
|
|
|
@submission.update!(archived_at: Time.current)
|
|
|
|
|
|
2024-11-01 15:35:02 +02:00
|
|
|
WebhookUrls.for_account_id(@submission.account_id, 'submission.archived').each do |webhook_url|
|
|
|
|
|
SendSubmissionArchivedWebhookRequestJob.perform_async('submission_id' => @submission.id,
|
|
|
|
|
'webhook_url_id' => webhook_url.id)
|
2024-10-28 20:39:09 +02:00
|
|
|
end
|
2024-07-20 22:21:36 +03:00
|
|
|
|
2024-09-18 19:47:47 +03:00
|
|
|
I18n.t('submission_has_been_archived')
|
2024-07-20 22:21:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
redirect_back(fallback_location: template_path(@submission.template), notice:)
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-04-17 16:08:58 +03:00
|
|
|
def save_template_message(template, params)
|
|
|
|
|
template.preferences['request_email_subject'] = params[:subject] if params[:subject].present?
|
|
|
|
|
template.preferences['request_email_body'] = params[:body] if params[:body].present?
|
|
|
|
|
|
|
|
|
|
template.save!
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-28 20:39:09 +02:00
|
|
|
def enqueue_submission_created_webhooks(template, submissions)
|
2024-11-01 15:35:02 +02:00
|
|
|
WebhookUrls.for_account_id(template.account_id, 'submission.created').each do |webhook_url|
|
2024-10-28 20:39:09 +02:00
|
|
|
submissions.each do |submission|
|
2024-11-01 15:35:02 +02:00
|
|
|
SendSubmissionCreatedWebhookRequestJob.perform_async('submission_id' => submission.id,
|
|
|
|
|
'webhook_url_id' => webhook_url.id)
|
2024-10-28 20:39:09 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-13 00:29:07 +03:00
|
|
|
def submissions_params
|
2023-09-03 14:05:31 +03:00
|
|
|
params.permit(submission: { submitters: [%i[uuid email phone name]] })
|
2023-06-13 00:29:07 +03:00
|
|
|
end
|
|
|
|
|
|
2023-05-31 23:19:20 +03:00
|
|
|
def load_template
|
2024-02-03 22:37:17 +02:00
|
|
|
@template = Template.accessible_by(current_ability).find(params[:template_id])
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
end
|