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

fix validations so that timer also works

Klaus Zandersdd24be463b39
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index 977a46bcd7f..f5e51ac54ef 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -45,11 +45,19 @@ class TimeEntry < ApplicationRecord
acts_as_journalized
- validates_presence_of :user_id, :project_id, :spent_on
- validates_presence_of :hours, if: -> { !ongoing? }
- validates_numericality_of :hours, allow_nil: true, message: :invalid
+ validates :user_id, :project_id, :spent_on,
+ presence: true
+ validates :hours,
+ presence: true,
+ if: -> { !ongoing? }
- validates :start_time, :hours,
+ validates :hours,
+ numericality: {
+ message: :invalid
+ },
+ allow_nil: true
+
+ validates :start_time,
presence: true,
if: -> { TimeEntry.must_track_start_and_end_time? }
diff --git a/modules/costs/app/services/time_entries/set_attributes_service.rb b/modules/costs/app/services/time_entries/set_attributes_service.rb
index f15f6c0c59b..acc65669139 100644
--- a/modules/costs/app/services/time_entries/set_attributes_service.rb
+++ b/modules/costs/app/services/time_entries/set_attributes_service.rb
@@ -48,6 +48,9 @@ module TimeEntries
model.time_zone = model.user.time_zone.name
end
+ # Set start time for ongoing time entries
+ ensure_start_time_for_onging_entries
+
# Always set the logging user as logged_by
set_logged_by
end
@@ -100,5 +103,15 @@ module TimeEntries
!model.project ||
(model.work_package && model.work_package_id_changed? && !model.project_id_changed?)
end
+
+ def ensure_start_time_for_onging_entries
+ return unless model.new_record?
+ return unless model.ongoing?
+ return unless TimeEntry.can_track_start_and_end_time?
+
+ Time.use_zone(model.user.time_zone) do
+ model.start_time ||= Time.zone.now.strftime("%H:%M")
+ end
+ end
end
end
diff --git a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
index f3a84208462..d6b28c6de02 100644
--- a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
+++ b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
@@ -159,6 +159,49 @@ RSpec.describe TimeEntries::SetAttributesService, type: :model do
end
end
+ context "with an ongoing time entry" do
+ let(:params) do
+ {
+ spent_on: Time.zone.today,
+ ongoing: true,
+ user_id: user.id,
+ work_package_id: work_package.id,
+ activity_id: activity.id
+ }
+ end
+
+ before do
+ user.pref[:time_zone] = "America/Los_Angeles"
+ end
+
+ context "when start_time is allowed" do
+ before do
+ allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(true)
+ end
+
+ it "sets the start_time to the users current time" do
+ Timecop.freeze do
+ subject
+
+ current_time = ActiveSupport::TimeZone[user.time_zone].now
+
+ expect(time_entry_instance.start_time).to eq((current_time.hour * 60) + current_time.min)
+ end
+ end
+ end
+
+ context "when start_time is not allowed" do
+ before do
+ allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(false)
+ end
+
+ it "does not set the start_time" do
+ subject
+ expect(time_entry_instance.start_time).to be_nil
+ end
+ end
+ end
+
context "with hours == 0" do
let(:params) do
{