FEATURE: Add endpoint to list all assignments, sorted by user

FEATURE: Add endpoint to list all assignments, sorted by user

Includes a fix to let all admin users to use plugin

diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb
index 1553902..628a87d 100644
--- a/app/controllers/discourse_assign/assign_controller.rb
+++ b/app/controllers/discourse_assign/assign_controller.rb
@@ -82,6 +82,38 @@ module DiscourseAssign
       end
     end
 
+    def assigned
+      offset = (params[:offset] || 0).to_i
+      limit = (params[:limit] || 100).to_i
+
+      topics = Topic
+        .includes(:tags)
+        .includes(:user)
+        .joins("JOIN topic_custom_fields tcf ON topics.id = tcf.topic_id AND tcf.name = 'assigned_to_id' AND tcf.value IS NOT NULL")
+        .order('tcf.value')
+        .offset(offset)
+        .limit(limit)
+
+      Topic.preload_custom_fields(topics, [TopicAssigner::ASSIGNED_TO_ID])
+
+      users = User
+        .where("users.id IN (SELECT value::int FROM topic_custom_fields WHERE name = 'assigned_to_id' AND topic_id IN (?))", topics.map(&:id))
+        .joins('join user_emails on user_emails.user_id = users.id AND user_emails.primary')
+        .select(AvatarLookup.lookup_columns)
+        .to_a
+
+      User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))
+
+      users = users.to_h { |u| [u.id, u] }
+      topics.each do |t|
+        if id = t.custom_fields[TopicAssigner::ASSIGNED_TO_ID]
+          t.preload_assigned_to_user(users[id.to_i])
+        end
+      end
+
+      render json: { topics: serialize_data(topics, AssignedTopicSerializer) }
+    end
+
     private
 
     def translate_failure(reason, user)
diff --git a/app/serializers/assigned_topic_serializer.rb b/app/serializers/assigned_topic_serializer.rb
new file mode 100644
index 0000000..8a3530f
--- /dev/null
+++ b/app/serializers/assigned_topic_serializer.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class AssignedTopicSerializer < BasicTopicSerializer
+  include TopicTagsMixin
+
+  attributes :excerpt,
+             :category_id,
+             :created_at,
+             :updated_at
+
+  has_one :user, serializer: BasicUserSerializer, embed: :objects
+  has_one :assigned_to_user, serializer: AssignedUserSerializer, embed: :objects
+end
diff --git a/app/serializers/assigned_user_serializer.rb b/app/serializers/assigned_user_serializer.rb
new file mode 100644
index 0000000..bbf2b62
--- /dev/null
+++ b/app/serializers/assigned_user_serializer.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class AssignedUserSerializer < BasicUserSerializer
+  attributes :custom_fields
+
+  def custom_fields
+    fields = User.whitelisted_user_custom_fields(scope)
+
+    result = {}
+    fields.each do |k|
+      result[k] = object.custom_fields[k] if object.custom_fields[k].present?
+    end
+
+    result
+  end
+
+end
diff --git a/config/routes.rb b/config/routes.rb
index 766057d..575166e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -5,4 +5,5 @@ DiscourseAssign::Engine.routes.draw do
   put "/assign" => "assign#assign"
   put "/unassign" => "assign#unassign"
   get "/suggestions" => "assign#suggestions"
+  get "/assigned" => "assign#assigned"
 end
diff --git a/plugin.rb b/plugin.rb
index 6b3af49..20ccb15 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -57,6 +57,7 @@ after_initialize do
   add_to_class(:user, :can_assign?) do
     @can_assign ||=
       begin
+        return true if admin?
         allowed_groups = SiteSetting.assign_allowed_on_groups.split('|').compact
         allowed_groups.present? && groups.where("groups.#{attribute} in (?)", allowed_groups).exists? ?
           :true : :false

GitHub sha: c46764e8

1 Like

This needs a request spec.

1 Like

DEV: Add test for assigned