2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: submissions
|
|
|
|
|
#
|
2023-07-26 09:58:50 +03:00
|
|
|
# id :bigint not null, primary key
|
2023-12-30 00:37:03 +02:00
|
|
|
# archived_at :datetime
|
2024-08-01 16:43:55 +03:00
|
|
|
# expire_at :datetime
|
2023-11-23 20:03:52 +02:00
|
|
|
# preferences :text not null
|
2023-11-20 00:52:05 +02:00
|
|
|
# slug :string not null
|
2023-08-05 19:20:06 +03:00
|
|
|
# source :text not null
|
2023-09-10 02:10:01 +03:00
|
|
|
# submitters_order :string not null
|
2023-07-26 09:58:50 +03:00
|
|
|
# template_fields :text
|
|
|
|
|
# template_schema :text
|
|
|
|
|
# template_submitters :text
|
|
|
|
|
# created_at :datetime not null
|
|
|
|
|
# updated_at :datetime not null
|
2024-02-03 14:39:28 +02:00
|
|
|
# account_id :bigint not null
|
2023-07-26 09:58:50 +03:00
|
|
|
# created_by_user_id :bigint
|
|
|
|
|
# template_id :bigint not null
|
2023-05-21 17:59:36 +03:00
|
|
|
#
|
|
|
|
|
# Indexes
|
|
|
|
|
#
|
2024-08-16 11:39:34 +03:00
|
|
|
# index_submissions_on_account_id_and_id (account_id,id)
|
2023-06-29 02:04:17 +03:00
|
|
|
# index_submissions_on_created_by_user_id (created_by_user_id)
|
2023-11-20 00:52:05 +02:00
|
|
|
# index_submissions_on_slug (slug) UNIQUE
|
2023-06-29 02:04:17 +03:00
|
|
|
# index_submissions_on_template_id (template_id)
|
2023-05-21 17:59:36 +03:00
|
|
|
#
|
|
|
|
|
# Foreign Keys
|
|
|
|
|
#
|
2023-06-29 02:04:17 +03:00
|
|
|
# fk_rails_... (created_by_user_id => users.id)
|
2023-05-31 23:19:20 +03:00
|
|
|
# fk_rails_... (template_id => templates.id)
|
2023-05-21 17:59:36 +03:00
|
|
|
#
|
|
|
|
|
class Submission < ApplicationRecord
|
2023-05-31 23:19:20 +03:00
|
|
|
belongs_to :template
|
2024-02-03 14:39:28 +02:00
|
|
|
belongs_to :account
|
2023-06-29 02:04:17 +03:00
|
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-06-13 00:29:07 +03:00
|
|
|
has_many :submitters, dependent: :destroy
|
2023-09-10 12:55:41 +03:00
|
|
|
has_many :submission_events, dependent: :destroy
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-11-23 20:03:52 +02:00
|
|
|
attribute :preferences, :string, default: -> { {} }
|
|
|
|
|
|
2023-12-29 18:50:14 +00:00
|
|
|
serialize :template_fields, coder: JSON
|
|
|
|
|
serialize :template_schema, coder: JSON
|
|
|
|
|
serialize :template_submitters, coder: JSON
|
|
|
|
|
serialize :preferences, coder: JSON
|
2023-07-26 09:58:50 +03:00
|
|
|
|
2023-08-05 19:20:06 +03:00
|
|
|
attribute :source, :string, default: 'link'
|
2023-09-10 02:10:01 +03:00
|
|
|
attribute :submitters_order, :string, default: 'random'
|
2023-08-05 19:20:06 +03:00
|
|
|
|
2023-11-20 00:52:05 +02:00
|
|
|
attribute :slug, :string, default: -> { SecureRandom.base58(14) }
|
|
|
|
|
|
2023-09-10 19:25:57 +03:00
|
|
|
has_one_attached :audit_trail
|
2024-05-25 20:57:36 +03:00
|
|
|
has_one_attached :combined_document
|
2023-09-10 19:25:57 +03:00
|
|
|
|
2024-10-15 19:16:09 +03:00
|
|
|
has_many_attached :preview_documents
|
|
|
|
|
|
2024-12-08 01:37:20 +02:00
|
|
|
has_many :template_accesses, primary_key: :template_id, foreign_key: :template_id, dependent: nil, inverse_of: false
|
|
|
|
|
|
2023-09-01 01:27:13 +03:00
|
|
|
has_many :template_schema_documents,
|
|
|
|
|
->(e) { where(uuid: (e.template_schema.presence || e.template.schema).pluck('attachment_uuid')) },
|
|
|
|
|
through: :template, source: :documents_attachments
|
|
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
scope :active, -> { where(archived_at: nil) }
|
2024-11-23 01:45:10 +02:00
|
|
|
scope :pending, -> { joins(:submitters).where(submitters: { completed_at: nil }).group(:id) }
|
|
|
|
|
scope :completed, lambda {
|
|
|
|
|
where.not(Submitter.where(Submitter.arel_table[:submission_id].eq(Submission.arel_table[:id])
|
|
|
|
|
.and(Submitter.arel_table[:completed_at].eq(nil))).select(1).arel.exists)
|
|
|
|
|
}
|
2024-12-13 19:45:37 +02:00
|
|
|
scope :declined, -> { joins(:submitters).where.not(submitters: { declined_at: nil }).group(:id) }
|
|
|
|
|
scope :expired, -> { where(expire_at: ..Time.current) }
|
2023-08-05 19:20:06 +03:00
|
|
|
|
|
|
|
|
enum :source, {
|
|
|
|
|
invite: 'invite',
|
2024-04-12 17:20:00 +03:00
|
|
|
bulk: 'bulk',
|
2023-08-05 19:20:06 +03:00
|
|
|
api: 'api',
|
|
|
|
|
embed: 'embed',
|
|
|
|
|
link: 'link'
|
|
|
|
|
}, scope: false, prefix: true
|
2023-09-10 02:10:01 +03:00
|
|
|
|
|
|
|
|
enum :submitters_order, {
|
|
|
|
|
random: 'random',
|
|
|
|
|
preserved: 'preserved'
|
|
|
|
|
}, scope: false, prefix: true
|
2023-11-01 12:17:35 +02:00
|
|
|
|
2024-08-01 16:43:55 +03:00
|
|
|
def expired?
|
|
|
|
|
expire_at && expire_at <= Time.current
|
|
|
|
|
end
|
|
|
|
|
|
2023-11-01 12:17:35 +02:00
|
|
|
def audit_trail_url
|
2023-12-03 13:03:36 +02:00
|
|
|
return if audit_trail.blank?
|
|
|
|
|
|
2024-02-18 11:26:30 +02:00
|
|
|
ActiveStorage::Blob.proxy_url(audit_trail.blob)
|
2023-11-01 12:17:35 +02:00
|
|
|
end
|
|
|
|
|
alias audit_log_url audit_trail_url
|
2024-08-23 23:46:22 +03:00
|
|
|
|
|
|
|
|
def combined_document_url
|
|
|
|
|
return if combined_document.blank?
|
|
|
|
|
|
|
|
|
|
ActiveStorage::Blob.proxy_url(combined_document.blob)
|
|
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|