admin.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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"] || wizard["id"].empty?
  15. error = 'id_required'
  16. elsif !wizard["name"] || wizard["name"].empty?
  17. error = 'name_required'
  18. elsif !wizard["steps"] || wizard["steps"].empty?
  19. error = 'steps_required'
  20. end
  21. return render json: { error: error } if error
  22. wizard["steps"].each do |s|
  23. puts "HERE IS THE ID: #{s["id"]}"
  24. if s["id"].blank?
  25. error = 'id_required'
  26. break
  27. end
  28. if s["fields"] && s["fields"].present?
  29. s["fields"].each do |f|
  30. if f["id"].blank?
  31. error = 'id_required'
  32. break
  33. end
  34. if f["type"] === 'dropdown'
  35. choices = f["choices"]
  36. if (!choices || choices.length < 1) && !f["choices_key"] && !f["choices_categories"]
  37. error = 'field.need_choices'
  38. break
  39. end
  40. end
  41. end
  42. end
  43. if s["actions"] && s["actions"].present?
  44. s["actions"].each do |a|
  45. if a["id"].blank?
  46. error = 'id_required'
  47. break
  48. end
  49. end
  50. end
  51. end
  52. return render json: { error: error } if error
  53. PluginStore.set('custom_wizard', wizard["id"], wizard)
  54. render json: success_json
  55. end
  56. def remove
  57. params.require(:id)
  58. PluginStore.remove('custom_wizard', params[:id])
  59. render json: success_json
  60. end
  61. def find_wizard
  62. params.require(:wizard_id)
  63. wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
  64. render json: success_json.merge(wizard: wizard)
  65. end
  66. def custom_wizards
  67. rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
  68. wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) }
  69. render json: success_json.merge(wizards: wizards)
  70. end
  71. def submissions
  72. params.require(:wizard_id)
  73. rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order(:id)
  74. submissions = [*rows].map { |r| ::JSON.parse(r.value) }.flatten
  75. render json: success_json.merge(submissions: submissions)
  76. end
  77. end