DEV: Assign allowed groups are sent alongside suggestions. After the migration happens, we'll need to still have access to the allowed group names so we can search users.

DEV: Assign allowed groups are sent alongside suggestions. After the migration happens, we’ll need to still have access to the allowed group names so we can search users.

diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb
index dfae899..9dadec5 100644
--- a/app/controllers/discourse_assign/assign_controller.rb
+++ b/app/controllers/discourse_assign/assign_controller.rb
@@ -23,8 +23,10 @@ module DiscourseAssign
         .order('X.last_assigned DESC')
         .limit(6)
 
-      render json: ActiveModel::ArraySerializer.new(users,
-                                                    scope: guardian, each_serializer: BasicUserSerializer)
+      render json: { 
+        assign_allowed_on_groups: Group.assign_allowed_groups.pluck(:name),
+        suggestions: ActiveModel::ArraySerializer.new(users, scope: guardian, each_serializer: BasicUserSerializer)
+      }
     end
 
     def claim
diff --git a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
index 4a4cb35..930ece8 100644
--- a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
+++ b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6
@@ -3,14 +3,16 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
 
 export default Ember.Controller.extend({
   assignSuggestions: null,
+  allowedGroups: [],
   taskActions: Ember.inject.service(),
 
   init() {
     this._super(...arguments);
 
-    ajax("/assign/suggestions").then(users =>
-      this.set("assignSuggestions", users)
-    );
+    ajax("/assign/suggestions").then(data => {
+      this.set("assignSuggestions", data.suggestions);
+      this.set("allowedGroups", data.assign_allowed_on_groups);
+    });
   },
 
   onClose() {
diff --git a/assets/javascripts/discourse/services/task-actions.js.es6 b/assets/javascripts/discourse/services/task-actions.js.es6
index 75fe9a3..230c7c7 100644
--- a/assets/javascripts/discourse/services/task-actions.js.es6
+++ b/assets/javascripts/discourse/services/task-actions.js.es6
@@ -3,14 +3,6 @@ import showModal from "discourse/lib/show-modal";
 import { getOwner } from "discourse-common/lib/get-owner";
 
 export default Ember.Service.extend({
-  init() {
-    this._super(...arguments);
-
-    this.allowedGroups = getOwner(this)
-      .lookup("site-settings:main")
-      .assign_allowed_on_groups.split("|");
-  },
-
   unassign(topicId) {
     return ajax("/assign/unassign", {
       type: "PUT",
@@ -22,8 +14,7 @@ export default Ember.Service.extend({
     return showModal("assign-user", {
       model: {
         topic,
-        username: topic.get("assigned_to_user.username"),
-        allowedGroups: this.allowedGroups
+        username: topic.get("assigned_to_user.username")
       }
     });
   }
diff --git a/assets/javascripts/discourse/templates/modal/assign-user.hbs b/assets/javascripts/discourse/templates/modal/assign-user.hbs
index b2ad776..dd80794 100644
--- a/assets/javascripts/discourse/templates/modal/assign-user.hbs
+++ b/assets/javascripts/discourse/templates/modal/assign-user.hbs
@@ -4,7 +4,7 @@
     single=true
     allowAny=false
     group="staff"
-    groupMembersOf=model.allowedGroups
+    groupMembersOf=allowedGroups
     excludeCurrentUser=false
     includeMentionableGroups=false
     hasGroups=false
diff --git a/spec/requests/assign_controller_spec.rb b/spec/requests/assign_controller_spec.rb
index 7e64a49..f743263 100644
--- a/spec/requests/assign_controller_spec.rb
+++ b/spec/requests/assign_controller_spec.rb
@@ -48,7 +48,7 @@ RSpec.describe DiscourseAssign::AssignController do
         TopicAssigner.new(post.topic, user).assign(user2)
 
         get '/assign/suggestions.json'
-        suggestions = JSON.parse(response.body).map { |u| u['username'] }
+        suggestions = JSON.parse(response.body)['suggestions'].map { |u| u['username'] }
 
         expect(suggestions).to contain_exactly(user2.username, user.username)
       end
@@ -60,7 +60,7 @@ RSpec.describe DiscourseAssign::AssignController do
         TopicAssigner.new(post.topic, user).assign(user2)
 
         get '/assign/suggestions.json'
-        suggestions = JSON.parse(response.body).map { |u| u['username'] }
+        suggestions = JSON.parse(response.body)['suggestions'].map { |u| u['username'] }
 
         expect(suggestions).to contain_exactly(user.username)
       end
@@ -78,7 +78,7 @@ RSpec.describe DiscourseAssign::AssignController do
       TopicAssigner.new(post.topic, user).assign(another_admin)
 
       get '/assign/suggestions.json'
-      suggestions = JSON.parse(response.body).map { |u| u['username'] }
+      suggestions = JSON.parse(response.body)['suggestions'].map { |u| u['username'] }
 
       expect(suggestions).to contain_exactly(user.username)
     end

GitHub sha: 8792cf51

You need to initialize allowedGroups in init() otherwise each instance of the controller will be pointing at the same array. It’s generally best to assign null to each non-number value when creating your ember objects, then setting them up in the init() method.

2 Likes

Followed up here: Initialize allowedGroups inside the constructor

1 Like