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

limit logged time to 24 hours a day

ulferts0efd4a5abdd1
diff --git a/app/contracts/time_entries/base_contract.rb b/app/contracts/time_entries/base_contract.rb
index c7b3904a594..93b57662120 100644
--- a/app/contracts/time_entries/base_contract.rb
+++ b/app/contracts/time_entries/base_contract.rb
@@ -58,7 +58,9 @@ module TimeEntries
attribute :activity_id do
validate_activity_active
end
- attribute :hours
+ attribute :hours do
+ validate_day_limit
+ end
attribute :comments
attribute_alias :comments, :comment
@@ -98,6 +100,14 @@ module TimeEntries
errors.add :activity_id, :inclusion if model.activity_id && !assignable_activities.exists?(model.activity_id)
end
+ def validate_day_limit
+ return unless model.spent_on && model.hours
+
+ if hours_extending_day_limit?
+ errors.add :hours, :day_limit
+ end
+ end
+
def work_package_invisible?
model.work_package.nil? || !model.work_package.visible?(user)
end
@@ -105,5 +115,11 @@ module TimeEntries
def work_package_not_in_project?
model.work_package && model.project != model.work_package.project
end
+
+ def hours_extending_day_limit?
+ existing_sum = TimeEntry::Scopes::OfUserAndDay.fetch(model.user, model.spent_on, excluding: model).sum(:hours)
+
+ existing_sum + model.hours > 24
+ end
end
end
diff --git a/app/models/time_entry/scopes/of_user_and_day.rb b/app/models/time_entry/scopes/of_user_and_day.rb
new file mode 100644
index 00000000000..c794ba18daf
--- /dev/null
+++ b/app/models/time_entry/scopes/of_user_and_day.rb
@@ -0,0 +1,45 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module TimeEntry::Scopes
+ class OfUserAndDay
+ def self.fetch(user, date, excluding: nil)
+ scope = TimeEntry
+ .where(spent_on: date,
+ user: user)
+
+ if excluding
+ scope = scope.where.not(id: excluding.id)
+ end
+
+ scope
+ end
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 4156fd46a1e..4fbd6a32765 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -647,6 +647,10 @@ en:
role:
permissions:
dependency_missing: "need to also include '%{dependency}' as '%{permission}' is selected."
+ time_entry:
+ attributes:
+ hours:
+ day_limit: "more than 24 hours logged for the date."
work_package:
is_not_a_valid_target_for_time_entries: "Work package #%{id} is not a valid target for reassigning the time entries."
attributes:
diff --git a/spec/contracts/time_entries/shared_contract_examples.rb b/spec/contracts/time_entries/shared_contract_examples.rb
index 19ce124b6eb..3f4307acb0d 100644
--- a/spec/contracts/time_entries/shared_contract_examples.rb
+++ b/spec/contracts/time_entries/shared_contract_examples.rb
@@ -54,6 +54,7 @@ shared_examples_for 'time entry contract' do
let(:time_entry_hours) { 5 }
let(:time_entry_comments) { "A comment" }
let(:work_package_visible) { true }
+ let(:time_entry_day_sum) { 5 }
let(:activities_scope) do
scope = double('activities')
@@ -77,7 +78,23 @@ shared_examples_for 'time entry contract' do
allow(TimeEntryActivity::Scopes::ActiveInProject)
.to receive(:fetch)
- .and_return(TimeEntryActivity.where('1=0'))
+ .and_return(TimeEntryActivity.none)
+
+ of_user_and_day_scope = double('of_user_and_day_scope')
+
+ allow(TimeEntry::Scopes::OfUserAndDay)
+ .to receive(:fetch)
+ .and_return(TimeEntry.none)
+
+ allow(TimeEntry::Scopes::OfUserAndDay)
+ .to receive(:fetch)
+ .with(time_entry.user, time_entry_spent_on, excluding: time_entry)
+ .and_return(of_user_and_day_scope)
+
+ allow(of_user_and_day_scope)
+ .to receive(:sum)
+ .with(:hours)
+ .and_return(time_entry_day_sum)
allow(TimeEntryActivity::Scopes::ActiveInProject)
.to receive(:fetch)
@@ -185,6 +202,14 @@ shared_examples_for 'time entry contract' do
it_behaves_like 'is valid'
end
+ context 'if more than 24 hours are booked for a day' do
+ let(:time_entry_day_sum) { 24 - time_entry_hours + 1 }
+
+ it 'is invalid' do
+ expect_valid(false, hours: %i(day_limit))
+ end
+ end
+
describe 'assignable_activities' do
context 'if no project is set' do
let(:time_entry_project) { nil }
diff --git a/spec/models/changeset_spec.rb b/spec/models/changeset_spec.rb
index 8f98aa084f3..bd7a4c0942e 100644
--- a/spec/models/changeset_spec.rb
+++ b/spec/models/changeset_spec.rb
@@ -228,13 +228,13 @@ describe Changeset, type: :model do
'15m' => 0.25,
'15min' => 0.25,
'3h15' => 3.25,
- '3h15m' => 3.25,
- '3h15min' => 3.25,
- '3:15' => 3.25,
- '3.25' => 3.25,
- '3.25h' => 3.25,
- '3,25' => 3.25,
- '3,25h' => 3.25
+ '2h15m' => 2.25,
+ '2h15min' => 2.25,
+ '2:15' => 2.25,
+ '2.25' => 2.25,
+ '1.25h' => 1.25,
+ '0,75' => 0.75,
+ '1,25h' => 1.25
}.each do |syntax, expected_hours|
c = Changeset.new repository: repository,
committed_on: 24.hours.ago,
diff --git a/spec/models/time_entry/scopes/of_user_and_day_spec.rb b/spec/models/time_entry/scopes/of_user_and_day_spec.rb
new file mode 100644
index 00000000000..33a5dd9999b
--- /dev/null
+++ b/spec/models/time_entry/scopes/of_user_and_day_spec.rb
@@ -0,0 +1,74 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe TimeEntry::Scopes::OfUserAndDay, type: :model do
+ let(:user) { FactoryBot.create(:user) }
+ let(:spent_on) { Date.today }
+ let!(:time_entry) do
+ FactoryBot.create(:time_entry,
+ user: user,
+ spent_on: spent_on)
+ end
+ let!(:other_time_entry) do
+ FactoryBot.create(:time_entry,
+ user: user,
+ spent_on: spent_on)
+ end
+ let!(:other_user_time_entry) do
+ FactoryBot.create(:time_entry,
+ user: FactoryBot.create(:user),
+ spent_on: spent_on)
+ end
+ let!(:other_date_time_entry) do
+ FactoryBot.create(:time_entry,
+ user: user,
+ spent_on: spent_on - 3.days)
+ end
+
+ describe '.fetch' do
+ subject { described_class.fetch(user, spent_on) }
+
+ it 'are all the time entries of the user on the date' do
+ is_expected
+ .to match_array([time_entry, other_time_entry])
+ end
+
+ context 'if excluding a time entry' do
+ subject { described_class.fetch(user, spent_on, excluding: other_time_entry) }
+
+ it 'does not include the time entry' do
+ is_expected
+ .to match_array([time_entry])
+ end
+ end
+ end
+end