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
|
|
|
|
|
2017-09-23 04:34:07 +02:00
|
|
|
def save
|
2017-09-24 05:01:18 +02:00
|
|
|
params.require(:wizard)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-09-24 05:01:18 +02:00
|
|
|
wizard = ::JSON.parse(params[:wizard])
|
2018-06-06 14:05:45 +02:00
|
|
|
existing = PluginStore.get('custom_wizard', wizard['id']) || {}
|
2018-06-06 15:49:28 +02:00
|
|
|
new_time = false
|
2017-10-30 07:24:51 +01:00
|
|
|
error = nil
|
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
if wizard["id"].blank?
|
2017-10-30 07:24:51 +01:00
|
|
|
error = 'id_required'
|
2017-11-01 05:21:14 +01:00
|
|
|
elsif wizard["name"].blank?
|
2017-10-30 07:24:51 +01:00
|
|
|
error = 'name_required'
|
2017-11-01 05:21:14 +01:00
|
|
|
elsif wizard["steps"].blank?
|
2017-10-30 07:24:51 +01:00
|
|
|
error = 'steps_required'
|
2017-11-01 05:21:14 +01:00
|
|
|
elsif wizard["after_time"]
|
2018-06-06 14:05:45 +02:00
|
|
|
if !wizard["after_time_scheduled"] && !existing["after_time_scheduled"]
|
2017-11-01 05:21:14 +01:00
|
|
|
error = 'after_time_need_time'
|
|
|
|
else
|
|
|
|
after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
|
2018-06-06 15:49:28 +02:00
|
|
|
|
|
|
|
new_time = existing['after_time_scheduled'] ?
|
|
|
|
after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc :
|
|
|
|
true
|
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
begin
|
2018-06-06 15:49:28 +02:00
|
|
|
if new_time && after_time_scheduled < Time.now.utc
|
2017-11-01 05:21:14 +01:00
|
|
|
error = 'after_time_invalid'
|
|
|
|
end
|
|
|
|
rescue ArgumentError
|
|
|
|
error = 'after_time_invalid'
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 07:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return render json: { error: error } if error
|
|
|
|
|
|
|
|
wizard["steps"].each do |s|
|
|
|
|
if s["id"].blank?
|
|
|
|
error = 'id_required'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if s["fields"] && s["fields"].present?
|
|
|
|
s["fields"].each do |f|
|
|
|
|
if f["id"].blank?
|
|
|
|
error = 'id_required'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2017-11-23 10:02:25 +01:00
|
|
|
if f["type"].blank?
|
|
|
|
error = 'type_required'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2017-10-30 07:24:51 +01:00
|
|
|
if f["type"] === 'dropdown'
|
|
|
|
choices = f["choices"]
|
2020-03-19 08:58:45 +01:00
|
|
|
if (!choices || choices.length < 1) && !f["choices_key"]
|
2017-10-30 07:24:51 +01:00
|
|
|
error = 'field.need_choices'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if s["actions"] && s["actions"].present?
|
|
|
|
s["actions"].each do |a|
|
|
|
|
if a["id"].blank?
|
|
|
|
error = 'id_required'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return render json: { error: error } if error
|
|
|
|
|
2017-11-02 03:22:13 +01:00
|
|
|
## end of error checks
|
|
|
|
|
|
|
|
wizard['steps'].each do |s|
|
2017-11-03 09:22:50 +01:00
|
|
|
s['description'] = PrettyText.cook(s['raw_description']) if s['raw_description']
|
2017-11-02 03:22:13 +01:00
|
|
|
end
|
|
|
|
|
2017-11-01 10:50:03 +01:00
|
|
|
if wizard['after_time'] && new_time
|
2018-04-24 02:55:03 +02:00
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
|
2017-11-01 05:21:14 +01:00
|
|
|
Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
|
|
|
|
end
|
|
|
|
|
|
|
|
if existing['after_time'] && !wizard['after_time']
|
2018-04-24 02:55:03 +02:00
|
|
|
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
|
2017-11-01 05:21:14 +01:00
|
|
|
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
|
|
|
|
end
|
|
|
|
|
2018-07-06 03:09:38 +02:00
|
|
|
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2020-04-01 14:16:26 +02:00
|
|
|
render json: success_json.merge(wizard: wizard)
|
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
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|