admin.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class CustomWizard::AdminController < ::ApplicationController
  2. before_action :ensure_logged_in
  3. before_action :ensure_admin
  4. def index
  5. render nothing: true
  6. end
  7. def field_types
  8. render json: { types: CustomWizard::Field.types }
  9. end
  10. def save
  11. params.require(:wizard)
  12. wizard = ::JSON.parse(params[:wizard])
  13. error = nil
  14. if wizard["id"].blank?
  15. error = 'id_required'
  16. elsif wizard["name"].blank?
  17. error = 'name_required'
  18. elsif wizard["steps"].blank?
  19. error = 'steps_required'
  20. elsif wizard["after_time"]
  21. if !wizard["after_time_scheduled"]
  22. error = 'after_time_need_time'
  23. else
  24. after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
  25. begin
  26. if after_time_scheduled < Time.now.utc
  27. error = 'after_time_invalid'
  28. end
  29. rescue ArgumentError
  30. error = 'after_time_invalid'
  31. end
  32. end
  33. end
  34. return render json: { error: error } if error
  35. wizard["steps"].each do |s|
  36. if s["id"].blank?
  37. error = 'id_required'
  38. break
  39. end
  40. if s["fields"] && s["fields"].present?
  41. s["fields"].each do |f|
  42. if f["id"].blank?
  43. error = 'id_required'
  44. break
  45. end
  46. if f["type"] === 'dropdown'
  47. choices = f["choices"]
  48. if (!choices || choices.length < 1) && !f["choices_key"] && !f["choices_preset"]
  49. error = 'field.need_choices'
  50. break
  51. end
  52. end
  53. end
  54. end
  55. if s["actions"] && s["actions"].present?
  56. s["actions"].each do |a|
  57. if a["id"].blank?
  58. error = 'id_required'
  59. break
  60. end
  61. end
  62. end
  63. end
  64. return render json: { error: error } if error
  65. ## end of error checks
  66. wizard['steps'].each do |s|
  67. s['description'] = PrettyText.cook(s['raw_description']) if s['raw_description']
  68. end
  69. existing = PluginStore.get('custom_wizard', wizard['id']) || {}
  70. new_time = existing['after_time_scheduled'] ?
  71. after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc :
  72. true
  73. if wizard['after_time'] && new_time
  74. Jobs.cancel_scheduled_job(:set_after_time_wizard)
  75. Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
  76. end
  77. if existing['after_time'] && !wizard['after_time']
  78. Jobs.cancel_scheduled_job(:set_after_time_wizard)
  79. Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
  80. end
  81. PluginStore.set('custom_wizard', wizard["id"], wizard)
  82. render json: success_json
  83. end
  84. def remove
  85. params.require(:id)
  86. wizard = PluginStore.get('custom_wizard', params[:id])
  87. if wizard['after_time']
  88. Jobs.cancel_scheduled_job(:set_after_time_wizard)
  89. Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
  90. end
  91. PluginStore.remove('custom_wizard', params[:id])
  92. render json: success_json
  93. end
  94. def find_wizard
  95. params.require(:wizard_id)
  96. wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
  97. render json: success_json.merge(wizard: wizard)
  98. end
  99. def custom_wizards
  100. rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
  101. wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) }
  102. render json: success_json.merge(wizards: wizards)
  103. end
  104. def submissions
  105. params.require(:wizard_id)
  106. rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC')
  107. all_submissions = [*rows].map do |r|
  108. submissions = ::JSON.parse(r.value)
  109. username = User.find(r.key).username
  110. submissions.map { |s| { username: username }.merge!(s) }
  111. end.flatten
  112. render json: success_json.merge(submissions: all_submissions)
  113. end
  114. end