assign default values on api complete

pull/349/head
Pete Matsyburka 1 year ago
parent e8e32e5d1f
commit 7eae09bdb0

@ -123,15 +123,32 @@ module Api
values = values.except(phone_field_uuid) values = values.except(phone_field_uuid)
submitter.values = submitter.values.merge(values) if values.present? 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) submitter.metadata = attrs[:metadata] if attrs.key?(:metadata)
maybe_assign_completed_attributes(submitter, attrs)
assign_external_id(submitter, attrs) assign_external_id(submitter, attrs)
assign_preferences(submitter, attrs) assign_preferences(submitter, attrs)
submitter submitter
end 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) def assign_external_id(submitter, attrs)
submitter.external_id = attrs[:application_key] if attrs.key?(:application_key) submitter.external_id = attrs[:application_key] if attrs.key?(:application_key)
submitter.external_id = attrs[:external_id] if attrs.key?(:external_id) submitter.external_id = attrs[:external_id] if attrs.key?(:external_id)

@ -159,6 +159,7 @@ module Submissions
values[f['uuid']].present? && f['type'] == 'phone' values[f['uuid']].present? && f['type'] == 'phone'
end&.dig('uuid') end&.dig('uuid')
submitter =
submission.submitters.new( submission.submitters.new(
email:, email:,
phone: (attrs[:phone] || values[phone_field_uuid]).to_s.gsub(/[^0-9+]/, ''), phone: (attrs[:phone] || values[phone_field_uuid]).to_s.gsub(/[^0-9+]/, ''),
@ -173,6 +174,22 @@ module Submissions
.except('bcc_completed'), .except('bcc_completed'),
uuid: 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 end
end end

@ -14,6 +14,14 @@ module Submitters
module_function module_function
def call(submitter, with_logo: true) 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 = generate_stamp_image(submitter, with_logo:)
image_data = image.write_to_buffer('.png') image_data = image.write_to_buffer('.png')
@ -22,11 +30,9 @@ module Submitters
attachment = submitter.attachments.joins(:blob).find_by(blob: { checksum: }) 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'), 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 }, metadata: { analyzed: true, identified: true, width: image.width, height: image.height }
name: 'attachments',
record: submitter
) )
end end
@ -61,10 +67,11 @@ module Submitters
end end
def build_text_image(submitter) def build_text_image(submitter)
time = I18n.l(submitter.completed_at.in_time_zone(submitter.account.timezone), format: :long, time = I18n.l(submitter.completed_at.in_time_zone(submitter.submission.account.timezone),
locale: submitter.account.locale) 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? name = if submitter.name.present? && submitter.email.present?
"#{submitter.name} #{submitter.email}" "#{submitter.name} #{submitter.email}"
@ -80,7 +87,7 @@ module Submitters
'' ''
end 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>) 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' if field['type'] == 'stamp'
acc[field['uuid']] ||= acc[field['uuid']] ||=
Submitters::CreateStampAttachment.call(submitter, Submitters::CreateStampAttachment.build_attachment(
with_logo: field.dig('preferences', 'with_logo') != false).uuid submitter,
with_logo: field.dig('preferences', 'with_logo') != false
).uuid
next next
end end

Loading…
Cancel
Save