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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
fix setting times in the correct timezone for users
Klaus Zanders86c6c975ddcf
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.rbindex 6e85f0a0eb7..07c4ab92a4c 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@@ -88,16 +88,6 @@ module APIdatetime_formatter.format_duration_from_hours(represented.hours) if represented.hoursend- date_time_property :start_time,- exec_context: :decorator,- getter: ->(*) { datetime_formatter.format_datetime(represented.start_timestamp, allow_nil: true) },- if: ->(*) { TimeEntry.can_track_start_and_end_time? }-- date_time_property :end_time,- exec_context: :decorator,- getter: ->(*) { datetime_formatter.format_datetime(represented.end_timestamp, allow_nil: true) },- if: ->(*) { TimeEntry.can_track_start_and_end_time? }-date_time_property :created_atdate_time_property :updated_at@@ -108,6 +98,20 @@ module APIassociated_resource :user+ date_time_property :start_time,+ exec_context: :decorator,+ getter: ->(*) {+ datetime_formatter.format_datetime(represented.start_timestamp, allow_nil: true)+ },+ if: ->(*) { TimeEntry.can_track_start_and_end_time? }++ date_time_property :end_time,+ exec_context: :decorator,+ getter: ->(*) {+ datetime_formatter.format_datetime(represented.end_timestamp, allow_nil: true)+ },+ if: ->(*) { TimeEntry.can_track_start_and_end_time? }+associated_resource :activity,representer: TimeEntriesActivityRepresenter,v3_path: :time_entries_activity,@@ -139,24 +143,32 @@ module APIallow_nil: true)end- def start_time=(value) # rubocop:disable Metrics/AbcSize+ def start_time=(value) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexityts = datetime_formatter.parse_datetime(value, "start_time", allow_nil: true)if ts.nil?- represented.start_timestamp = nil+ represented.start_time = nilreturnend- if represented.user.present?- ts = ts.in_time_zone(represented.user.time_zone)- end-- if ts.to_date == represented.spent_on- represented.start_time = ts.strftime("%H:%M")+ tz_specific_time = if represented.user_id.present? && represented.user_id != current_user.id+ user = User.visible.find_by(id: represented.user_id)+ if user+ ts.in_time_zone(user.time_zone)+ else+ ts.in_time_zone(current_user.time_zone)+ end+ else+ ts.in_time_zone(current_user.time_zone)+ end++ if tz_specific_time.to_date == represented.spent_on+ represented.start_time = tz_specific_time.strftime("%H:%M")elseraise 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))+ spent_on: represented.spent_on,+ start_time: tz_specific_time.to_date))endenddiff --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.rbindex 72b5c0bc57b..feabb300391 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@@ -185,7 +185,7 @@ RSpec.describe API::V3::TimeEntries::TimeEntryRepresenter, "parsing" do# We are sending in 12:30:00 UTC as the start time, in Tokyo time (for 2017-07-28) that equals# 21:30:00 in Japan Standard Time (JST), so the time should be set to 21:30- expect(time_entry.start_time).to eq((21 * 60) + 30) # 12:30+ expect(time_entry.start_time).to eq((21 * 60) + 30) # 21:30expect(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"))diff --git a/modules/costs/spec/requests/api/time_entries/create_resource_spec.rb b/modules/costs/spec/requests/api/time_entries/create_resource_spec.rbnew file mode 100644index 00000000000..8d035ab6bb0--- /dev/null+++ b/modules/costs/spec/requests/api/time_entries/create_resource_spec.rb@@ -0,0 +1,124 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++require "spec_helper"+require "rack/test"++RSpec.describe "API v3 Time Entries resource",+ content_type: :json,+ with_settings: { allow_tracking_start_and_end_times: true } do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ let(:project) { create(:project) }+ let(:active_activity) { create(:time_entry_activity) }+ let(:work_package) { create(:work_package, project:) }+ let(:permissions) { %i[log_time view_work_packages] }++ let(:german_user) do+ create(:user, member_with_permissions: { project => permissions }, preferences: { time_zone: "Europe/Berlin" })+ end+ let(:japanese_user) do+ create(:user, member_with_permissions: { project => [:view_project] }, preferences: { time_zone: "Asia/Tokyo" })+ end++ let(:path) { api_v3_paths.time_entries }+ let(:parameters) { {} }+ let(:json_response) { JSON.parse(response.body) }++ subject(:response) { last_response }++ describe "#POST /api/v3/time_entries" do+ before do+ login_as(german_user)+ end++ describe "correct time zone handling for start time" do+ context "when logging time for yourself" 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: "2024-12-24",+ hours: "PT2H",+ startTime: "2024-12-24T12:00:00Z"+ }+ end++ it "creates the time entry and sets the start time to the users timezone" do+ post path, parameters.to_json++ expect(subject).to have_http_status(:created)+ time_entry_id = json_response["id"]+ time_entry = TimeEntry.find(time_entry_id)++ expect(time_entry.spent_on).to eq(Date.new(2024, 12, 24))+ expect(time_entry.hours).to eq(2)+ expect(time_entry.start_time).to eq(13 * 60) # 12:00 UTC = 13:00 Berlin on December 24th, 2024+ expect(time_entry.time_zone).to eq("Europe/Berlin")+ expect(time_entry.user).to eq(german_user)+ end+ end++ context "when logging time for another user" do+ let(:parameters) do+ {+ _links: {+ user: { href: api_v3_paths.user(japanese_user.id) },+ 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: "2024-12-24",+ hours: "PT2H",+ startTime: "2024-12-24T12:00:00Z"+ }+ end++ it "creates the time entry and sets the start time to the users timezone" do+ post path, parameters.to_json++ expect(subject).to have_http_status(:created)+ time_entry_id = json_response["id"]+ time_entry = TimeEntry.find(time_entry_id)++ expect(time_entry.spent_on).to eq(Date.new(2024, 12, 24))+ expect(time_entry.hours).to eq(2)+ expect(time_entry.start_time).to eq(21 * 60) # 12:00 UTC = 21:00 Tokyo on Dec 24th, 2024+ expect(time_entry.time_zone).to eq("Asia/Tokyo")+ expect(time_entry.user).to eq(japanese_user)+ end+ end+ end+ end+end