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

Avoid assigning inactive time entry activity

Oliver Günther14f872160d66
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 920943a831d..b2930ec9d30 100644
--- a/modules/costs/app/services/time_entries/set_attributes_service.rb
+++ b/modules/costs/app/services/time_entries/set_attributes_service.rb
@@ -31,7 +31,7 @@ module TimeEntries
private
def set_attributes(attributes)
- super
+ model.attributes = params
##
# Update project context if moving time entry
@@ -39,14 +39,16 @@ module TimeEntries
model.project = model.work_package&.project
end
+ set_default_attributes(params) if model.new_record?
+
# Always set the logging user as logged_by
set_logged_by
end
def set_default_attributes(*)
set_default_user
- set_default_activity
set_default_hours
+ set_default_activity if model.activity.nil?
end
def set_logged_by
@@ -62,7 +64,25 @@ module TimeEntries
end
def set_default_activity
- model.activity ||= TimeEntryActivity.default
+ return unless TimeEntryActivity.default
+
+ if model.project
+ assign_default_project_activity
+ else
+ assign_default_activity
+ end
+ end
+
+ def assign_default_project_activity
+ if TimeEntryActivity.active_in_project(model.project).exists?(id: TimeEntryActivity.default.id)
+ assign_default_activity
+ end
+ end
+
+ def assign_default_activity
+ model.change_by_system do
+ model.activity = TimeEntryActivity.default
+ end
end
def set_default_hours
diff --git a/modules/costs/spec/services/time_entries/set_attributes_service_integration_spec.rb b/modules/costs/spec/services/time_entries/set_attributes_service_integration_spec.rb
new file mode 100644
index 00000000000..e1a825fbc34
--- /dev/null
+++ b/modules/costs/spec/services/time_entries/set_attributes_service_integration_spec.rb
@@ -0,0 +1,67 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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'
+
+RSpec.describe TimeEntries::SetAttributesService, 'integration', type: :model do
+ let(:project) { create(:project) }
+ let(:user) do
+ create(:user,
+ member_with_permissions: {
+ project => %i[view_work_packages log_time]
+ })
+ end
+ let(:work_package) { create(:work_package, project:) }
+ let(:time_entry_instance) { TimeEntry.new(params) }
+ let(:instance) do
+ described_class.new(user:, model: time_entry_instance, contract_class: TimeEntries::CreateContract)
+ end
+
+ let(:params) do
+ {
+ work_package:,
+ spent_on: Time.zone.today,
+ hours: 1
+ }
+ end
+
+ subject { instance.call(params) }
+
+ context 'default activity not active in project' do
+ let!(:default_activity) { create(:time_entry_activity, is_default: true) }
+
+ before do
+ project.time_entry_activities_projects.create(activity_id: default_activity.id, active: false)
+ end
+
+ it 'does not assign the default activity' do
+ expect(subject).to be_success
+ expect(subject.result.activity).to be_nil
+ end
+ end
+end