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 specific api end points for allowed work packages for time entries

ulferts51b1ef026c47
diff --git a/app/services/api/v3/work_package_collection_from_query_params_service.rb b/app/services/api/v3/work_package_collection_from_query_params_service.rb
index 6eab9d8549f..1f450f130df 100644
--- a/app/services/api/v3/work_package_collection_from_query_params_service.rb
+++ b/app/services/api/v3/work_package_collection_from_query_params_service.rb
@@ -29,21 +29,23 @@
module API
module V3
class WorkPackageCollectionFromQueryParamsService
- def initialize(user)
+ def initialize(user, scope: nil)
self.current_user = user
+ self.scope = scope
end
def call(params = {})
query = Query.new_default(name: '_', project: params[:project])
WorkPackageCollectionFromQueryService
- .new(query, current_user)
+ .new(query, current_user, scope: scope)
.call(params)
end
private
- attr_accessor :current_user
+ attr_accessor :current_user,
+ :scope
end
end
end
diff --git a/app/services/api/v3/work_package_collection_from_query_service.rb b/app/services/api/v3/work_package_collection_from_query_service.rb
index be3615a6d79..cd70f7cdaa6 100644
--- a/app/services/api/v3/work_package_collection_from_query_service.rb
+++ b/app/services/api/v3/work_package_collection_from_query_service.rb
@@ -32,9 +32,10 @@ module API
include Utilities::PathHelper
include ::API::Utilities::PageSizeHelper
- def initialize(query, user)
+ def initialize(query, user, scope: nil)
self.query = query
self.current_user = user
+ self.scope = scope
end
def call(params = {}, valid_subset: false)
@@ -54,7 +55,13 @@ module API
private
def results_to_representer(params)
- collection_representer(query.results.sorted_work_packages,
+ results_scope = query.results.sorted_work_packages
+
+ if scope
+ results_scope = results_scope.where(id: scope.select(:id))
+ end
+
+ collection_representer(results_scope,
params: params,
project: query.project,
groups: generate_groups,
@@ -62,7 +69,8 @@ module API
end
attr_accessor :query,
- :current_user
+ :current_user,
+ :scope
def representer
::API::V3::WorkPackages::WorkPackageCollectionRepresenter
diff --git a/lib/api/v3/time_entries/available_work_packages_helper.rb b/lib/api/v3/time_entries/available_work_packages_helper.rb
index e69de29bb2d..3364b2918af 100644
--- a/lib/api/v3/time_entries/available_work_packages_helper.rb
+++ b/lib/api/v3/time_entries/available_work_packages_helper.rb
@@ -0,0 +1,51 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module TimeEntries
+ module AvailableWorkPackagesHelper
+ def available_work_packages_collection(allowed_scope)
+ service = WorkPackageCollectionFromQueryParamsService
+ .new(current_user, scope: allowed_scope)
+ .call(params)
+
+ if service.success?
+ service.result
+ else
+ api_errors = service.errors.full_messages.map do |message|
+ ::API::Errors::InvalidQuery.new(message)
+ end
+
+ raise ::API::Errors::MultipleErrors.create_if_many api_errors
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/time_entries/available_work_packages_on_create_api.rb b/lib/api/v3/time_entries/available_work_packages_on_create_api.rb
index e69de29bb2d..089b8e0141a 100644
--- a/lib/api/v3/time_entries/available_work_packages_on_create_api.rb
+++ b/lib/api/v3/time_entries/available_work_packages_on_create_api.rb
@@ -0,0 +1,54 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module TimeEntries
+ class AvailableWorkPackagesOnCreateAPI < ::API::OpenProjectAPI
+ after_validation do
+ authorize_any %i[log_time],
+ global: true
+ end
+
+ helpers AvailableWorkPackagesHelper
+
+ helpers do
+ def allowed_scope
+ WorkPackage.where(project_id: Project.allowed_to(User.current, :log_time))
+ end
+ end
+
+ resources :available_work_packages do
+ get do
+ available_work_packages_collection(allowed_scope)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/time_entries/available_work_packages_on_edit_api.rb b/lib/api/v3/time_entries/available_work_packages_on_edit_api.rb
index e69de29bb2d..8ed13aed651 100644
--- a/lib/api/v3/time_entries/available_work_packages_on_edit_api.rb
+++ b/lib/api/v3/time_entries/available_work_packages_on_edit_api.rb
@@ -0,0 +1,57 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module TimeEntries
+ class AvailableWorkPackagesOnEditAPI < ::API::OpenProjectAPI
+ after_validation do
+ authorize_any %i[edit_time_entries edit_own_time_entries],
+ projects: @time_entry.project
+ end
+
+ helpers AvailableWorkPackagesHelper
+
+ helpers do
+ def allowed_scope
+ edit_scope = WorkPackage.where(project_id: Project.allowed_to(User.current, :edit_time_entries))
+ edit_own_scope = WorkPackage.where(project_id: Project.allowed_to(User.current, :edit_own_time_entries))
+
+ edit_scope.or(edit_own_scope)
+ end
+ end
+
+ resources :available_work_packages do
+ get do
+ available_work_packages_collection(allowed_scope)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb b/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb
index dc04f0c2fe3..8ee45fc5295 100644
--- a/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb
+++ b/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb
@@ -87,13 +87,11 @@ module API
}
def allowed_work_package_href
- api_v3_paths.path_for(:work_packages, filters: allowed_work_packages_filters)
- end
-
- def allowed_work_packages_filters
- return unless represented.project
-
- [{ project: { operator: '=', values: [represented.project.id.to_s] } }]
+ if represented.new_record?
+ api_v3_paths.time_entries_available_work_packages_on_create
+ else
+ api_v3_paths.time_entries_available_work_packages_on_edit(represented.id)
+ end
end
def allowed_projects_href
diff --git a/lib/api/v3/time_entries/time_entries_api.rb b/lib/api/v3/time_entries/time_entries_api.rb
index 3bf8a935183..f8d1956ee68 100644
--- a/lib/api/v3/time_entries/time_entries_api.rb
+++ b/lib/api/v3/time_entries/time_entries_api.rb
@@ -39,6 +39,7 @@ module API
mount ::API::V3::TimeEntries::CreateFormAPI
mount ::API::V3::TimeEntries::Schemas::TimeEntrySchemaAPI
mount ::API::V3::TimeEntries::AvailableProjectsAPI
+ mount ::API::V3::TimeEntries::AvailableWorkPackagesOnCreateAPI
route_param :id, type: Integer, desc: 'Time entry ID' do
after_validation do
@@ -52,6 +53,7 @@ module API
delete &::API::V3::Utilities::Endpoints::Delete.new(model: TimeEntry).mount
mount ::API::V3::TimeEntries::UpdateFormAPI
+ mount ::API::V3::TimeEntries::AvailableWorkPackagesOnEditAPI
end
mount ::API::V3::TimeEntries::TimeEntriesActivityAPI
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 25a1c125fea..4c1b2f78e43 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -333,6 +333,14 @@ module API
"#{time_entries}/available_projects"
end
+ def self.time_entries_available_work_packages_on_create
+ "#{time_entries}/available_work_packages"
+ end
+
+ def self.time_entries_available_work_packages_on_edit(time_entry_id)
+ "#{time_entry(time_entry_id)}/available_work_packages"
+ end
+
index :type
show :type
diff --git a/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb b/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb
index fc77954870d..d6b04b694c4 100644
--- a/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb
+++ b/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb
@@ -44,6 +44,7 @@ describe ::API::V3::TimeEntries::Schemas::TimeEntrySchemaRepresenter do
let(:contract) do
contract = double('contract',
new_record?: new_record,
+ id: new_record ? nil : 5,
project: assigned_project)
allow(contract)
@@ -171,41 +172,22 @@ describe ::API::V3::TimeEntries::Schemas::TimeEntrySchemaRepresenter do
context 'if embedding' do
let(:embedded) { true }
- context 'if having no project' do
- it_behaves_like 'links to allowed values via collection link' do
- let(:href) do
- api_v3_paths.work_packages
- end
- end
- end
-
- context 'if having a project' do
- let(:assigned_project) { project }
+ context 'if being a new record' do
+ let(:new_record) { true }
it_behaves_like 'links to allowed values via collection link' do
let(:href) do
- api_v3_paths.path_for(:work_packages, filters: [{ project: { operator: '=', values: [project.id.to_s] } }])
+ api_v3_paths.time_entries_available_work_packages_on_create
end
end
end
- end
-
- describe 'project' do
- let(:path) { 'project' }
-
- it_behaves_like 'has basic schema properties' do
- let(:type) { 'Project' }
- let(:name) { TimeEntry.human_attribute_name('project') }
- let(:required) { false }
- let(:writable) { true }
- end
- context 'if embedding' do
- let(:embedded) { true }
+ context 'if being an existing record' do
+ let(:new_record) { false }
it_behaves_like 'links to allowed values via collection link' do
let(:href) do
- api_v3_paths.time_entries_available_projects
+ api_v3_paths.time_entries_available_work_packages_on_edit(contract.id)
end
end
end
diff --git a/spec/requests/api/v3/time_entries/create_form_resource_spec.rb b/spec/requests/api/v3/time_entries/create_form_resource_spec.rb
index 64261790617..832115d6153 100644
--- a/spec/requests/api/v3/time_entries/create_form_resource_spec.rb
+++ b/spec/requests/api/v3/time_entries/create_form_resource_spec.rb
@@ -176,7 +176,7 @@ describe ::API::V3::TimeEntries::CreateFormAPI, content_type: :json do
it 'has the available values listed in the schema' do
body = subject.body
- wp_path = api_v3_paths.path_for(:work_packages, filters: [{ project: { operator: '=', values: [project.id.to_s] } }])
+ wp_path = api_v3_paths.time_entries_available_work_packages_on_create
expect(body)
.to be_json_eql(wp_path.to_json)
diff --git a/spec/requests/api/v3/time_entries/update_form_resource_spec.rb b/spec/requests/api/v3/time_entries/update_form_resource_spec.rb
index dbaabe3a800..8797e8a4eac 100644
--- a/spec/requests/api/v3/time_entries/update_form_resource_spec.rb
+++ b/spec/requests/api/v3/time_entries/update_form_resource_spec.rb
@@ -149,7 +149,7 @@ describe ::API::V3::TimeEntries::UpdateFormAPI, content_type: :json do
it 'has the available values listed in the schema' do
body = subject.body
- wp_path = api_v3_paths.path_for(:work_packages, filters: [{ project: { operator: '=', values: [project.id.to_s] } }])
+ wp_path = api_v3_paths.time_entries_available_work_packages_on_edit(time_entry.id)
expect(body)
.to be_json_eql(wp_path.to_json)
diff --git a/spec/services/api/v3/work_package_collection_from_query_params_service_spec.rb b/spec/services/api/v3/work_package_collection_from_query_params_service_spec.rb
index e578c2a8e2b..667987515bf 100644
--- a/spec/services/api/v3/work_package_collection_from_query_params_service_spec.rb
+++ b/spec/services/api/v3/work_package_collection_from_query_params_service_spec.rb
@@ -65,7 +65,7 @@ describe ::API::V3::WorkPackageCollectionFromQueryParamsService,
allow(::API::V3::WorkPackageCollectionFromQueryService)
.to receive(:new)
- .with(query, user)
+ .with(query, user, scope: nil)
.and_return(mock_wp_collection_from_query_service)
end