Files
docuseal/app/models/submitter.rb
T

123 lines
3.3 KiB
Ruby
Raw Normal View History

2023-06-13 00:29:07 +03:00
# frozen_string_literal: true
# == Schema Information
#
# Table name: submitters
#
2024-02-01 00:34:35 +02:00
# id :bigint not null, primary key
# completed_at :datetime
2024-08-21 09:22:59 +03:00
# declined_at :datetime
2024-02-01 00:34:35 +02:00
# email :string
# ip :string
2024-02-01 23:29:27 +02:00
# metadata :text not null
2024-02-01 00:34:35 +02:00
# name :string
# opened_at :datetime
# phone :string
# preferences :text not null
# sent_at :datetime
# slug :string not null
# ua :string
# uuid :string not null
# values :text not null
# created_at :datetime not null
# updated_at :datetime not null
2024-08-16 10:42:16 +03:00
# account_id :bigint not null
2024-02-01 00:34:35 +02:00
# external_id :string
# submission_id :bigint not null
2023-06-13 00:29:07 +03:00
#
# Indexes
#
2024-08-16 10:19:00 +03:00
# index_submitters_on_account_id_and_id (account_id,id)
# index_submitters_on_email (email)
# index_submitters_on_external_id (external_id)
# index_submitters_on_slug (slug) UNIQUE
# index_submitters_on_submission_id (submission_id)
2023-06-13 00:29:07 +03:00
#
# Foreign Keys
#
# fk_rails_... (submission_id => submissions.id)
#
class Submitter < ApplicationRecord
belongs_to :submission
2024-08-16 10:58:51 +03:00
belongs_to :account
2023-08-05 15:25:16 +03:00
has_one :template, through: :submission
2023-06-13 00:29:07 +03:00
attribute :values, :string, default: -> { {} }
2023-11-23 20:03:52 +02:00
attribute :preferences, :string, default: -> { {} }
2024-02-01 23:29:27 +02:00
attribute :metadata, :string, default: -> { {} }
2023-07-14 22:33:42 +03:00
attribute :slug, :string, default: -> { SecureRandom.base58(14) }
2023-06-13 00:29:07 +03:00
2023-12-29 18:50:14 +00:00
serialize :values, coder: JSON
serialize :preferences, coder: JSON
2024-02-01 23:29:27 +02:00
serialize :metadata, coder: JSON
2023-06-13 00:29:07 +03:00
has_many_attached :documents
has_many_attached :attachments
2024-12-01 00:10:07 +02:00
has_many_attached :preview_documents
2025-05-11 16:11:02 +03:00
has_many :template_accesses, through: :submission
2025-02-28 12:08:30 +02:00
has_many :email_events, as: :emailable, dependent: (Docuseal.multitenant? ? nil : :destroy)
2023-06-13 00:29:07 +03:00
2023-07-01 13:16:07 +03:00
has_many :document_generation_events, dependent: :destroy
2023-09-10 12:55:41 +03:00
has_many :submission_events, dependent: :destroy
2024-10-08 14:14:24 +03:00
has_many :start_form_submission_events, -> { where(event_type: :start_form) },
class_name: 'SubmissionEvent', dependent: :destroy, inverse_of: :submitter
2023-07-01 13:16:07 +03:00
2024-04-18 16:45:02 +03:00
scope :completed, -> { where.not(completed_at: nil) }
2025-02-28 12:08:30 +02:00
after_destroy :anonymize_email_events, if: -> { Docuseal.multitenant? }
2023-06-13 00:29:07 +03:00
def status
2024-08-21 09:22:59 +03:00
if declined_at?
'declined'
elsif completed_at?
2023-06-13 00:29:07 +03:00
'completed'
elsif opened_at?
'opened'
elsif sent_at?
'sent'
else
'awaiting'
end
end
2023-06-30 01:26:13 +03:00
2024-02-01 00:34:35 +02:00
def application_key
external_id
end
def friendly_name
2023-09-16 13:46:47 +03:00
if name.present? && email.present? && email.exclude?(',')
2023-11-15 23:42:48 +02:00
%("#{name.delete('"')}" <#{email}>)
else
email
end
end
2024-09-01 20:38:49 +03:00
def first_name
2024-09-26 16:07:23 +03:00
name&.split(/\s+/, 2)&.first
2024-09-01 20:38:49 +03:00
end
def last_name
2024-09-26 16:07:23 +03:00
name&.split(/\s+/, 2)&.last
2024-09-01 20:38:49 +03:00
end
2023-06-30 01:26:13 +03:00
def status_event_at
2024-08-21 09:22:59 +03:00
declined_at || completed_at || opened_at || sent_at || created_at
2023-06-30 01:26:13 +03:00
end
2024-09-18 15:46:39 +03:00
def with_signature_fields?
@with_signature_fields ||= begin
fields = submission.template_fields || template.fields
signature_field_types = %w[signature initials]
fields.any? { |f| f['submitter_uuid'] == uuid && signature_field_types.include?(f['type']) }
end
end
2025-02-28 12:08:30 +02:00
private
def anonymize_email_events
email_events.each do |event|
event.update!(email: Digest::MD5.base64digest(event.email))
end
end
2023-06-13 00:29:07 +03:00
end