diff --git a/app/controllers/start_form_controller.rb b/app/controllers/start_form_controller.rb index 929767f78..008f4934c 100644 --- a/app/controllers/start_form_controller.rb +++ b/app/controllers/start_form_controller.rb @@ -11,7 +11,7 @@ class StartFormController < ApplicationController def show @submitter = @template.submissions.new(account_id: @template.account_id) - .submitters.new(uuid: @template.submitters.first['uuid']) + .submitters.new(uuid: filter_undefined_submitters(@template).first['uuid']) end def update @@ -22,15 +22,17 @@ def update if @submitter.completed_at? redirect_to start_form_completed_path(@template.slug, email: submitter_params[:email]) else - if @template.submitters.to_a.size > 2 && @submitter.new_record? + if filter_undefined_submitters(@template).size > 2 && @submitter.new_record? @error_message = 'Not found' return render :show end - assign_submission_attributes(@submitter, @template) if @submitter.new_record? + if (is_new_record = @submitter.new_record?) + assign_submission_attributes(@submitter, @template) - is_new_record = @submitter.new_record? + Submissions::AssignDefinedSubmitters.call(@submitter.submission) + end if @submitter.save if is_new_record @@ -66,7 +68,7 @@ def assign_submission_attributes(submitter, template) (Submitter.where(submission: template.submissions).find_by(slug: params[:resubmit]) if params[:resubmit].present?) submitter.assign_attributes( - uuid: template.submitters.first['uuid'], + uuid: filter_undefined_submitters(template).first['uuid'], ip: request.remote_ip, ua: request.user_agent, values: resubmit_submitter&.preferences&.fetch('default_values', nil) || {}, @@ -83,25 +85,18 @@ def assign_submission_attributes(submitter, template) submitter.submission ||= Submission.new(template:, account_id: template.account_id, template_submitters: template.submitters, + submitters: [submitter], source: :link) - maybe_assign_default_second_submitter(submitter.submission) - submitter.account_id = submitter.submission.account_id submitter end - def maybe_assign_default_second_submitter(submission) - return unless submission.new_record? - return if submission.template.submitters.to_a.size != 2 - - submission.submitters_order = 'preserved' - submission.submitters.new( - account_id: submission.account_id, - uuid: submission.template.submitters.second['uuid'], - email: submission.template.author.email - ) + def filter_undefined_submitters(template) + template.submitters.select do |item| + item['linked_to_uuid'].blank? && item['is_requester'].blank? && item['email'].blank? + end end def submitter_params diff --git a/lib/submissions/assign_defined_submitters.rb b/lib/submissions/assign_defined_submitters.rb new file mode 100644 index 000000000..a27520e27 --- /dev/null +++ b/lib/submissions/assign_defined_submitters.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module Submissions + module AssignDefinedSubmitters + module_function + + def call(submission) + submission.submitters_order = 'preserved' + + assign_defined_submitters(submission) + assign_linked_submitters(submission) + + if submission.submitters.size == 1 && submission.template.submitters.size == 2 + submission.submitters.new( + account_id: submission.account_id, + uuid: submission.template.submitters.second['uuid'], + email: submission.template.author.email + ) + end + + submission + end + + def assign_defined_submitters(submission) + submission.template.submitters.to_a.select do |item| + next if item['email'].blank? && item['is_requester'].blank? + + submission.submitters.new( + account_id: submission.account_id, + uuid: item['uuid'], + email: item['is_requester'] ? submission.template.author.email : item['email'] + ) + end + end + + def assign_linked_submitters(submission) + submission.template.submitters.to_a.select do |item| + next if item['linked_to_uuid'].blank? + + email = submission.submitters.find { |s| s.uuid == item['linked_to_uuid'] }&.email + + next unless email + + submission.submitters.new( + account_id: submission.account_id, + uuid: item['uuid'], + email: + ) + end + end + end +end