assign default values on api complete
This commit is contained in:
@@ -123,15 +123,32 @@ module Api
|
||||
values = values.except(phone_field_uuid)
|
||||
|
||||
submitter.values = submitter.values.merge(values) if values.present?
|
||||
submitter.completed_at = attrs[:completed] ? Time.current : submitter.completed_at
|
||||
submitter.metadata = attrs[:metadata] if attrs.key?(:metadata)
|
||||
|
||||
maybe_assign_completed_attributes(submitter, attrs)
|
||||
|
||||
assign_external_id(submitter, attrs)
|
||||
assign_preferences(submitter, attrs)
|
||||
|
||||
submitter
|
||||
end
|
||||
|
||||
def maybe_assign_completed_attributes(submitter, attrs)
|
||||
submitter.completed_at = attrs[:completed] ? Time.current : submitter.completed_at
|
||||
|
||||
if attrs[:completed]
|
||||
submitter.values = Submitters::SubmitValues.merge_default_values(submitter)
|
||||
submitter.values = Submitters::SubmitValues.merge_formula_values(submitter)
|
||||
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(submitter)
|
||||
|
||||
submitter.values = submitter.values.transform_values do |v|
|
||||
v == '{{date}}' ? Time.current.in_time_zone(submitter.account.timezone).to_date.to_s : v
|
||||
end
|
||||
end
|
||||
|
||||
submitter
|
||||
end
|
||||
|
||||
def assign_external_id(submitter, attrs)
|
||||
submitter.external_id = attrs[:application_key] if attrs.key?(:application_key)
|
||||
submitter.external_id = attrs[:external_id] if attrs.key?(:external_id)
|
||||
|
||||
@@ -159,20 +159,37 @@ module Submissions
|
||||
values[f['uuid']].present? && f['type'] == 'phone'
|
||||
end&.dig('uuid')
|
||||
|
||||
submission.submitters.new(
|
||||
email:,
|
||||
phone: (attrs[:phone] || values[phone_field_uuid]).to_s.gsub(/[^0-9+]/, ''),
|
||||
name: attrs[:name],
|
||||
external_id: attrs[:external_id].presence || attrs[:application_key],
|
||||
completed_at: attrs[:completed].present? ? Time.current : nil,
|
||||
sent_at: mark_as_sent && email.present? && is_order_sent ? Time.current : nil,
|
||||
values: values.except(phone_field_uuid),
|
||||
metadata: attrs[:metadata] || {},
|
||||
preferences: preferences.merge(submitter_preferences)
|
||||
.merge({ default_values: attrs[:values] }.compact_blank)
|
||||
.except('bcc_completed'),
|
||||
uuid:
|
||||
)
|
||||
submitter =
|
||||
submission.submitters.new(
|
||||
email:,
|
||||
phone: (attrs[:phone] || values[phone_field_uuid]).to_s.gsub(/[^0-9+]/, ''),
|
||||
name: attrs[:name],
|
||||
external_id: attrs[:external_id].presence || attrs[:application_key],
|
||||
completed_at: attrs[:completed].present? ? Time.current : nil,
|
||||
sent_at: mark_as_sent && email.present? && is_order_sent ? Time.current : nil,
|
||||
values: values.except(phone_field_uuid),
|
||||
metadata: attrs[:metadata] || {},
|
||||
preferences: preferences.merge(submitter_preferences)
|
||||
.merge({ default_values: attrs[:values] }.compact_blank)
|
||||
.except('bcc_completed'),
|
||||
uuid:
|
||||
)
|
||||
|
||||
assign_completed_attributes(submitter) if submitter.completed_at?
|
||||
|
||||
submitter
|
||||
end
|
||||
|
||||
def assign_completed_attributes(submitter)
|
||||
submitter.values = Submitters::SubmitValues.merge_default_values(submitter)
|
||||
submitter.values = Submitters::SubmitValues.merge_formula_values(submitter)
|
||||
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(submitter)
|
||||
|
||||
submitter.values = submitter.values.transform_values do |v|
|
||||
v == '{{date}}' ? Time.current.in_time_zone(submitter.submission.account.timezone).to_date.to_s : v
|
||||
end
|
||||
|
||||
submitter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,6 +14,14 @@ module Submitters
|
||||
module_function
|
||||
|
||||
def call(submitter, with_logo: true)
|
||||
attachment = build_attachment(submitter, with_logo:)
|
||||
|
||||
attachment.save!
|
||||
|
||||
attachment
|
||||
end
|
||||
|
||||
def build_attachment(submitter, with_logo: true)
|
||||
image = generate_stamp_image(submitter, with_logo:)
|
||||
|
||||
image_data = image.write_to_buffer('.png')
|
||||
@@ -22,11 +30,9 @@ module Submitters
|
||||
|
||||
attachment = submitter.attachments.joins(:blob).find_by(blob: { checksum: })
|
||||
|
||||
attachment || ActiveStorage::Attachment.create!(
|
||||
attachment || submitter.attachments_attachments.new(
|
||||
blob: ActiveStorage::Blob.create_and_upload!(io: StringIO.new(image_data), filename: 'stamp.png'),
|
||||
metadata: { analyzed: true, identified: true, width: image.width, height: image.height },
|
||||
name: 'attachments',
|
||||
record: submitter
|
||||
metadata: { analyzed: true, identified: true, width: image.width, height: image.height }
|
||||
)
|
||||
end
|
||||
|
||||
@@ -61,10 +67,11 @@ module Submitters
|
||||
end
|
||||
|
||||
def build_text_image(submitter)
|
||||
time = I18n.l(submitter.completed_at.in_time_zone(submitter.account.timezone), format: :long,
|
||||
locale: submitter.account.locale)
|
||||
time = I18n.l(submitter.completed_at.in_time_zone(submitter.submission.account.timezone),
|
||||
format: :long,
|
||||
locale: submitter.submission.account.locale)
|
||||
|
||||
timezone = TimeUtils.timezone_abbr(submitter.account.timezone, submitter.completed_at)
|
||||
timezone = TimeUtils.timezone_abbr(submitter.submission.account.timezone, submitter.completed_at)
|
||||
|
||||
name = if submitter.name.present? && submitter.email.present?
|
||||
"#{submitter.name} #{submitter.email}"
|
||||
@@ -80,7 +87,7 @@ module Submitters
|
||||
''
|
||||
end
|
||||
|
||||
digitally_signed_by = I18n.t(:digitally_signed_by, locale: submitter.account.locale)
|
||||
digitally_signed_by = I18n.t(:digitally_signed_by, locale: submitter.submission.account.locale)
|
||||
|
||||
text = %(<span size="90">#{digitally_signed_by}:\n<b>#{name}</b>\n#{role}#{time} #{timezone}</span>)
|
||||
|
||||
|
||||
@@ -96,8 +96,10 @@ module Submitters
|
||||
|
||||
if field['type'] == 'stamp'
|
||||
acc[field['uuid']] ||=
|
||||
Submitters::CreateStampAttachment.call(submitter,
|
||||
with_logo: field.dig('preferences', 'with_logo') != false).uuid
|
||||
Submitters::CreateStampAttachment.build_attachment(
|
||||
submitter,
|
||||
with_logo: field.dig('preferences', 'with_logo') != false
|
||||
).uuid
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user