Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

add assignee link to wp resource signaling (#10304)

ulfertsc694b20782b1
diff --git a/lib/api/decorators/sql/hal_associated_resource.rb b/lib/api/decorators/sql/hal_associated_resource.rb
new file mode 100644
index 00000000000..255cb7bc414
--- /dev/null
+++ b/lib/api/decorators/sql/hal_associated_resource.rb
@@ -0,0 +1,89 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+
+module API
+ module Decorators
+ module Sql
+ module HalAssociatedResource
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def associated_user_link(name,
+ column_name: "#{name}_id")
+ plural_name = name.to_s.pluralize
+
+ link name,
+ href: associated_user_link_href(plural_name, column_name),
+ title: associated_user_link_title(plural_name),
+ join: associated_user_link_join(plural_name, column_name)
+ end
+
+ private
+
+ def associated_user_link_href(table_name, column_name)
+ ->(*) {
+ <<~SQL.squish
+ CASE #{table_name}_type
+ WHEN 'Group' THEN format('#{api_v3_paths.group('%s')}', #{column_name})
+ WHEN 'PlaceholderUser' THEN format('#{api_v3_paths.placeholder_user('%s')}', #{column_name})
+ ELSE format('#{api_v3_paths.user('%s')}', #{column_name})
+ END
+ SQL
+ }
+ end
+
+ def associated_user_link_title(table_name)
+ -> {
+ join_string = if Setting.user_format == :lastname_coma_firstname
+ " || ', ' || "
+ else
+ " || ' ' || "
+ end
+
+ <<~SQL.squish
+ CASE #{table_name}_type
+ WHEN 'Group' THEN lastname
+ WHEN 'PlaceholderUser' THEN lastname
+ ELSE #{User::USER_FORMATS_STRUCTURE[Setting.user_format].join(join_string)}
+ END
+ SQL
+ }
+ end
+
+ def associated_user_link_join(table_name, column_name)
+ { table: :users,
+ condition: "#{table_name}.id = #{column_name}",
+ select: ["#{table_name}.firstname",
+ "#{table_name}.lastname",
+ "#{table_name}.login",
+ "#{table_name}.mail",
+ "#{table_name}.type #{table_name}_type"] }
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/capabilities/capability_sql_representer.rb b/lib/api/v3/capabilities/capability_sql_representer.rb
index ed64f71b66f..ab5e68552d4 100644
--- a/lib/api/v3/capabilities/capability_sql_representer.rb
+++ b/lib/api/v3/capabilities/capability_sql_representer.rb
@@ -31,6 +31,7 @@ module API
module Capabilities
class CapabilitySqlRepresenter
include API::Decorators::Sql::Hal
+ include API::Decorators::Sql::HalAssociatedResource
property :_type,
representation: ->(*) { "'Capability'" }
@@ -82,38 +83,7 @@ module API
condition: "contexts.id = capabilities.context_id",
select: ['contexts.name context_name'] }
- link :principal,
- href: ->(*) {
- <<~SQL.squish
- CASE principal_type
- WHEN 'Group' THEN format('#{api_v3_paths.group('%s')}', principal_id)
- WHEN 'PlaceholderUser' THEN format('#{api_v3_paths.placeholder_user('%s')}', principal_id)
- ELSE format('#{api_v3_paths.user('%s')}', principal_id)
- END
- SQL
- },
- title: -> {
- join_string = if Setting.user_format == :lastname_coma_firstname
- " || ', ' || "
- else
- " || ' ' || "
- end
-
- <<~SQL.squish
- CASE principal_type
- WHEN 'Group' THEN lastname
- WHEN 'PlaceholderUser' THEN lastname
- ELSE #{User::USER_FORMATS_STRUCTURE[Setting.user_format].map { |p| p }.join(join_string)}
- END
- SQL
- },
- join: { table: :users,
- condition: "principals.id = capabilities.principal_id",
- select: ['principals.firstname',
- 'principals.lastname',
- 'principals.login',
- 'principals.mail',
- 'principals.type principal_type'] }
+ associated_user_link :principal
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_sql_representer.rb b/lib/api/v3/work_packages/work_package_sql_representer.rb
index eec11f0ee3d..02b47a5f548 100644
--- a/lib/api/v3/work_packages/work_package_sql_representer.rb
+++ b/lib/api/v3/work_packages/work_package_sql_representer.rb
@@ -29,6 +29,7 @@ module API
module WorkPackages
class WorkPackageSqlRepresenter
include API::Decorators::Sql::Hal
+ include API::Decorators::Sql::HalAssociatedResource
link :self,
path: { api: :work_package, params: %w(id) },
@@ -43,6 +44,9 @@ module API
condition: 'projects.id = project_id',
select: ['projects.name project_name'] }
+ associated_user_link :assignee,
+ column_name: :assigned_to_id
+
property :_type,
representation: ->(*) { "'WorkPackage'" }
diff --git a/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
index a573f5e00b6..d24b52ea88e 100644
--- a/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
@@ -43,8 +43,13 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
.where(id: rendered_work_package.id)
end
- let(:rendered_work_package) { create(:work_package, project: project) }
+ let(:rendered_work_package) do
+ create(:work_package,
+ project: project,
+ assigned_to: assignee)
+ end
let(:project) { create(:project) }
+ let(:assignee) { create(:user) }
let(:select) { { '*' => {} } }
@@ -66,6 +71,10 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
project: {
href: api_v3_paths.project(project.id),
title: project.name
+ },
+ assignee: {
+ href: api_v3_paths.user(assignee.id),
+ title: assignee.name
}
}
}
@@ -76,4 +85,66 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
.to be_json_eql(expected.to_json)
end
end
+
+ describe 'assignee link' do
+ let(:select) { { 'assignee' => {} } }
+
+ context 'with a user' do
+ let(:expected) do
+ {
+ _links: {
+ assignee: {
+ href: api_v3_paths.user(assignee.id),
+ title: assignee.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
+ context 'with a group' do
+ let(:assignee) { create(:group) }
+
+ let(:expected) do
+ {
+ _links: {
+ assignee: {
+ href: api_v3_paths.group(assignee.id),
+ title: assignee.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
+ context 'with a placeholder user' do
+ let(:assignee) { create(:placeholder_user) }
+
+ let(:expected) do
+ {
+ _links: {
+ assignee: {
+ href: api_v3_paths.placeholder_user(assignee.id),
+ title: assignee.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+ end
end