0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00
discourse-custom-wizard/controllers/custom_wizard/admin.rb

233 Zeilen
5,8 KiB
Ruby

2017-09-23 04:34:07 +02:00
class CustomWizard::AdminController < ::ApplicationController
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
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
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)
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
{
2020-04-10 09:57:49 +02:00
wizard: {
after_time: 'after_time_scheduled'
},
step: {},
field: {},
action: {}
2020-04-08 04:52:07 +02:00
}
end
2020-04-10 09:57:49 +02:00
def check_required(object, type, error)
required_properties[type].each do |property|
if object[property].blank?
2020-04-08 04:52:07 +02:00
error = {
type: 'required',
2020-04-10 09:57:49 +02:00
params: { type: type, property: property }
2020-04-08 04:52:07 +02:00
}
end
end
error
end
2020-04-10 09:57:49 +02:00
def check_depdendent(object, type, error)
dependent_properties[type].each do |property, dependent|
if object[property] && object[dependent].blank?
2020-04-08 04:52:07 +02:00
error = {
type: 'dependent',
2020-04-10 09:57:49 +02:00
params: { property: property, dependent: dependent }
2020-04-08 04:52:07 +02:00
}
end
end
2020-04-10 09:57:49 +02:00
2020-04-08 04:52:07 +02:00
error
end
def validate_wizard(wizard)
error = nil
error = check_required(wizard, :wizard, error)
2020-04-10 09:57:49 +02:00
error = check_depdendent(wizard, :wizard, error)
if !error
wizard['steps'].each do |step|
error = check_required(step, :step, error)
error = check_depdendent(step, :step, error)
break if error.present?
if step['fields'].present?
step['fields'].each do |field|
error = check_required(field, :field, error)
error = check_depdendent(field, :field, error)
break if error.present?
end
end
end
2020-04-08 04:52:07 +02:00
2020-04-10 09:57:49 +02:00
if wizard['actions'].present?
wizard['actions'].each do |action|
error = check_required(action, :action, error)
error = check_depdendent(action, :action, error)
2020-04-08 09:59:54 +02:00
break if error.present?
end
2020-04-08 04:52:07 +02:00
end
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)
2020-04-10 09:57:49 +02:00
return validation if validation[:error]
2020-04-08 04:52:07 +02:00
after_time_validation = validate_after_time(wizard, existing_wizard)
2020-04-10 09:57:49 +02:00
return after_time_validation if after_time_validation[:error]
2020-04-08 04:52:07 +02:00
wizard['steps'].each do |step|
2020-04-08 09:59:54 +02:00
if step['raw_description']
step['description'] = PrettyText.cook(step['raw_description'])
2020-04-08 04:52:07 +02:00
end
end
2020-04-10 09:57:49 +02:00
{
2020-04-08 04:52:07 +02:00
wizard: wizard,
existing_wizard: existing_wizard,
new_after_time: after_time_validation[:new]
}
end
2017-09-23 04:34:07 +02:00
end