Angus McLeod преди 7 години
родител
ревизия
11953055fd

+ 1 - 1
assets/javascripts/discourse/models/custom-wizard.js.es6

@@ -24,7 +24,7 @@ const CustomWizard = Discourse.Model.extend({
         if (value) wizard[p] = value;
       });
 
-      if (wizard['after_time'] && wizard['after_time_scheduled']) {
+      if (wizard['after_time'] && !wizard['after_time_scheduled']) {
         return reject({ error: 'after_time_need_time' });
       };
 

+ 17 - 15
assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6

@@ -2,26 +2,28 @@ import CustomWizard from '../models/custom-wizard';
 
 export default Discourse.Route.extend({
   model(params) {
-    return Ember.RSVP.hash({
-      submissions: CustomWizard.submissions(params.wizard_id),
-      wizard: this.modelFor('admin-wizards-submissions').findBy('id', params.wizard_id)
-    });
+    return CustomWizard.submissions(params.wizard_id);
   },
 
   setupController(controller, model) {
-    let fields = ['user'];
-
-    model.wizard.steps.forEach((s) => {
-      if (s.fields) {
-        s.fields.forEach((f) => {
-          fields.push(f.id);
-        });
-      };
+    let fields = [];
+    model.forEach((s) => {
+      Object.keys(s).forEach((k) => {
+        if (fields.indexOf(k) < 0) {
+          fields.push(k);
+        }
+      });
     });
 
-    controller.setProperties({
-      submissions: model.submissions,
-      fields
+    let submissions = [];
+    model.forEach((s) => {
+      let submission = {};
+      fields.forEach((f) => {
+        submission[f] = s[f];
+      });
+      submissions.push(submission);
     });
+
+    controller.setProperties({ submissions, fields });
   }
 });

+ 14 - 5
controllers/admin.rb

@@ -75,14 +75,18 @@ class CustomWizard::AdminController < ::ApplicationController
 
     return render json: { error: error } if error
 
-    existing = PluginStore.get('custom_wizard', params[:id])
+    existing = PluginStore.get('custom_wizard', wizard['id']) || {}
+    new_time = existing['after_time_scheduled'] ?
+               after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc :
+               true
 
-    if wizard['after_time'] && after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc
+    if wizard['after_time'] && new_time
       Jobs.cancel_scheduled_job(:set_after_time_wizard)
       Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
     end
 
     if existing['after_time'] && !wizard['after_time']
+      Jobs.cancel_scheduled_job(:set_after_time_wizard)
       Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
     end
 
@@ -97,6 +101,7 @@ class CustomWizard::AdminController < ::ApplicationController
     wizard = PluginStore.get('custom_wizard', params[:id])
 
     if wizard['after_time']
+      Jobs.cancel_scheduled_job(:set_after_time_wizard)
       Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
     end
 
@@ -124,10 +129,14 @@ class CustomWizard::AdminController < ::ApplicationController
   def submissions
     params.require(:wizard_id)
 
-    rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order(:id)
+    rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC')
 
-    submissions = [*rows].map { |r| ::JSON.parse(r.value) }.flatten
+    all_submissions = [*rows].map do |r|
+      submissions = ::JSON.parse(r.value)
+      username = User.find(r.key).username
+      submissions.map { |s| { username: username }.merge!(s) }
+    end.flatten
 
-    render json: success_json.merge(submissions: submissions)
+    render json: success_json.merge(submissions: all_submissions)
   end
 end

+ 1 - 1
jobs/clear_next_session_wizard.rb

@@ -1,5 +1,5 @@
 module Jobs
-  class ClearNextSessionWizard < Jobs::Base
+  class ClearAfterTimeWizard < Jobs::Base
     sidekiq_options queue: 'critical'
 
     def execute(args)

+ 1 - 1
jobs/set_next_session_wizard.rb

@@ -1,5 +1,5 @@
 module Jobs
-  class SetNextSessionWizard < Jobs::Base
+  class SetAfterTimeWizard < Jobs::Base
     def execute(args)
       if PluginStoreRow.exists?(plugin_name: 'custom_wizard', key: args[:wizard_id])
         user_ids = []

+ 14 - 6
lib/builder.rb

@@ -103,6 +103,7 @@ class CustomWizard::Builder
             submission = @submissions.last || {}
             step_input = updater.fields || {}
             user = @wizard.user
+            final_step = updater.step.next.nil?
 
             if s['fields'] && s['fields'].length
               s['fields'].each do |f|
@@ -225,10 +226,6 @@ class CustomWizard::Builder
               end
             end
 
-            if updater.errors.empty?
-              updater.result = { redirect_to: data['redirect_to'] }
-            end
-
             if @wizard.save_submissions && updater.errors.empty?
               if step_input
                 step_input.each do |key, value|
@@ -236,6 +233,10 @@ class CustomWizard::Builder
                 end
               end
 
+              if final_step
+                data['submitted_at'] = Time.now.iso8601
+              end
+
               if data.present?
                 @submissions.pop(1) if @wizard.unfinished?
                 @submissions.push(data)
@@ -244,14 +245,21 @@ class CustomWizard::Builder
             end
 
             # Ensure there is no submission left over after the user has completed a wizard with save_submissions off
-            if !@wizard.save_submissions && updater.step.next.nil?
+            if !@wizard.save_submissions && final_step
               PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
             end
 
-            if @wizard.after_time && updater.step.next.nil?
+            if @wizard.after_time && final_step
               @wizard.user.custom_fields.delete('redirect_to_wizard');
               @wizard.user.save_custom_fields(true)
             end
+
+            if updater.errors.empty?
+              # If the user will be redirected to a new wizard send them there straight away
+              user_redirect = user.custom_fields['redirect_to_wizard']
+              redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
+              updater.result = { redirect_to: redirect_to } if redirect_to
+            end
           end
         end
       end

+ 1 - 1
plugin.rb

@@ -98,7 +98,7 @@ after_initialize do
 
     def redirect_to_wizard_if_required
       @wizard_id ||= current_user.custom_fields['redirect_to_wizard']
-      if @wizard_id && request.original_url !~ /w/ && request.original_url !~ /admin/
+      if @wizard_id && request.referer !~ /w/ && request.referer !~ /admin/
         redirect_to "/w/#{@wizard_id}"
       end
     end