2021-06-23 08:15:17 +02:00
|
|
|
# frozen_string_literal: true
|
2021-06-17 09:50:22 +02:00
|
|
|
class CustomWizard::Submission
|
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
KEY ||= "submissions"
|
2021-08-02 09:31:49 +02:00
|
|
|
META ||= %w(updated_at submitted_at route_to redirect_on_complete redirect_to)
|
2021-06-17 09:50:22 +02:00
|
|
|
|
|
|
|
attr_reader :id,
|
|
|
|
:user,
|
2021-06-23 08:13:58 +02:00
|
|
|
:user_id,
|
2021-06-17 09:50:22 +02:00
|
|
|
:wizard
|
2021-06-23 08:13:58 +02:00
|
|
|
|
|
|
|
attr_accessor :fields,
|
|
|
|
:permitted_param_keys
|
2021-06-17 09:50:22 +02:00
|
|
|
|
|
|
|
META.each do |attr|
|
|
|
|
class_eval { attr_accessor attr }
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(wizard, data = {}, user_id = nil)
|
|
|
|
@wizard = wizard
|
2021-06-23 08:13:58 +02:00
|
|
|
@user_id = user_id
|
2021-06-17 09:50:22 +02:00
|
|
|
|
|
|
|
if user_id
|
2021-06-23 08:13:58 +02:00
|
|
|
@user = User.find_by(id: user_id)
|
2021-06-17 09:50:22 +02:00
|
|
|
else
|
2021-06-23 08:15:17 +02:00
|
|
|
@user = wizard.user
|
2021-06-17 09:50:22 +02:00
|
|
|
end
|
|
|
|
|
2021-06-23 09:02:21 +02:00
|
|
|
data = (data || {}).with_indifferent_access
|
2021-06-17 09:50:22 +02:00
|
|
|
@id = data['id'] || SecureRandom.hex(12)
|
2021-07-12 09:53:58 +02:00
|
|
|
non_field_keys = META + ['id']
|
|
|
|
@fields = data.except(*non_field_keys) || {}
|
2021-06-17 09:50:22 +02:00
|
|
|
|
|
|
|
META.each do |attr|
|
|
|
|
send("#{attr}=", data[attr]) if data[attr]
|
|
|
|
end
|
2021-06-23 08:13:58 +02:00
|
|
|
|
|
|
|
@permitted_param_keys = data['permitted_param_keys'] || []
|
2021-06-17 09:50:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
return nil unless wizard.save_submissions
|
2021-06-23 08:13:58 +02:00
|
|
|
validate
|
2021-06-17 09:50:22 +02:00
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
submission_list = self.class.list(wizard, user_id: user.id)
|
|
|
|
submissions = submission_list.select { |submission| submission.id != self.id }
|
2021-08-02 09:31:49 +02:00
|
|
|
self.updated_at = Time.now.iso8601
|
2021-06-17 09:50:22 +02:00
|
|
|
submissions.push(self)
|
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
submission_data = submissions.map { |submission| data_to_save(submission) }
|
2021-06-17 09:50:22 +02:00
|
|
|
PluginStore.set("#{wizard.id}_#{KEY}", user.id, submission_data)
|
|
|
|
end
|
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
def validate
|
|
|
|
self.fields = fields.select { |key, value| validate_field_key(key) }
|
2021-06-17 09:50:22 +02:00
|
|
|
end
|
2021-06-23 08:13:58 +02:00
|
|
|
|
|
|
|
def validate_field_key(key)
|
|
|
|
wizard.field_ids.include?(key) ||
|
|
|
|
wizard.action_ids.include?(key) ||
|
|
|
|
permitted_param_keys.include?(key)
|
|
|
|
end
|
|
|
|
|
2021-06-17 09:50:22 +02:00
|
|
|
def fields_and_meta
|
|
|
|
result = fields
|
|
|
|
|
|
|
|
META.each do |attr|
|
|
|
|
if value = self.send(attr)
|
|
|
|
result[attr] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
def present?
|
|
|
|
fields_and_meta.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def data_to_save(submission)
|
|
|
|
data = {
|
|
|
|
id: submission.id
|
|
|
|
}
|
|
|
|
|
|
|
|
data.merge!(submission.fields_and_meta)
|
|
|
|
|
|
|
|
if submission.permitted_param_keys.present?
|
|
|
|
data[:permitted_param_keys] = submission.permitted_param_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2021-06-17 09:50:22 +02:00
|
|
|
def self.get(wizard, user_id)
|
|
|
|
data = PluginStore.get("#{wizard.id}_#{KEY}", user_id).first
|
|
|
|
new(wizard, data, user_id)
|
|
|
|
end
|
|
|
|
|
2021-08-10 11:01:59 +02:00
|
|
|
def self.remove(submission)
|
|
|
|
if submission.present?
|
|
|
|
user_id = submission.user.id
|
|
|
|
wizard_id = submission.wizard.id
|
|
|
|
submission_id = submission.id
|
|
|
|
data = PluginStore.get("#{wizard_id}_#{KEY}", user_id)
|
2021-08-10 11:05:56 +02:00
|
|
|
data.delete_if { |sub| sub["id"] == submission_id }
|
2021-08-10 11:01:59 +02:00
|
|
|
PluginStore.set("#{wizard_id}_#{KEY}", user_id, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
self.class.remove(self)
|
|
|
|
end
|
|
|
|
|
2021-08-02 09:31:49 +02:00
|
|
|
def self.cleanup_incomplete_submissions(wizard)
|
|
|
|
user_id = wizard.user.id
|
|
|
|
all_submissions = list(wizard, user_id: user_id)
|
|
|
|
sorted_submissions = all_submissions.sort_by do |submission|
|
|
|
|
zero_epoch_time = DateTime.strptime("0", '%s')
|
|
|
|
[
|
|
|
|
submission.submitted_at ? Time.iso8601(submission.submitted_at) : zero_epoch_time,
|
|
|
|
submission.updated_at ? Time.iso8601(submission.updated_at) : zero_epoch_time
|
|
|
|
]
|
|
|
|
end.reverse
|
|
|
|
|
|
|
|
has_incomplete = false
|
|
|
|
valid_submissions = sorted_submissions.select do |submission|
|
|
|
|
to_be_included = submission.submitted_at || !has_incomplete
|
|
|
|
has_incomplete = true if !submission.submitted_at
|
|
|
|
|
|
|
|
to_be_included
|
|
|
|
end
|
|
|
|
|
|
|
|
valid_data = valid_submissions.map { |submission| submission.data_to_save(submission) }
|
|
|
|
PluginStore.set("#{wizard.id}_#{KEY}", user_id, valid_data)
|
|
|
|
end
|
|
|
|
|
2021-06-17 09:50:22 +02:00
|
|
|
def self.list(wizard, user_id: nil, order_by: nil)
|
|
|
|
params = { plugin_name: "#{wizard.id}_#{KEY}" }
|
|
|
|
params[:key] = user_id if user_id.present?
|
|
|
|
|
|
|
|
query = PluginStoreRow.where(params)
|
|
|
|
query = query.order("#{order_by} DESC") if order_by.present?
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
query.each do |record|
|
|
|
|
if (submission_data = ::JSON.parse(record.value)).any?
|
|
|
|
submission_data.each do |data|
|
|
|
|
result.push(new(wizard, data, record.key))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2021-06-23 08:15:17 +02:00
|
|
|
end
|