2017-09-23 04:34:07 +02:00
|
|
|
class CustomWizard::AdminController < ::ApplicationController
|
2017-09-29 13:27:03 +02:00
|
|
|
before_action :ensure_logged_in
|
|
|
|
before_action :ensure_admin
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
def index
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
def field_types
|
2017-10-09 07:52:09 +02:00
|
|
|
render json: { types: CustomWizard::Field.types }
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
def save
|
|
|
|
result = build_wizard
|
|
|
|
|
|
|
|
if result[:error]
|
|
|
|
render json: { error: result[:error] }
|
|
|
|
else
|
|
|
|
wizard = result[:wizard]
|
|
|
|
existing_wizard = result[:existing_wizard]
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
|
|
|
|
|
|
|
if wizard['after_time'] && result[:new_after_time]
|
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
|
|
|
|
Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
|
2017-11-01 05:21:14 +01:00
|
|
|
end
|
2017-10-30 07:24:51 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if existing_wizard && existing_wizard['after_time'] && !wizard['after_time']
|
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
|
|
|
|
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
|
2017-10-30 07:24:51 +01:00
|
|
|
end
|
|
|
|
end
|
2020-04-08 04:52:07 +02:00
|
|
|
|
|
|
|
render json: success_json.merge(wizard: wizard)
|
2017-11-01 05:21:14 +01:00
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
2017-09-24 05:01:18 +02:00
|
|
|
params.require(:id)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
wizard = PluginStore.get('custom_wizard', params[:id])
|
|
|
|
|
|
|
|
if wizard['after_time']
|
2017-11-01 10:50:03 +01:00
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard)
|
2017-11-01 05:21:14 +01:00
|
|
|
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
|
|
|
|
end
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
PluginStore.remove('custom_wizard', params[:id])
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
def find_wizard
|
|
|
|
params.require(:wizard_id)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-10-07 04:27:38 +02:00
|
|
|
wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
render json: success_json.merge(wizard: wizard)
|
|
|
|
end
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
def custom_wizards
|
2017-09-29 13:27:03 +02:00
|
|
|
rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-10-15 05:58:22 +02:00
|
|
|
wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) }
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
render json: success_json.merge(wizards: wizards)
|
|
|
|
end
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-10-22 05:37:58 +02:00
|
|
|
def submissions
|
2017-10-05 02:36:46 +02:00
|
|
|
params.require(:wizard_id)
|
|
|
|
|
2017-11-01 10:50:03 +01:00
|
|
|
rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC')
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-11-01 10:50:03 +01:00
|
|
|
all_submissions = [*rows].map do |r|
|
|
|
|
submissions = ::JSON.parse(r.value)
|
2018-06-29 08:07:18 +02:00
|
|
|
|
|
|
|
if user = User.find_by(id: r.key)
|
|
|
|
username = user.username
|
|
|
|
else
|
|
|
|
username = I18n.t('admin.wizard.submissions.no_user', id: r.key)
|
|
|
|
end
|
|
|
|
|
2018-05-24 14:54:36 +02:00
|
|
|
submissions.map { |s| { username: username }.merge!(s.except("redirect_to")) }
|
2017-11-01 10:50:03 +01:00
|
|
|
end.flatten
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-11-01 10:50:03 +01:00
|
|
|
render json: success_json.merge(submissions: all_submissions)
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
2020-04-08 04:52:07 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def wizard_params
|
|
|
|
params.require(:wizard)
|
|
|
|
params[:wizard]
|
|
|
|
end
|
|
|
|
|
|
|
|
def required_properties
|
|
|
|
{
|
|
|
|
wizard: ['id', 'name', 'steps'],
|
|
|
|
step: ['id'],
|
|
|
|
field: ['id', 'type'],
|
|
|
|
action: ['id', 'type']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependent_properties
|
|
|
|
{
|
|
|
|
after_time: 'after_time_scheduled'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_required(object, type, error)
|
|
|
|
object.each do |property, value|
|
|
|
|
required = required_properties[type].include?(property)
|
|
|
|
|
|
|
|
if required && property.blank?
|
|
|
|
error = {
|
|
|
|
type: 'required',
|
|
|
|
params: { property: property }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_depdendent(object, error)
|
|
|
|
object.each do |property, value|
|
|
|
|
dependent = dependent_properties[property]
|
|
|
|
|
|
|
|
if dependent && object[dependent].blank?
|
|
|
|
error = {
|
|
|
|
type: 'dependent',
|
|
|
|
params: { dependent: dependent, property: property }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_wizard(wizard)
|
|
|
|
error = nil
|
|
|
|
|
|
|
|
error = check_required(wizard, :wizard, error)
|
|
|
|
error = check_depdendent(wizard, error)
|
|
|
|
|
|
|
|
wizard['steps'].each do |step|
|
|
|
|
error = check_required(step, :step, error)
|
|
|
|
error = check_depdendent(step, error)
|
|
|
|
break if error.present?
|
|
|
|
|
|
|
|
step['fields'].each do |field|
|
|
|
|
error = check_required(field, :field, error)
|
|
|
|
error = check_depdendent(field, error)
|
|
|
|
break if error.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
wizard['actions'].each do |action|
|
|
|
|
error = check_required(action, :action, error)
|
|
|
|
error = check_depdendent(action, error)
|
|
|
|
break if error.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
if error
|
|
|
|
{ error: error }
|
|
|
|
else
|
|
|
|
{ success: true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_after_time(wizard, existing_wizard)
|
|
|
|
new = false
|
|
|
|
error = nil
|
|
|
|
|
|
|
|
if wizard["after_time"]
|
|
|
|
if !wizard["after_time_scheduled"] && !existing_wizard["after_time_scheduled"]
|
|
|
|
error = 'after_time_need_time'
|
|
|
|
else
|
|
|
|
after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
|
|
|
|
|
|
|
|
new = existing_wizard['after_time_scheduled'] ?
|
|
|
|
after_time_scheduled != Time.parse(existing_wizard['after_time_scheduled']).utc :
|
|
|
|
true
|
|
|
|
|
|
|
|
begin
|
|
|
|
error = 'after_time_invalid' if new && after_time_scheduled < Time.now.utc
|
|
|
|
rescue ArgumentError
|
|
|
|
error = 'after_time_invalid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if error
|
|
|
|
{ error: { type: error } }
|
|
|
|
else
|
|
|
|
{ new: new }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_wizard
|
|
|
|
wizard = ::JSON.parse(wizard_params)
|
|
|
|
existing_wizard = PluginStore.get('custom_wizard', wizard['id']) || {}
|
|
|
|
|
|
|
|
validation = validate_wizard(wizard)
|
|
|
|
return validation[:error] if validation[:error]
|
|
|
|
|
|
|
|
after_time_validation = validate_after_time(wizard, existing_wizard)
|
|
|
|
return after_time_validation[:error] if after_time_validation[:error]
|
|
|
|
|
|
|
|
wizard['steps'].each do |step|
|
|
|
|
if s['raw_description']
|
|
|
|
step['description'] = PrettyText.cook(s['raw_description'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result = {
|
|
|
|
wizard: wizard,
|
|
|
|
existing_wizard: existing_wizard,
|
|
|
|
new_after_time: after_time_validation[:new]
|
|
|
|
}
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|