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

[#47615] Modify the Query#timestamps attributes to store data of relative date and fixed time combination

Dombi Attila82255c5bc1ae
diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb
index e52b8712195..36d63627f47 100644
--- a/app/controllers/repositories_controller.rb
+++ b/app/controllers/repositories_controller.rb
@@ -492,19 +492,3 @@ class RepositoriesController < ApplicationController
ent.dup.force_encoding('UTF-8') == ent.dup.force_encoding('BINARY')
end
end
-
-class Date
- def months_ago(date = Date.today)
- ((date.year - year) * 12) + (date.month - month)
- end
-
- def weeks_ago(date = Date.today)
- ((date.year - year) * 52) + (date.cweek - cweek)
- end
-end
-
-class String
- def with_leading_slash
- starts_with?('/') ? self : "/#{self}"
- end
-end
diff --git a/app/models/day.rb b/app/models/day.rb
index 7e8f1ae7028..89e7d0609e2 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -42,6 +42,8 @@ class Day < ApplicationRecord
delegate :name, to: :week_day, allow_nil: true
+ scope :working, -> { where(working: true) }
+
def self.default_scope
today = Time.zone.today
from = today.at_beginning_of_month
@@ -56,6 +58,8 @@ class Day < ApplicationRecord
end
def self.from_sql(from:, to:)
+ from = from.to_date
+ to = to.to_date
<<~SQL.squish
(SELECT
to_char(dd, 'YYYYMMDD')::integer id,
@@ -75,6 +79,11 @@ class Day < ApplicationRecord
SQL
end
+ def self.last_working
+ # Look up only from 8 days ago, because the Setting.working_days must have at least 1 working day.
+ from_range(from: 8.days.ago, to: Time.zone.yesterday).where(working: true).last
+ end
+
def week_day
WeekDay.new(day: day_of_week)
end
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index 2b2f6c898df..746706c3dcd 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -31,21 +31,44 @@ class Timestamp
class Exception < StandardError; end
- class ISO8601Parser
+ class TimestampParser
def initialize(string)
- @iso8601_string = string
+ @original_string = string
end
def parse!
- @iso8601_string = self.class.substitute_special_shortcut_values(@iso8601_string)
+ @timestamp_string = self.class.substitute_special_shortcut_values(@original_string)
- if @iso8601_string.start_with? /[+-]?P/ # ISO8601 "Period"
- ActiveSupport::Duration.parse(@iso8601_string).iso8601
+ case @timestamp_string
+ when /[+-]?P/ # ISO8601 "Period"
+ ActiveSupport::Duration.parse(@timestamp_string).iso8601
+ when /@/ # Built in date bare words
+ parse_relative_date(@timestamp_string).iso8601
else
- Time.zone.iso8601(@iso8601_string).iso8601
+ Time.zone.iso8601(@timestamp_string).iso8601
end
rescue ArgumentError => e
- raise e.class, "The string \"#{@iso8601_string}\" cannot be parsed to Time or ActiveSupport::Duration."
+ raise e.class, "The string \"#{@original_string}\" cannot be parsed to a Timestamp."
+ end
+
+ def parse_relative_date(string)
+ date = case string
+ when /yesterday@/
+ 1.day.ago
+ when /^lastWorkingDay@/
+ Day.last_working.date || 1.day.ago
+ when /^lastWeek@/
+ 1.week.ago
+ when /^lastMonth@/
+ 1.month.ago
+ end
+
+ hours = string[/@(([0-1]?[0-9]|2[0-3]):[0-5][0-9])$/, 1].presence
+ unless date && hours
+ raise ArgumentError, "The string \"#{string}\" cannot be parsed to a Timestamp."
+ end
+
+ Time.zone.parse(hours, date)
end
class << self
@@ -83,19 +106,19 @@ class Timestamp
end
class << self
- def parse(iso8601_string)
- return iso8601_string if iso8601_string.is_a?(Timestamp)
+ def parse(timestamp_string)
+ return timestamp_string if timestamp_string.is_a?(Timestamp)
- iso8601_string = ISO8601Parser.new(iso8601_string.strip).parse!
- new(iso8601_string)
+ timestamp_string = TimestampParser.new(timestamp_string.strip).parse!
+ new(timestamp_string)
end
# Take a comma-separated string of ISO-8601 timestamps and convert it
# into an array of Timestamp objects.
#
- def parse_multiple(comma_separated_iso8601_string)
- comma_separated_iso8601_string.to_s.split(",").compact_blank.collect do |iso8601_string|
- Timestamp.parse(iso8601_string)
+ def parse_multiple(comma_separated_timestamp_string)
+ comma_separated_timestamp_string.to_s.split(",").compact_blank.collect do |timestamp_string|
+ Timestamp.parse(timestamp_string)
end
end
@@ -106,9 +129,9 @@ class Timestamp
def initialize(arg = Timestamp.now.to_s)
if arg.is_a? String
- @timestamp_iso8601_string = ISO8601Parser.substitute_special_shortcut_values(arg)
+ @timestamp_string = TimestampParser.substitute_special_shortcut_values(arg)
elsif arg.respond_to? :iso8601
- @timestamp_iso8601_string = arg.iso8601
+ @timestamp_string = arg.iso8601
else
raise Timestamp::Exception,
"Argument type not supported. " \
@@ -129,7 +152,7 @@ class Timestamp
end
def iso8601
- @timestamp_iso8601_string.to_s
+ @timestamp_string.to_s
end
def to_iso8601
@@ -146,7 +169,7 @@ class Timestamp
def to_time
if relative?
- Time.zone.now - (to_duration * (to_duration.to_i.positive? ? 1 : -1))
+ Time.zone.now - to_duration.abs
else
Time.zone.parse(self)
end
diff --git a/spec/factories/non_working_day_factory.rb b/spec/factories/non_working_day_factory.rb
index e6a8fd6f31c..9f4e20b78cc 100644
--- a/spec/factories/non_working_day_factory.rb
+++ b/spec/factories/non_working_day_factory.rb
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :non_working_day do
- sequence(:name) { |n| "Non working on #{Date.current + n.day} " }
+ sequence(:name) { "Non working on #{date} " }
sequence(:date) { |n| Date.current + n.day }
end
end
diff --git a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
index 7f441209baa..ae50f80200d 100644
--- a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
+++ b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
@@ -171,14 +171,14 @@ describe API::V3::Queries::QueryRepresenter, 'parsing' do
end
describe 'timestamps' do
- let(:timestamp_params) { [1.week.ago.iso8601, 'P0D'] }
+ let(:timestamp_params) { [1.week.ago.iso8601, 'lastWorkingDay@12:00', 'P0D'] }
let(:request_body) do
{ 'timestamps' => timestamp_params }
end
it 'sets timestamps' do
expect(subject.timestamps)
- .to eq([1.week.ago.iso8601, 'P0D'])
+ .to eq([1.week.ago.iso8601, 'lastWorkingDay@12:00', 'P0D'])
end
end
end
diff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rb
index 63a9cc2e2ae..a2888da03c2 100644
--- a/spec/models/day_spec.rb
+++ b/spec/models/day_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper'
describe Day do
+ shared_let(:week_days) { week_with_saturday_and_sunday_as_weekend }
let(:today) { Date.current }
let(:date_range) { Date.new(2022, 1, 1)..Date.new(2022, 2, 1) }
let(:first_of_year) { date_range.begin }
@@ -49,7 +50,6 @@ describe Day do
end
context 'for collection with multiple non-working days' do
- shared_let(:week_days) { week_with_saturday_and_sunday_as_weekend }
let(:non_working_dates) { [date_range.begin, date_range.begin + 1.day] }
before do
@@ -83,6 +83,44 @@ describe Day do
end
end
+ describe '.last_working' do
+ subject { described_class.last_working }
+
+ around do |ex|
+ Timecop.travel(current_time, &ex)
+ end
+
+ context 'when today is Monday' do
+ let(:current_time) { Time.current.monday }
+
+ context 'when yesterday is a weekend day' do
+ it 'returns last Friday' do
+ expect(subject.date).to eq(current_time.prev_occurring(:friday))
+ end
+ end
+ end
+
+ context 'when today is Tuesday' do
+ let(:current_time) { Time.current.monday + 1.day }
+
+ context 'when yesterday is working' do
+ it 'returns Monday' do
+ expect(subject.date).to eq(current_time.yesterday)
+ end
+ end
+
+ context 'when yesterday is non-working' do
+ before do
+ create(:non_working_day, date: current_time.yesterday)
+ end
+
+ it 'returns last Friday' do
+ expect(subject.date).to eq(current_time.prev_occurring(:friday))
+ end
+ end
+ end
+ end
+
describe '#working' do
context 'when the week day is non-working' do
shared_let(:working_days) { week_with_no_working_days }
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index b6fe7ca9129..4d740eecc68 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -171,6 +171,53 @@ describe Timestamp do
end
end
+ describe "when providing bare words for dates" do
+ describe "yesterday@12:00" do
+ subject { described_class.parse("yesterday@12:00") }
+
+ it "returns a Timestamp representing the yesterday at 12:00 pm" do
+ expect(subject).to be_a described_class
+ expect(subject.relative?).to be false
+ expect(subject.to_time).to eq 1.day.ago.change(hour: 12)
+ end
+ end
+
+ describe "lastWorkingDay@12:00" do
+ subject { described_class.parse("lastWorkingDay@12:00") }
+
+ before do
+ week_with_all_days_working
+ create(:non_working_day, date: Time.zone.yesterday)
+ end
+
+ it "returns a Timestamp representing the last working day at 12:00 pm" do
+ expect(subject).to be_a described_class
+ expect(subject.relative?).to be false
+ expect(subject.to_time).to eq 2.days.ago.change(hour: 12)
+ end
+ end
+
+ describe "lastWeek@12:00" do
+ subject { described_class.parse("lastWeek@12:00") }
+
+ it "returns a Timestamp representing the last week at 12:00 pm" do
+ expect(subject).to be_a described_class
+ expect(subject.relative?).to be false
+ expect(subject.to_time).to eq 1.week.ago.change(hour: 12)
+ end
+ end
+
+ describe "lastMonth@12:00" do
+ subject { described_class.parse("lastMonth@12:00") }
+
+ it "returns a Timestamp representing the last month at 12:00 pm" do
+ expect(subject).to be_a described_class
+ expect(subject.relative?).to be false
+ expect(subject.to_time).to eq 1.month.ago.change(hour: 12)
+ end
+ end
+ end
+
describe "when providing something invalid" do
subject { described_class.parse("foo") }
@@ -186,6 +233,22 @@ describe Timestamp do
expect { subject }.to raise_error ActiveSupport::Duration::ISO8601Parser::ParsingError
end
end
+
+ describe "when providing something invalid with date bare words" do
+ subject { described_class.parse("yesterday@") }
+
+ it "raises an error" do
+ expect { subject }.to raise_error ArgumentError
+ end
+ end
+
+ describe "when providing something invalid with date bare words#2" do
+ subject { described_class.parse("yesterday@11:22:asd") }
+
+ it "raises an error" do
+ expect { subject }.to raise_error ArgumentError
+ end
+ end
end
describe 'when providing a Timestamp' do
diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb
index 2a725e50b5d..aa6b276e38f 100644
--- a/spec/requests/api/v3/queries/create_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb
@@ -326,7 +326,7 @@ describe "POST /api/v3/queries/form" do
describe 'with all parameters given' do
let(:status) { create(:status) }
- let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
+ let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00', "P0D"] }
let(:parameters) do
{
@@ -586,6 +586,17 @@ describe "POST /api/v3/queries/form" do
end
end
+ context 'when one timestamp cannot be parsed (malformed)#2' do
+ let(:override_params) do
+ { timestamps: ['LastWorkingDayInvalid@12:00', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: LastWorkingDayInvalid@12:00"
+ end
+ end
+
context 'when both timestamps cannot be parsed' do
let(:override_params) do
{ timestamps: ['invalid', 'invalid2'] }
diff --git a/spec/requests/api/v3/queries/create_query_spec.rb b/spec/requests/api/v3/queries/create_query_spec.rb
index a5bb888b6db..f5310d051d1 100644
--- a/spec/requests/api/v3/queries/create_query_spec.rb
+++ b/spec/requests/api/v3/queries/create_query_spec.rb
@@ -32,7 +32,7 @@ describe "POST /api/v3/queries" do
shared_let(:user) { create(:admin) }
shared_let(:status) { create(:status) }
shared_let(:project) { create(:project) }
- shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
+ shared_let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00', "P0D"] }
let(:default_params) do
{
diff --git a/spec/requests/api/v3/queries/update_form_api_spec.rb b/spec/requests/api/v3/queries/update_form_api_spec.rb
index ca61121979d..e4d8aa766c9 100644
--- a/spec/requests/api/v3/queries/update_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/update_form_api_spec.rb
@@ -35,7 +35,7 @@ describe "POST /api/v3/queries/form" do
let(:user) { create(:admin) }
let(:role) { create(:existing_role, permissions:) }
let(:permissions) { %i(view_work_packages manage_public_queries) }
- let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
+ let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00', "P0D"] }
let!(:project) { create(:project_with_types, members: { user => role }) }
@@ -542,6 +542,28 @@ describe "POST /api/v3/queries/form" do
end
end
+ context 'when one timestamp cannot be parsed (malformed)' do
+ let(:override_params) do
+ { timestamps: ['2022-03-02 invalid string 20:45:56Z', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: 2022-03-02 invalid string 20:45:56Z"
+ end
+ end
+
+ context 'when one timestamp cannot be parsed (malformed)#2' do
+ let(:override_params) do
+ { timestamps: ['LastWorkingDayInvalid@12:00', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: LastWorkingDayInvalid@12:00"
+ end
+ end
+
context 'when both timestamps cannot be parsed' do
let(:override_params) do
{ timestamps: ['invalid', 'invalid2'] }
diff --git a/spec/requests/api/v3/queries/update_query_spec.rb b/spec/requests/api/v3/queries/update_query_spec.rb
index e46a2155108..2df818b7300 100644
--- a/spec/requests/api/v3/queries/update_query_spec.rb
+++ b/spec/requests/api/v3/queries/update_query_spec.rb
@@ -32,7 +32,7 @@ describe "PATCH /api/v3/queries/:id" do
shared_let(:user) { create(:admin) }
shared_let(:status) { create(:status) }
shared_let(:project) { create(:project) }
- shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
+ shared_let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00', "P0D"] }
let!(:query) do
create(