Skip to content

Commit

Permalink
assign default submitters
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj authored and AlexBTurchyn committed Sep 9, 2024
1 parent bd853c6 commit be4a050
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
29 changes: 12 additions & 17 deletions app/controllers/start_form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) || {},
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions lib/submissions/assign_defined_submitters.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit be4a050

Please sign in to comment.