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

recover from dupliate ongoing te in service

ulferts834184938f41
diff --git a/modules/costs/app/services/time_entries/create_service.rb b/modules/costs/app/services/time_entries/create_service.rb
index 4dae70bf0bd..3f51cb91f58 100644
--- a/modules/costs/app/services/time_entries/create_service.rb
+++ b/modules/costs/app/services/time_entries/create_service.rb
@@ -27,6 +27,16 @@
#++
class TimeEntries::CreateService < BaseServices::Create
+ protected
+
+ def persist(service_result)
+ super
+ rescue ActiveRecord::RecordNotUnique
+ fail_service_on_duplicate_ongoing(service_result)
+
+ service_result
+ end
+
def after_perform(call)
OpenProject::Notifications.send(
OpenProject::Events::TIME_ENTRY_CREATED,
@@ -35,4 +45,11 @@ class TimeEntries::CreateService < BaseServices::Create
call
end
+
+ def fail_service_on_duplicate_ongoing(service_result)
+ return unless service_result.result.ongoing
+
+ service_result.errors.add :base, :duplicate_ongoing
+ service_result.success = false
+ end
end
diff --git a/modules/costs/spec/requests/api/time_entry_resource_spec.rb b/modules/costs/spec/requests/api/time_entry_resource_spec.rb
index 907de868eb5..f8adf998ee8 100644
--- a/modules/costs/spec/requests/api/time_entry_resource_spec.rb
+++ b/modules/costs/spec/requests/api/time_entry_resource_spec.rb
@@ -565,6 +565,27 @@ RSpec.describe "API v3 time_entry resource" do
.at_path("message")
end
end
+
+ context "when starting an ongoing time entry with an ongoing timer already running" do
+ let(:additional_setup) { -> { existing_ongoing_time_entry } }
+
+ let(:existing_ongoing_time_entry) do
+ create(:time_entry, ongoing: true, project:, work_package:, user: current_user)
+ end
+
+ let(:params) do
+ super().merge({ ongoing: true })
+ end
+
+ it "returns 422 and remarks that another time entry is ongoing" do
+ expect(subject.status)
+ .to be(422)
+
+ expect(subject.body)
+ .to be_json_eql("An ongoing time entry already exists for this user.".to_json)
+ .at_path("message")
+ end
+ end
end
describe "PATCH api/v3/time_entries/:id" do