add submitter update endpoint
This commit is contained in:
@@ -39,11 +39,11 @@ module Submissions
|
||||
end
|
||||
end
|
||||
|
||||
def maybe_set_template_fields(submission, submitters_attrs)
|
||||
template_fields = submission.template.fields.deep_dup
|
||||
def maybe_set_template_fields(submission, submitters_attrs, submitter_uuid: nil)
|
||||
template_fields = (submission.template_fields || submission.template.fields).deep_dup
|
||||
|
||||
submitters_attrs.each_with_index do |submitter_attrs, index|
|
||||
submitter_uuid = find_submitter_uuid(submission.template, submitter_attrs, index)
|
||||
submitter_uuid ||= find_submitter_uuid(submission.template, submitter_attrs, index)
|
||||
|
||||
process_readonly_fields_param(submitter_attrs[:readonly_fields], template_fields, submitter_uuid)
|
||||
|
||||
|
||||
@@ -268,7 +268,7 @@ module Submissions
|
||||
Submissions::EnsureResultGenerated.call(latest_submitter) if latest_submitter
|
||||
|
||||
documents = latest_submitter&.documents&.preload(:blob).to_a.presence
|
||||
documents ||= submitter.submission.template.documents.preload(:blob)
|
||||
documents ||= submitter.submission.template.schema_documents.preload(:blob)
|
||||
|
||||
documents.to_h do |attachment|
|
||||
pdf =
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Submissions
|
||||
module NormalizeParamUtils
|
||||
module_function
|
||||
|
||||
def normalize_submissions_params!(submissions_params, template)
|
||||
attachments = []
|
||||
|
||||
Array.wrap(submissions_params).each do |submission|
|
||||
submission[:submitters].each_with_index do |submitter, index|
|
||||
_, new_attachments = normalize_submitter_params!(submitter, template, index)
|
||||
|
||||
attachments.push(*new_attachments)
|
||||
end
|
||||
end
|
||||
|
||||
[submissions_params, attachments]
|
||||
end
|
||||
|
||||
def normalize_submitter_params!(submitter_params, template, index = nil, for_submitter: nil)
|
||||
default_values = submitter_params[:values] || {}
|
||||
|
||||
submitter_params[:fields]&.each do |f|
|
||||
default_values[f[:name]] = f[:default_value] if f[:default_value].present?
|
||||
end
|
||||
|
||||
return submitter_params if default_values.blank?
|
||||
|
||||
values, new_attachments =
|
||||
Submitters::NormalizeValues.call(template,
|
||||
default_values,
|
||||
submitter_name: submitter_params[:role] ||
|
||||
template.submitters.dig(index, 'name'),
|
||||
for_submitter:,
|
||||
throw_errors: true)
|
||||
|
||||
submitter_params[:values] = values
|
||||
|
||||
[submitter_params, new_attachments]
|
||||
end
|
||||
|
||||
def save_default_value_attachments!(attachments, submitters)
|
||||
return if attachments.blank?
|
||||
|
||||
attachments_index = attachments.index_by(&:uuid)
|
||||
|
||||
submitters.each do |submitter|
|
||||
submitter.values.to_a.each do |_, value|
|
||||
attachment = attachments_index[value]
|
||||
|
||||
next unless attachment
|
||||
|
||||
attachment.record = submitter
|
||||
|
||||
attachment.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,12 +9,8 @@ module Submitters
|
||||
|
||||
module_function
|
||||
|
||||
def call(template, values, submitter_name, throw_errors: false)
|
||||
submitter =
|
||||
template.submitters.find { |e| e['name'] == submitter_name } ||
|
||||
raise(UnknownSubmitterName, "Unknown submitter: #{submitter_name}")
|
||||
|
||||
fields = template.fields.select { |e| e['submitter_uuid'] == submitter['uuid'] }
|
||||
def call(template, values, submitter_name: nil, for_submitter: nil, throw_errors: false)
|
||||
fields = fetch_fields(template, submitter_name:, for_submitter:)
|
||||
|
||||
fields_uuid_index = fields.index_by { |e| e['uuid'] }
|
||||
fields_name_index = build_fields_index(fields)
|
||||
@@ -31,7 +27,7 @@ module Submitters
|
||||
next if key.blank?
|
||||
|
||||
if fields_uuid_index[key]['type'].in?(%w[initials signature image file])
|
||||
new_value, new_attachments = normalize_attachment_value(value, template.account)
|
||||
new_value, new_attachments = normalize_attachment_value(value, template.account, for_submitter)
|
||||
|
||||
attachments.push(*new_attachments)
|
||||
|
||||
@@ -44,27 +40,45 @@ module Submitters
|
||||
[normalized_values, attachments]
|
||||
end
|
||||
|
||||
def fetch_fields(template, submitter_name: nil, for_submitter: nil)
|
||||
if submitter_name
|
||||
submitter =
|
||||
template.submitters.find { |e| e['name'] == submitter_name } ||
|
||||
raise(UnknownSubmitterName, "Unknown submitter: #{submitter_name}")
|
||||
end
|
||||
|
||||
fields = for_submitter&.submission&.template_fields || template.fields
|
||||
|
||||
fields.select { |e| e['submitter_uuid'] == (for_submitter&.uuid || submitter['uuid']) }
|
||||
end
|
||||
|
||||
def build_fields_index(fields)
|
||||
fields.index_by { |e| e['name'] }.merge(fields.index_by { |e| e['name'].to_s.parameterize.underscore })
|
||||
end
|
||||
|
||||
def normalize_attachment_value(value, account)
|
||||
def normalize_attachment_value(value, account, for_submitter = nil)
|
||||
if value.is_a?(Array)
|
||||
new_attachments = value.map { |v| build_attachment(v, account) }
|
||||
new_attachments = value.map { |v| find_or_build_attachment(v, account, for_submitter) }
|
||||
|
||||
[new_attachments.map(&:uuid), new_attachments]
|
||||
else
|
||||
new_attachment = build_attachment(value, account)
|
||||
new_attachment = find_or_build_attachment(value, account, for_submitter)
|
||||
|
||||
[new_attachment.uuid, new_attachment]
|
||||
end
|
||||
end
|
||||
|
||||
def build_attachment(value, account)
|
||||
ActiveStorage::Attachment.new(
|
||||
blob: find_or_create_blobs(account, value),
|
||||
def find_or_build_attachment(value, account, for_submitter = nil)
|
||||
blob = find_or_create_blobs(account, value)
|
||||
|
||||
attachment = for_submitter.attachments.find_by(blob_id: blob.id) if for_submitter
|
||||
|
||||
attachment ||= ActiveStorage::Attachment.new(
|
||||
blob:,
|
||||
name: 'attachments'
|
||||
)
|
||||
|
||||
attachment
|
||||
end
|
||||
|
||||
def find_or_create_blobs(account, url)
|
||||
|
||||
Reference in New Issue
Block a user