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
prevent duplicate ongoing per user in contract
ulfertsc486e3c6d457
diff --git a/modules/costs/app/contracts/time_entries/base_contract.rb b/modules/costs/app/contracts/time_entries/base_contract.rbindex 275350dfad9..a778a8422fe 100644--- a/modules/costs/app/contracts/time_entries/base_contract.rb+++ b/modules/costs/app/contracts/time_entries/base_contract.rb@@ -60,6 +60,7 @@ module TimeEntriesendattribute :ongoing dovalidate_self_timer+ validate_no_other_ongoingendattribute :hoursattribute :comments@@ -134,5 +135,12 @@ module TimeEntriesdef validate_self_timererrors.add :ongoing, :not_current_user if model.ongoing? && model.user != userend++ def validate_no_other_ongoing+ if model.ongoing? && model.ongoing_changed? && TimeEntry.ongoing_for_user_other_than(model.user, model).any?+ errors.add :base,+ :duplicate_ongoing+ end+ endendenddiff --git a/modules/costs/app/models/time_entries/scopes/ongoing_for_user_other_than.rb b/modules/costs/app/models/time_entries/scopes/ongoing_for_user_other_than.rbindex e69de29bb2d..a216aed9c97 100644--- a/modules/costs/app/models/time_entries/scopes/ongoing_for_user_other_than.rb+++ b/modules/costs/app/models/time_entries/scopes/ongoing_for_user_other_than.rb@@ -0,0 +1,41 @@+# 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.+# ++++module TimeEntries::Scopes+ module OngoingForUserOtherThan+ extend ActiveSupport::Concern++ class_methods do+ def ongoing_for_user_other_than(user, time_entry)+ ongoing.where(user:).where.not(id: time_entry.id)+ end+ end+ end+enddiff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rbindex 552d007f068..ee502d13d4c 100644--- a/modules/costs/app/models/time_entry.rb+++ b/modules/costs/app/models/time_entry.rb@@ -84,7 +84,8 @@ class TimeEntry < ApplicationRecordinclude Entry::SplashedDatesscopes :of_user_and_day,- :ongoing+ :ongoing,+ :ongoing_for_user_other_than# TODO: move into servicebefore_save :update_costsdiff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.ymlindex 9cea9a193dc..707aef6fae1 100644--- a/modules/costs/config/locales/en.yml+++ b/modules/costs/config/locales/en.yml@@ -86,6 +86,7 @@ en:time_entry:invalid_time: "must be between 00:00 and 23:59."cannot_log_for_this_work_package: "Cannot log time for this work package."+ duplicate_ongoing: "An ongoing time entry already exists for this user."work_package:is_not_a_valid_target_for_cost_entries: "Work package #%{id} is not a valid target for reassigning the cost entries."nullify_is_not_valid_for_cost_entries: "Cost entries can not be assigned to a project."diff --git a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb b/modules/costs/spec/contracts/time_entries/create_contract_spec.rbindex e967cb1a2cb..8f189c446ad 100644--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb@@ -48,7 +48,6 @@ RSpec.describe TimeEntries::CreateContract dot.changed_by_system(changed_by_system) if changed_by_systemendend- let(:time_entry_ongoing) { false }let(:permissions) { %i(log_time) }let(:other_user) { build_stubbed(:user) }let(:changed_by_system) dodiff --git a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rbindex c8027517da6..bba2f60b038 100644--- a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb+++ b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb@@ -50,6 +50,7 @@ RSpec.shared_examples_for "time entry contract" dolet(:time_entry_spent_on) { Time.zone.today }let(:time_entry_hours) { 5 }let(:time_entry_comments) { "A comment" }+ let(:time_entry_ongoing) { false }let(:work_package_visible) { true }let(:user_visible) { true }let(:time_entry_day_sum) { 5 }@@ -224,6 +225,21 @@ RSpec.shared_examples_for "time entry contract" doendend+ context "when ongoing and another ongoing time entry already exists for the current user" do+ let(:time_entry_ongoing) { true }++ before do+ allow(TimeEntry)+ .to receive(:ongoing_for_user_other_than)+ .with(time_entry_user, time_entry)+ .and_return([build_stubbed(:time_entry, user: time_entry_user, ongoing: true)])+ end++ it "is invalid" do+ expect_valid(false, base: %i(duplicate_ongoing))+ end+ end+describe "assignable_activities" docontext "if no project is set" dolet(:time_entry_project) { nil }diff --git a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb b/modules/costs/spec/contracts/time_entries/update_contract_spec.rbindex 90a2595bc34..db5ebc848fb 100644--- a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb+++ b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb@@ -39,7 +39,9 @@ RSpec.describe TimeEntries::UpdateContract doactivity: time_entry_activity,spent_on: time_entry_spent_on,hours: time_entry_hours,- comments: time_entry_comments)+ comments: time_entry_comments) do |te|+ te.ongoing = time_entry_ongoing+ endendsubject(:contract) { described_class.new(time_entry, current_user) }diff --git a/modules/costs/spec/models/time_entries/scopes/ongoing_for_user_other_than_spec.rb b/modules/costs/spec/models/time_entries/scopes/ongoing_for_user_other_than_spec.rbindex e69de29bb2d..20fd96d7953 100644--- a/modules/costs/spec/models/time_entries/scopes/ongoing_for_user_other_than_spec.rb+++ b/modules/costs/spec/models/time_entries/scopes/ongoing_for_user_other_than_spec.rb@@ -0,0 +1,79 @@+# 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"++RSpec.describe TimeEntries::Scopes::OngoingForUserOtherThan do+ shared_let(:user_with_ongoing) { create(:user) }+ shared_let(:user_without_ongoing) { create(:user) }+ shared_let(:ongoing_time_entry) { create(:time_entry, user: user_with_ongoing, ongoing: true) }+ shared_let(:not_ongoing_time_entry) { create(:time_entry, user: user_without_ongoing, ongoing: false) }++ describe ".ongoing_for_user_other_than" do+ context "for a user with an ongoing time entry - for another time entry" do+ let(:other_time_entry_of_user) { create(:time_entry, user: user_with_ongoing, ongoing: false) }++ it "returns the ongoing time entry" do+ expect(TimeEntry.ongoing_for_user_other_than(user_with_ongoing, other_time_entry_of_user))+ .to contain_exactly(ongoing_time_entry)+ end+ end++ context "for a user with an ongoing time entry - for that ongoing time entry" do+ it "returns nothing" do+ expect(TimeEntry.ongoing_for_user_other_than(user_with_ongoing, ongoing_time_entry))+ .to be_empty+ end+ end++ context "for a user with an ongoing time entry - for a new ongoing time entry" do+ it "returns the ongoing time entry" do+ expect(TimeEntry.ongoing_for_user_other_than(user_with_ongoing, TimeEntry.new(user: user_with_ongoing, ongoing: true)))+ .to contain_exactly(ongoing_time_entry)+ end+ end++ context "for a user without an ongoing time entry - for another time entry" do+ let(:other_time_entry_of_user) { create(:time_entry, user: user_without_ongoing, ongoing: false) }++ it "returns nothing" do+ expect(TimeEntry.ongoing_for_user_other_than(user_without_ongoing, other_time_entry_of_user))+ .to be_empty+ end+ end++ context "for a user without an ongoing time entry - for a new ongoing time entry" do+ it "returns nothing" do+ expect(TimeEntry.ongoing_for_user_other_than(user_without_ongoing, TimeEntry.new(user: user_with_ongoing, ongoing: true)))+ .to be_empty+ end+ end+ end+end