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
|
2024-12-08 01:37:20 +02:00
|
|
|
has_many :template_accesses, through: :template
|
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) }
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-09-09 00:43:32 +03:00
|
|
|
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}>)
|
2023-09-09 00:43:32 +03:00
|
|
|
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
|
2023-06-13 00:29:07 +03:00
|
|
|
end
|