2020-12-04 08:05:56 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
class CustomWizard::Template
|
2020-11-03 01:24:20 +01:00
|
|
|
include HasErrors
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
AFTER_SIGNUP_CACHE_KEY ||= "after_signup_wizard_ids"
|
|
|
|
AFTER_TIME_CACHE_KEY ||= "after_time_wizard_ids"
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
attr_reader :data,
|
2021-04-20 19:58:19 +02:00
|
|
|
:opts,
|
|
|
|
:steps,
|
|
|
|
:actions
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
2021-04-20 19:58:19 +02:00
|
|
|
@steps = data['steps'] || []
|
|
|
|
@actions = data['actions'] || []
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
|
|
|
def save(opts = {})
|
2020-11-03 01:24:20 +01:00
|
|
|
@opts = opts
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
normalize_data
|
|
|
|
validate_data
|
|
|
|
prepare_data
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
return false if errors.any?
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-11-03 01:24:20 +01:00
|
|
|
schedule_save_jobs unless opts[:skip_jobs]
|
2020-12-04 08:05:56 +01:00
|
|
|
PluginStore.set(CustomWizard::PLUGIN_NAME, @data[:id], @data)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
self.class.clear_cache_keys
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
@data[:id]
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
|
|
|
def self.save(data, opts = {})
|
2020-11-03 01:24:20 +01:00
|
|
|
new(data).save(opts)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
def self.create(wizard_id)
|
|
|
|
if data = find(wizard_id)
|
|
|
|
new(data)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def self.find(wizard_id)
|
2020-12-04 08:05:56 +01:00
|
|
|
PluginStore.get(CustomWizard::PLUGIN_NAME, wizard_id)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
def self.remove(wizard_id)
|
2020-11-03 01:24:20 +01:00
|
|
|
wizard = CustomWizard::Wizard.create(wizard_id)
|
2020-11-09 04:32:36 +01:00
|
|
|
return false if !wizard
|
2021-03-11 07:30:15 +01:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2020-12-04 08:05:56 +01:00
|
|
|
PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id)
|
2022-01-31 10:18:04 +01:00
|
|
|
clear_user_wizard_redirect(wizard_id, after_time: !!wizard.after_time)
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
clear_cache_keys
|
2021-06-07 15:15:41 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
true
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
def self.exists?(wizard_id)
|
|
|
|
PluginStoreRow.exists?(plugin_name: 'custom_wizard', key: wizard_id)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
def self.list(setting: nil, query_str: nil, order: :id)
|
2020-11-03 01:24:20 +01:00
|
|
|
query = "plugin_name = 'custom_wizard'"
|
2022-01-31 10:18:04 +01:00
|
|
|
query += " AND (value::json ->> '#{setting}')::boolean IS TRUE" if setting
|
|
|
|
query += " #{query_str}" if query_str
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
PluginStoreRow.where(query).order(order)
|
2020-10-31 08:05:50 +01:00
|
|
|
.reduce([]) do |result, record|
|
|
|
|
attrs = JSON.parse(record.value)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
if attrs.present? &&
|
|
|
|
attrs.is_a?(Hash) &&
|
|
|
|
attrs['id'].present? &&
|
|
|
|
attrs['name'].present?
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
result.push(attrs)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-10-31 08:05:50 +01:00
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
def self.clear_user_wizard_redirect(wizard_id, after_time: false)
|
2021-06-07 15:15:41 +02:00
|
|
|
UserCustomField.where(name: 'redirect_to_wizard', value: wizard_id).destroy_all
|
2022-01-31 10:18:04 +01:00
|
|
|
|
|
|
|
if after_time
|
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_signup_ids
|
|
|
|
::CustomWizard::Cache.wrap(AFTER_SIGNUP_CACHE_KEY) do
|
|
|
|
list(setting: 'after_signup').map { |t| t['id'] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_time_ids
|
|
|
|
::CustomWizard::Cache.wrap(AFTER_TIME_CACHE_KEY) do
|
|
|
|
list(
|
|
|
|
setting: 'after_time',
|
|
|
|
query_str: "AND (value::json ->> 'after_time_scheduled')::timestamp < CURRENT_TIMESTAMP"
|
|
|
|
).map { |t| t['id'] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.can_redirect_users?(wizard_id)
|
|
|
|
after_signup_ids.include?(wizard_id) || after_time_ids.include?(wizard_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache_keys
|
|
|
|
CustomWizard::Cache.new(AFTER_SIGNUP_CACHE_KEY).delete
|
|
|
|
CustomWizard::Cache.new(AFTER_TIME_CACHE_KEY).delete
|
2021-06-07 15:15:41 +02:00
|
|
|
end
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
private
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def normalize_data
|
|
|
|
@data = ::JSON.parse(@data) if @data.is_a?(String)
|
|
|
|
@data = @data.with_indifferent_access
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def prepare_data
|
|
|
|
@data[:steps].each do |step|
|
|
|
|
if step[:raw_description]
|
2021-04-23 13:51:41 +02:00
|
|
|
step[:description] = step[:raw_description]
|
2020-11-03 01:24:20 +01:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
remove_non_mapped_index(step)
|
|
|
|
|
|
|
|
step[:fields].each do |field|
|
|
|
|
remove_non_mapped_index(field)
|
|
|
|
end
|
2020-11-03 01:24:20 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def validate_data
|
2020-11-26 04:05:50 +01:00
|
|
|
validator = CustomWizard::TemplateValidator.new(@data, @opts)
|
2020-11-03 01:24:20 +01:00
|
|
|
validator.perform
|
|
|
|
add_errors_from(validator)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def schedule_save_jobs
|
|
|
|
if @data[:after_time] && @data[:after_time_scheduled]
|
|
|
|
wizard_id = @data[:id]
|
|
|
|
old_data = CustomWizard::Template.find(data[:id])
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
begin
|
|
|
|
enqueue_wizard_at = Time.parse(@data[:after_time_scheduled]).utc
|
|
|
|
rescue ArgumentError
|
|
|
|
errors.add :validation, I18n.t("wizard.validation.after_time")
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
if enqueue_wizard_at
|
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard_id)
|
|
|
|
Jobs.enqueue_at(enqueue_wizard_at, :set_after_time_wizard, wizard_id: wizard_id)
|
|
|
|
elsif old_data && old_data[:after_time]
|
2022-01-31 10:18:04 +01:00
|
|
|
clear_user_wizard_redirect(wizard_id, after_time: true)
|
2020-11-03 01:24:20 +01:00
|
|
|
end
|
|
|
|
end
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
def remove_non_mapped_index(object)
|
|
|
|
if !object[:index].is_a?(Array)
|
|
|
|
object.delete(:index)
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|