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 time entries update form

ulferts620a88b6631e
diff --git a/app/contracts/time_entries/update_contract.rb b/app/contracts/time_entries/update_contract.rb
index be56bd57644..11ddb26b2be 100644
--- a/app/contracts/time_entries/update_contract.rb
+++ b/app/contracts/time_entries/update_contract.rb
@@ -40,8 +40,6 @@ module TimeEntries
super
end
- private
-
##
# Users may update time entries IF
# they have the :edit_time_entries or
@@ -52,6 +50,8 @@ module TimeEntries
end && user_allowed_to_update_in?(model.project)
end
+ private
+
def user_allowed_to_update_in?(project)
user.allowed_to?(:edit_time_entries, project) ||
(model.user == user && user.allowed_to?(:edit_own_time_entries, project))
diff --git a/docs/api/apiv3/endpoints/time_entries.apib b/docs/api/apiv3/endpoints/time_entries.apib
index af8c0d94b2f..c491571d451 100644
--- a/docs/api/apiv3/endpoints/time_entries.apib
+++ b/docs/api/apiv3/endpoints/time_entries.apib
@@ -111,7 +111,7 @@ Depending on custom fields defined for time entries, additional properties might
Returned if the time entry does not exist or if the user does not have permission to view them.
- **Required permission** `view time entries` in the project the time entry is assigned to
+ **Required permission** `view time entries` in the project the time entry is assigned to or `view own time entries` for time entries belonging to the user
+ Body
@@ -149,7 +149,9 @@ Permanently deletes the specified time entry.
+ Response 404 (application/hal+json)
- Returned if the time entry does not exist.
+ Returned if the time entry does not exist or if the user does not have sufficient permissions to see the time entry.
+
+ **Required permission** `view time entries` in the project the time entry is assigned to or `view own time entries` for time entries belonging to the user
+ Body
@@ -674,7 +676,7 @@ For more details and all possible responses see the general specification of [Fo
},
"workPackage": {
"href": "/api/v3/work_packages/5",
- "title": "non sed quis id vel quia culpa ipsam § hkjhkhj"
+ "title": "non sed quis id vel quia culpa ipsam"
}
},
"hours": "PT5H",
@@ -898,6 +900,287 @@ For more details and all possible responses see the general specification of [Fo
"message": "You are not authorized to access this resource."
}
+## Time entry update form [/api/v3/time_entries/:id/form]
+
+This endpoint returns a form allowing a guided update of existing time entries.
+
+For more details and all possible responses see the general specification of [Forms](/api/forms).
+
+## Time entry update form [POST]
+
++ Request Create time entry form
+
+ + Body
+
+ {
+ "_links": {
+ "workPackage": {
+ "href": "/api/v3/work_packages/9"
+ }
+ },
+ "hours": "PT8H",
+ "customField1": {
+ "raw": "some altered text custom field value"
+ }
+
++ Response 200 (application/hal+json)
+
+ + Body
+
+ {
+ "_type": "Form",
+ "_embedded": {
+ "payload": {
+ {
+ "_links": {
+ "project": {
+ "href": "/api/v3/projects/1",
+ "title": "Some project"
+ },
+ "activity": {
+ "href": "/api/v3/time_entries/activities/18",
+ "title": "Management"
+ },
+ "workPackage": {
+ "href": "/api/v3/work_packages/9",
+ "title": "The updated work package"
+ }
+ },
+ "hours": "PT8H",
+ "comment": {
+ "format": "plain",
+ "raw": "Some comment",
+ "html": "<p>Some comment</p>"
+ },
+ "spentOn": "2017-07-28",
+ "customField1": {
+ "format": "markdown",
+ "raw": "some altered text custom field value"
+ "html": "<p>some altered text custom field value</p>"
+ },
+ "customField8": 5
+ }
+ },
+ "schema": {
+ "_type": "Schema",
+ "_dependencies": [],
+ "id": {
+ "type": "Integer",
+ "name": "ID",
+ "required": true,
+ "hasDefault": false,
+ "writable": false,
+ "options": {}
+ },
+ "createdAt": {
+ "type": "DateTime",
+ "name": "Created on",
+ "required": true,
+ "hasDefault": false,
+ "writable": false,
+ "options": {}
+ },
+ "updatedAt": {
+ "type": "DateTime",
+ "name": "Updated on",
+ "required": true,
+ "hasDefault": false,
+ "writable": false,
+ "options": {}
+ },
+ "spentOn": {
+ "type": "Date",
+ "name": "Date",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "options": {}
+ },
+ "hours": {
+ "type": "Duration",
+ "name": "Hours",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "options": {}
+ },
+ "user": {
+ "type": "User",
+ "name": "User",
+ "required": true,
+ "hasDefault": false,
+ "writable": false,
+ "options": {}
+ },
+ "workPackage": {
+ "type": "WorkPackage",
+ "name": "Work package",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "_links": {
+ "allowedValues": {
+ "href": "/api/v3/work_packages?filters=%5B%7B%22project%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%221%22%5D%7D%7D%5D"
+ }
+ }
+ },
+ "project": {
+ "type": "Project",
+ "name": "Project",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "_links": {
+ "allowedValues": {
+ "href": "/api/v3/time_entries/available_projects"
+ }
+ }
+ },
+ "activity": {
+ "type": "TimeEntriesActivity",
+ "name": "Activity",
+ "required": true,
+ "hasDefault": true,
+ "writable": true,
+ "_embedded": {
+ "allowedValues": [
+ {
+ "_type": "TimeEntriesActivity",
+ "id": 18,
+ "name": "Management",
+ "position": 1,
+ "default": true,
+ "_links": {
+ "self": {
+ "href": "/api/v3/time_entries/activities/18",
+ "title": "Management"
+ },
+ "projects": [
+ {
+ "href": "/api/v3/projects/some-project",
+ "title": "Some project"
+ },
+ {
+ "href": "/api/v3/projects/some-other-project",
+ "title": "Some other project"
+ }
+ ]
+ }
+ },
+ {
+ "_type": "TimeEntriesActivity",
+ "id": 23,
+ "name": "Implementation",
+ "position": 1,
+ "default": true,
+ "_links": {
+ "self": {
+ "href": "/api/v3/time_entries/activities/23",
+ "title": "Implementation"
+ },
+ "projects": [
+ {
+ "href": "/api/v3/projects/some-project",
+ "title": "Some project"
+ }
+ ]
+ }
+ }
+ ]
+ },
+ "_links": {
+ "allowedValues": [
+ {
+ "href": "/api/v3/time_entries/activities/18",
+ "title": "Management"
+ },
+ {
+ "href": "/api/v3/time_entries/activities/23",
+ "title": "Implementation"
+ }
+ ]
+ }
+ },
+ "customField1": {
+ "type": "Formattable",
+ "name": "Some formattable",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "options": {
+ "rtl": null
+ }
+ },
+ "customField8": {
+ "type": "Integer",
+ "name": "Some integer",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "options": {
+ "rtl": null
+ }
+ },
+ "_links": {}
+ },
+ "validationErrors": {}
+ },
+ "_links": {
+ "self": {
+ "href": "/api/v3/time_entries/42/form",
+ "method": "post"
+ },
+ "validate": {
+ "href": "/api/v3/time_entries/42/form",
+ "method": "post"
+ },
+ "commit": {
+ "href": "/api/v3/time_entries/42",
+ "method": "post"
+ }
+ }
+ }
+
++ Response 400 (application/hal+json)
+
+ Occurs when the client did not send a valid JSON object in the request body.
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:InvalidRequestBody",
+ "message": "The request body was not a single JSON object."
+ }
+
++ Response 403 (application/hal+json)
+
+ Returned if the client does not have sufficient permissions to edit the time entry.
+
+ **Required permission:** *edit time entries* for every time entry of a project, or *edit own time entries* for time entries belonging to the user.
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",
+ "message": "You are not authorized to access this resource."
+ }
+
++ Response 404 (application/hal+json)
+
+ Returned if the time entry does not exist or if the client does not have sufficient permissions to view it.
+
+ **Required permission** `view time entries` in the project the time entry is assigned to or `view own time entries` for time entries belonging to the user
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:NotFound",
+ "message": "The requested resource could not be found."
+ }
+
## Available projects for time entries [/api/v3/time_entries/available_projects]
+ Model
diff --git a/lib/api/decorators/update_form.rb b/lib/api/decorators/update_form.rb
index 94640c18793..9540241f578 100644
--- a/lib/api/decorators/update_form.rb
+++ b/lib/api/decorators/update_form.rb
@@ -50,7 +50,7 @@ module API
private
def downcase_model_name
- model_name.downcase
+ model_name.underscore
end
end
end
diff --git a/lib/api/v3/time_entries/time_entries_api.rb b/lib/api/v3/time_entries/time_entries_api.rb
index da881c4c1ba..1699d5d9709 100644
--- a/lib/api/v3/time_entries/time_entries_api.rb
+++ b/lib/api/v3/time_entries/time_entries_api.rb
@@ -48,9 +48,10 @@ module API
end
get &::API::V3::Utilities::Endpoints::Show.new(model: TimeEntry).mount
-
patch &::API::V3::Utilities::Endpoints::Update.new(model: TimeEntry).mount
delete &::API::V3::Utilities::Endpoints::Delete.new(model: TimeEntry).mount
+
+ mount ::API::V3::TimeEntries::UpdateFormAPI
end
mount ::API::V3::TimeEntries::TimeEntriesActivityAPI
diff --git a/lib/api/v3/time_entries/update_form_api.rb b/lib/api/v3/time_entries/update_form_api.rb
new file mode 100644
index 00000000000..5fc1f02bb49
--- /dev/null
+++ b/lib/api/v3/time_entries/update_form_api.rb
@@ -0,0 +1,49 @@
+#-- 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 UpdateFormAPI < ::API::OpenProjectAPI
+ resource :form do
+ after_validation do
+ authorize_by_with_raise(-> {
+ ::TimeEntries::UpdateContract
+ .new(@time_entry, current_user)
+ .user_allowed_to_update?
+ })
+ end
+
+ post &::API::V3::Utilities::Endpoints::UpdateForm
+ .new(model: TimeEntry)
+ .mount
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/time_entries/update_form_representer.rb b/lib/api/v3/time_entries/update_form_representer.rb
new file mode 100644
index 00000000000..88e9d069683
--- /dev/null
+++ b/lib/api/v3/time_entries/update_form_representer.rb
@@ -0,0 +1,39 @@
+#-- encoding: UTF-8
+
+#-- 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 UpdateFormRepresenter < FormRepresenter
+ include API::Decorators::UpdateForm
+ end
+ end
+ end
+end
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
new file mode 100644
index 00000000000..d3ed1887aea
--- /dev/null
+++ b/spec/requests/api/v3/time_entries/update_form_resource_spec.rb
@@ -0,0 +1,230 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2015 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-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 doc/COPYRIGHT.rdoc for more details.
+
+require 'spec_helper'
+require 'rack/test'
+
+describe ::API::V3::TimeEntries::UpdateFormAPI, content_type: :json do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ let(:project) { time_entry.project }
+ let(:time_entry) { FactoryBot.create(:time_entry) }
+ let(:active_activity) { FactoryBot.create(:time_entry_activity) }
+ let(:in_project_inactive_activity) do
+ FactoryBot.create(:time_entry_activity).tap do |tea|
+ TimeEntryActivitiesProject.insert(project_id: project.id, activity_id: tea.id, active: false)
+ end
+ end
+ let(:custom_field) { FactoryBot.create(:time_entry_custom_field) }
+ let(:user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:work_package) do
+ FactoryBot.create(:work_package, project: project)
+ end
+ let(:other_user) { FactoryBot.create(:user) }
+ let(:permissions) { %i[view_time_entries edit_time_entries view_work_packages] }
+
+ let(:path) { api_v3_paths.time_entry_form(time_entry.id) }
+ let(:parameters) { {} }
+
+ before do
+ login_as(user)
+
+ post path, parameters.to_json
+ end
+
+ subject(:response) { last_response }
+
+ describe '#POST /api/v3/time_entries/:id/form' do
+ it 'returns 200 OK' do
+ expect(response.status).to eq(200)
+ end
+
+ it 'returns a form' do
+ expect(response.body)
+ .to be_json_eql('Form'.to_json)
+ .at_path('_type')
+ end
+
+ context 'with all parameters' do
+ let(:parameters) do
+ {
+ _links: {
+ workPackage: {
+ href: api_v3_paths.work_package(work_package.id)
+ },
+ project: {
+ href: api_v3_paths.project(project.id)
+ },
+ activity: {
+ href: api_v3_paths.time_entries_activity(active_activity.id)
+ }
+ },
+ spentOn: Date.today.to_s,
+ hours: 'PT5H',
+ "comment": {
+ raw: "some comment"
+ },
+ "customField#{custom_field.id}": {
+ raw: 'some cf text'
+ }
+ }
+ end
+
+ it 'has 0 validation errors' do
+ expect(subject.body).to have_json_size(0).at_path('_embedded/validationErrors')
+ end
+
+ it 'does not alter the entry' do
+ expect(time_entry.work_package)
+ .not_to eql work_package
+ end
+
+ it 'has the values prefilled in the payload' do
+ body = subject.body
+
+ expect(body)
+ .to be_json_eql(api_v3_paths.project(project.id).to_json)
+ .at_path('_embedded/payload/_links/project/href')
+
+ expect(body)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id).to_json)
+ .at_path('_embedded/payload/_links/workPackage/href')
+
+ expect(body)
+ .to be_json_eql(api_v3_paths.time_entries_activity(active_activity.id).to_json)
+ .at_path('_embedded/payload/_links/activity/href')
+
+ expect(body)
+ .to be_json_eql("some comment".to_json)
+ .at_path("_embedded/payload/comment/raw")
+
+ expect(body)
+ .to be_json_eql(Date.today.to_s.to_json)
+ .at_path('_embedded/payload/spentOn')
+
+ expect(body)
+ .to be_json_eql("PT5H".to_json)
+ .at_path('_embedded/payload/hours')
+
+ expect(body)
+ .to be_json_eql("some cf text".to_json)
+ .at_path("_embedded/payload/customField#{custom_field.id}/raw")
+
+ # As the user is always the current user, it is not part of the payload
+ expect(body)
+ .not_to have_json_path('_embedded/payload/_links/user')
+ end
+
+ 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] } }])
+
+ expect(body)
+ .to be_json_eql(wp_path.to_json)
+ .at_path('_embedded/schema/workPackage/_links/allowedValues/href')
+
+ expect(body)
+ .to be_json_eql(api_v3_paths.time_entries_available_projects.to_json)
+ .at_path('_embedded/schema/project/_links/allowedValues/href')
+ end
+
+ it 'has a commit link' do
+ expect(subject.body)
+ .to be_json_eql(api_v3_paths.time_entry(time_entry.id).to_json)
+ .at_path('_links/commit/href')
+ end
+ end
+
+ context 'with an invalid parameter' do
+ let(:parameters) do
+ {
+ _links: {
+ workPackage: {
+ href: api_v3_paths.work_package(0)
+ }
+ }
+ }
+ end
+
+ it 'has 1 validation errors' do
+ expect(subject.body).to have_json_size(1).at_path('_embedded/validationErrors')
+ end
+
+ it 'has a validation error on workPackage' do
+ expect(subject.body).to have_json_path('_embedded/validationErrors/workPackage')
+ end
+
+ it 'has no commit link' do
+ expect(subject.body)
+ .not_to have_json_path('_links/commit/href')
+ end
+ end
+
+ context 'with permissions to edit own time entries' do
+ let(:permissions) { %i[view_time_entries edit_own_time_entries] }
+
+ context 'with the time_entry being of the user' do
+ let(:user) do
+ user = time_entry.user
+
+ FactoryBot.create(:member,
+ project: time_entry.project,
+ roles: [FactoryBot.create(:role, permissions: permissions)],
+ principal: user)
+
+ user
+ end
+
+ it 'returns 200 OK' do
+ expect(response.status).to eq(200)
+ end
+ end
+
+ context 'with the time_entry being of a different user' do
+ it 'returns 403 Not Authorized' do
+ expect(response.status).to eq(403)
+ end
+ end
+ end
+
+ context 'without the necessary permission' do
+ let(:permissions) { %i[view_time_entries] }
+
+ it 'returns 403 Not Authorized' do
+ expect(response.status).to eq(403)
+ end
+ end
+ end
+end