|  |  | @ -8,7 +8,9 @@ class StartFormController < ApplicationController | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   around_action :with_browser_locale, only: %i[show completed] |  |  |  |   around_action :with_browser_locale, only: %i[show completed] | 
			
		
	
		
		
			
				
					
					|  |  |  |   before_action :maybe_redirect_com, only: %i[show completed] |  |  |  |   before_action :maybe_redirect_com, only: %i[show completed] | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   before_action :load_resubmit_submitter, only: :update | 
			
		
	
		
		
			
				
					
					|  |  |  |   before_action :load_template |  |  |  |   before_action :load_template | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   before_action :authorize_start!, only: :update | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def show |  |  |  |   def show | 
			
		
	
		
		
			
				
					
					|  |  |  |     raise ActionController::RoutingError, I18n.t('not_found') if @template.preferences['require_phone_2fa'] |  |  |  |     raise ActionController::RoutingError, I18n.t('not_found') if @template.preferences['require_phone_2fa'] | 
			
		
	
	
		
		
			
				
					|  |  | @ -28,8 +30,6 @@ class StartFormController < ApplicationController | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def update |  |  |  |   def update | 
			
		
	
		
		
			
				
					
					|  |  |  |     return redirect_to start_form_path(@template.slug) if @template.archived_at? || !@template.shared_link? |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     @submitter = find_or_initialize_submitter(@template, submitter_params) |  |  |  |     @submitter = find_or_initialize_submitter(@template, submitter_params) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     if @submitter.completed_at? |  |  |  |     if @submitter.completed_at? | 
			
		
	
	
		
		
			
				
					|  |  | @ -74,6 +74,24 @@ class StartFormController < ApplicationController | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   private |  |  |  |   private | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   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 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def enqueue_submission_create_webhooks(submitter) |  |  |  |   def enqueue_submission_create_webhooks(submitter) | 
			
		
	
		
		
			
				
					
					|  |  |  |     WebhookUrls.for_account_id(submitter.account_id, 'submission.created').each do |webhook_url| |  |  |  |     WebhookUrls.for_account_id(submitter.account_id, 'submission.created').each do |webhook_url| | 
			
		
	
		
		
			
				
					
					|  |  |  |       SendSubmissionCreatedWebhookRequestJob.perform_async('submission_id' => submitter.submission_id, |  |  |  |       SendSubmissionCreatedWebhookRequestJob.perform_async('submission_id' => submitter.submission_id, | 
			
		
	
	
		
		
			
				
					|  |  | @ -82,31 +100,29 @@ class StartFormController < ApplicationController | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def find_or_initialize_submitter(template, submitter_params) |  |  |  |   def find_or_initialize_submitter(template, submitter_params) | 
			
		
	
		
		
			
				
					
					|  |  |  |     Submitter.where(submission: template.submissions.where(expire_at: Time.current..) |  |  |  |     Submitter | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                         .or(template.submissions.where(expire_at: nil)).where(archived_at: nil)) |  |  |  |       .where(submission: template.submissions.where(expire_at: Time.current..) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .order(id: :desc) |  |  |  |                                  .or(template.submissions.where(expire_at: nil)).where(archived_at: nil)) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .where(declined_at: nil) |  |  |  |       .order(id: :desc) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .where(external_id: nil) |  |  |  |       .where(declined_at: nil) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .where(ip: [nil, request.remote_ip]) |  |  |  |       .where(external_id: nil) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .then { |rel| params[:resubmit].present? ? rel.where(completed_at: nil) : rel } |  |  |  |       .where(ip: [nil, request.remote_ip]) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |              .find_or_initialize_by(email: submitter_params[:email], **submitter_params.compact_blank) |  |  |  |       .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) | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def assign_submission_attributes(submitter, template) |  |  |  |   def assign_submission_attributes(submitter, template) | 
			
		
	
		
		
			
				
					
					|  |  |  |     resubmit_submitter = |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       (Submitter.where(submission: template.submissions).find_by(slug: params[:resubmit]) if params[:resubmit].present?) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     submitter.assign_attributes( |  |  |  |     submitter.assign_attributes( | 
			
		
	
		
		
			
				
					
					|  |  |  |       uuid: (filter_undefined_submitters(template).first || @template.submitters.first)['uuid'], |  |  |  |       uuid: (filter_undefined_submitters(template).first || @template.submitters.first)['uuid'], | 
			
		
	
		
		
			
				
					
					|  |  |  |       ip: request.remote_ip, |  |  |  |       ip: request.remote_ip, | 
			
		
	
		
		
			
				
					
					|  |  |  |       ua: request.user_agent, |  |  |  |       ua: request.user_agent, | 
			
		
	
		
		
			
				
					
					|  |  |  |       values: resubmit_submitter&.preferences&.fetch('default_values', nil) || {}, |  |  |  |       values: @resubmit_submitter&.preferences&.fetch('default_values', nil) || {}, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       preferences: resubmit_submitter&.preferences.presence || { 'send_email' => true }, |  |  |  |       preferences: @resubmit_submitter&.preferences.presence || { 'send_email' => true }, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       metadata: resubmit_submitter&.metadata.presence || {} |  |  |  |       metadata: @resubmit_submitter&.metadata.presence || {} | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     ) |  |  |  |     ) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     if submitter.values.present? |  |  |  |     if submitter.values.present? | 
			
		
	
		
		
			
				
					
					|  |  |  |       resubmit_submitter.attachments.each do |attachment| |  |  |  |       @resubmit_submitter.attachments.each do |attachment| | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         submitter.attachments << attachment.dup if submitter.values.value?(attachment.uuid) |  |  |  |         submitter.attachments << attachment.dup if submitter.values.value?(attachment.uuid) | 
			
		
	
		
		
			
				
					
					|  |  |  |       end |  |  |  |       end | 
			
		
	
		
		
			
				
					
					|  |  |  |     end |  |  |  |     end | 
			
		
	
	
		
		
			
				
					|  |  | @ -128,15 +144,21 @@ class StartFormController < ApplicationController | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def submitter_params |  |  |  |   def submitter_params | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     return current_user.slice(:email) if params[:selfsign] | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     return @resubmit_submitter.slice(:name, :phone, :email) if @resubmit_submitter.present? | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     params.require(:submitter).permit(:email, :phone, :name).tap do |attrs| |  |  |  |     params.require(:submitter).permit(:email, :phone, :name).tap do |attrs| | 
			
		
	
		
		
			
				
					
					|  |  |  |       attrs[:email] = Submissions.normalize_email(attrs[:email]) |  |  |  |       attrs[:email] = Submissions.normalize_email(attrs[:email]) | 
			
		
	
		
		
			
				
					
					|  |  |  |     end |  |  |  |     end | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def load_template |  |  |  |   def load_template | 
			
		
	
		
		
			
				
					
					|  |  |  |     slug = params[:slug] || params[:start_form_slug] |  |  |  |     @template = | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |       if @resubmit_submitter | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     @template = Template.find_by!(slug:) |  |  |  |         @resubmit_submitter.template | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       else | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         Template.find_by!(slug: params[:slug] || params[:start_form_slug]) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       end | 
			
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   def multiple_submitters_error_message |  |  |  |   def multiple_submitters_error_message | 
			
		
	
	
		
		
			
				
					|  |  | 
 |