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

enable setter for start_time in the API

Klaus Zandersabed5435c89b
diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.yml
index cf4b1dc3dc4..bbbc33a3327 100644
--- a/modules/costs/config/locales/en.yml
+++ b/modules/costs/config/locales/en.yml
@@ -186,3 +186,8 @@ en:
js:
text_are_you_sure: "Are you sure?"
+
+ api_v3:
+ errors:
+ validation:
+ start_time_different_date: "Date part of startTime (%{start_time}) must be the same as the spentOn (%{spent_on}) date."
diff --git a/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb b/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
index 643648f251b..0b220ad1ac5 100644
--- a/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
+++ b/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
@@ -90,12 +90,12 @@ module API
date_time_property :start_time,
exec_context: :decorator,
- getter: ->(*) { represented.start_timestamp },
+ getter: ->(*) { datetime_formatter.format_datetime(represented.start_timestamp) },
if: ->(*) { TimeEntry.can_track_start_and_end_time? }
date_time_property :end_time,
exec_context: :decorator,
- getter: ->(*) { represented.end_timestamp },
+ getter: ->(*) { datetime_formatter.format_datetime(represented.end_timestamp) },
if: ->(*) { TimeEntry.can_track_start_and_end_time? }
date_time_property :created_at
@@ -139,6 +139,20 @@ module API
allow_nil: true)
end
+ def start_time=(value) # rubocop:disable Metrics/AbcSize
+ ts = datetime_formatter.parse_datetime(value, "start_time", allow_nil: true)
+
+ if ts.nil?
+ represented.start_timestamp = nil
+ elsif ts.to_date != represented.spent_on
+ raise API::Errors::Validation.new("start_time",
+ I18n.t("api_v3.errors.validation.start_time_different_date",
+ spent_on: represented.spent_on, start_time: ts.to_date))
+ else
+ represented.start_time = ts.strftime("%H:%M")
+ end
+ end
+
self.to_eager_load = [:work_package,
:user,
:activity,
diff --git a/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb b/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb
index 0c7afd3b3de..884ac9a0531 100644
--- a/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb
+++ b/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb
@@ -81,6 +81,7 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do
"raw" => "some comment"
},
"spentOn" => "2017-07-28",
+ "startTime" => "2017-07-28T12:30:00Z",
text_custom_field.attribute_name(:camel_case) => {
"raw" => "some text"
}
@@ -129,7 +130,7 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do
end
describe "properties" do
- context "spentOn" do
+ describe "spentOn" do
it "updates spent_on" do
time_entry = representer.from_hash(hash)
expect(time_entry.spent_on)
@@ -137,7 +138,56 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do
end
end
- context "hours" do
+ describe "startTime" do
+ context "when not tracking start and end time" do
+ before do
+ allow(TimeEntry).to receive_messages(
+ can_track_start_and_end_time?: false,
+ must_track_start_and_end_time?: false
+ )
+ end
+
+ it "does not set start_time" do
+ time_entry = representer.from_hash(hash)
+ expect(time_entry.start_time).to be_nil
+ end
+ end
+
+ context "when tracking start and end time" do
+ before do
+ allow(TimeEntry).to receive_messages(
+ can_track_start_and_end_time?: true,
+ must_track_start_and_end_time?: false
+ )
+ end
+
+ context "when spent_on != start_time date" do
+ before do
+ hash["startTime"] = "1980-12-22T12:00:00Z"
+ end
+
+ it "raises an error" do
+ expect do
+ representer.from_hash(hash)
+ end.to raise_error(API::Errors::Validation)
+ end
+ end
+
+ it "sets start_time" do
+ time_entry = representer.from_hash(hash)
+
+ # timezone will be set by the UpdateAttributeService, so we do it manually here in the test
+ time_entry.time_zone = "Etc/UTC"
+
+ expect(time_entry.start_time).to eq((12 * 60) + 30) # 12:30
+
+ expect(time_entry.start_timestamp).to eq(DateTime.parse("2017-07-28T12:30:00Z"))
+ expect(time_entry.end_timestamp).to eq(DateTime.parse("2017-07-28T17:30:00Z"))
+ end
+ end
+ end
+
+ describe "hours" do
it "updates hours" do
time_entry = representer.from_hash(hash)
expect(time_entry.hours)
@@ -159,7 +209,7 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do
end
end
- context "comment" do
+ describe "comment" do
it "updates comment" do
time_entry = representer.from_hash(hash)
expect(time_entry.comments)
@@ -167,7 +217,7 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do
end
end
- context "property custom field" do
+ describe "property custom field" do
it "updates the custom value" do
time_entry = representer.from_hash(hash)