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

Includes a fix to let all admin users to use plugin
This commit is contained in:
Bianca Nenciu 2019-09-16 13:41:10 +03:00 committed by GitHub
parent 653a529dd9
commit c46764e804
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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