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
Add dates_interval and working filters for api/v3/days endpoint
Dombi Attilac9e4a7dd6e92
diff --git a/app/models/day.rb b/app/models/day.rbindex 5c98e020806..15b77503735 100644--- a/app/models/day.rb+++ b/app/models/day.rb@@ -46,7 +46,12 @@ class Day < ApplicationRecordfrom = today.at_beginning_of_monthto = today.next_month.at_end_of_month- days_sql = <<~SQL.squish+ select('days.*')+ .from(Arel.sql(from_sql(from:, to:)))+ end++ def self.from_sql(from:, to:)+ <<~SQL.squish(SELECTdate_trunc('day', dd)::date date,@@ -61,8 +66,5 @@ class Day < ApplicationRecordORDER BY date) daysSQL-- select('days.*')- .from(days_sql)endenddiff --git a/app/models/queries/days.rb b/app/models/queries/days.rbnew file mode 100644index 00000000000..51b3328c91c--- /dev/null+++ b/app/models/queries/days.rb@@ -0,0 +1,34 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 Queries::Days+ ::Queries::Register.register(DayQuery) do+ filter Filters::DatesIntervalFilter+ filter Filters::WorkingFilter+ end+enddiff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rbindex 2b8eba8227a..996c04e9f80 100644--- a/app/models/queries/days/day_query.rb+++ b/app/models/queries/days/day_query.rb@@ -35,7 +35,19 @@ class Queries::Days::DayQuery < Queries::BaseQueryDay.defaultend+ ##+ # This override is necessary, the dates interval filter needs to adjust the+ # `from` clause of the query. To update the `from` clause, we reverse merge the filters,+ # otherwise the `from` clause of the filter is ignored.+ def apply_filters(scope)+ filters.each do |filter|+ scope = filter.scope.merge(scope)+ end++ scope+ end+def results- super.reorder('date ASC')+ super.reorder(date: :asc)endenddiff --git a/app/models/queries/days/filters/dates_interval_filter.rb b/app/models/queries/days/filters/dates_interval_filter.rbnew file mode 100644index 00000000000..00511675f33--- /dev/null+++ b/app/models/queries/days/filters/dates_interval_filter.rb@@ -0,0 +1,62 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++class Queries::Days::Filters::DatesIntervalFilter < Queries::Days::Filters::DayFilter+ include Queries::Operators::DateRangeClauses++ def type+ :date+ end++ def self.key+ :date+ end++ def from+ from, to = values.map { |v| v.blank? ? nil : Date.parse(v) }++ # Both from and to cannot be blank at this point+ if from.nil?+ from = to.at_beginning_of_month+ end++ if to.nil?+ to = from.next_month.at_end_of_month+ end++ model.from_sql(from:, to:)+ end++ def type_strategy+ @type_strategy ||= Queries::Filters::Strategies::DateInterval.new(self)+ end++ def connection+ ActiveRecord::Base::connection+ end+enddiff --git a/app/models/queries/days/filters/day_filter.rb b/app/models/queries/days/filters/day_filter.rbnew file mode 100644index 00000000000..93262cb6dfa--- /dev/null+++ b/app/models/queries/days/filters/day_filter.rb@@ -0,0 +1,35 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++class Queries::Days::Filters::DayFilter < Queries::Filters::Base+ self.model = Day++ def human_name+ model.human_attribute_name(name)+ end+enddiff --git a/app/models/queries/days/filters/working_filter.rb b/app/models/queries/days/filters/working_filter.rbnew file mode 100644index 00000000000..8997cb23ce4--- /dev/null+++ b/app/models/queries/days/filters/working_filter.rb@@ -0,0 +1,35 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++class Queries::Days::Filters::WorkingFilter < Queries::Days::Filters::DayFilter+ include Queries::Filters::Shared::BooleanFilter++ def self.key+ :working+ end+enddiff --git a/app/models/queries/filters/base.rb b/app/models/queries/filters/base.rbindex faf12008e6a..a6664cebacb 100644--- a/app/models/queries/filters/base.rb+++ b/app/models/queries/filters/base.rb@@ -73,7 +73,7 @@ class Queries::Filters::Basedef filter_instance_optionsvalues = filter_params.map { |key| [key, send(key)] }- initial_options.merge(Hash[values])+ initial_options.merge(values.to_h)enddef human_name@@ -88,9 +88,7 @@ class Queries::Filters::Basenilend- def valid_values!- type_strategy.valid_values!- end+ delegate :valid_values!, to: :type_strategydef available?true@@ -106,6 +104,7 @@ class Queries::Filters::Basedef scopescope = model.where(where)+ scope = scope.from(from) if fromscope = scope.joins(joins) if joinsscope = scope.left_outer_joins(left_outer_joins) if left_outer_joinsscope@@ -120,13 +119,17 @@ class Queries::Filters::Baseenddef self.all_for(context = nil)- create!(name: key, context: context)+ create!(name: key, context:)enddef whereoperator_strategy.sql_for_field(values, self.class.model.table_name, self.class.key)end+ def from+ nil+ end+def joinsnilend@@ -194,7 +197,7 @@ class Queries::Filters::Baseenddef validate_presence_of_values- if operator_strategy&.requires_value? && (values.nil? || values.reject(&:blank?).empty?)+ if operator_strategy&.requires_value? && (values.nil? || values.compact_blank.empty?)errors.add(:values, I18n.t('activerecord.errors.messages.blank'))endenddiff --git a/docs/api/apiv3/paths/days.yml b/docs/api/apiv3/paths/days.ymlindex cff47067eae..b9499060db2 100644--- a/docs/api/apiv3/paths/days.yml+++ b/docs/api/apiv3/paths/days.yml@@ -19,11 +19,11 @@ get:Accepts the same format as returned by the [queries](https://www.openproject.org/docs/api/endpoints/queries/)endpoint. Currently supported filters are:- + interval: the inclusive date interval to scope days to look up. When+ + date: the inclusive date interval to scope days to look up. Whenunspecified, default is from the beginning of current month to the endof following month.- Example: `{ "interval": { "operator": "<>d", "values": ["2022-05-02","2022-05-26"] } }`+ Example: `{ "date": { "operator": "<>d", "values": ["2022-05-02","2022-05-26"] } }`would return days between May 5 and May 26 2022, inclusive.+ working: when `true`, returns only the working days. When `false`,@@ -32,7 +32,7 @@ get:Example: `{ "working": { "operator": "=", "values": ["t"] } }`would exclude non-working days from the response.- example: '[{ "interval": { "operator": "<>d", "values": ["2022-05-02","2022-05-26"] } }, { "working": { "operator": "=", "values": ["f"] } }]'+ example: '[{ "date": { "operator": "<>d", "values": ["2022-05-02","2022-05-26"] } }, { "working": { "operator": "=", "values": ["f"] } }]'required: falseschema:type: stringdiff --git a/spec/models/queries/days/day_query_spec.rb b/spec/models/queries/days/day_query_spec.rbnew file mode 100644index 00000000000..9db6195b74e--- /dev/null+++ b/spec/models/queries/days/day_query_spec.rb@@ -0,0 +1,104 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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'++describe Queries::Days::DayQuery, type: :model do+ let(:instance) { described_class.new }+ let(:base_scope) { Day.default.order(date: :asc) }+ let(:current_user) { build_stubbed(:admin) }++ before do+ login_as(current_user)+ end++ context 'without a filter' do+ context 'as an admin' do+ it 'is the same as getting all days' do+ expect(instance.results.to_sql).to eql base_scope.to_sql+ end+ end++ context 'as a non admin' do+ let(:current_user) { build_stubbed(:user) }++ it 'is the same as getting all days' do+ expect(instance.results.to_sql).to eql base_scope.to_sql+ end+ end+ end++ context 'with a date filter using the "<>d" operator' do+ let(:date_range) { [from.iso8601, to.iso8601] }++ before do+ instance.where('date', '<>d', date_range)+ end++ context 'with dates within the default range' do+ let(:from) { Time.zone.today }+ let(:to) { 5.days.from_now.to_date }+ let(:base_scope) { Day.default.from(Day.from_sql(from:, to:)).order(date: :asc) }++ it 'is the same as handwriting the query' do+ # Expectation has to be weirdly specific to the logic of Queries::Operators::DateRangeClauses+ expected = base_scope.where("days.date > ? AND days.date <= ?",+ (from - 1.day).end_of_day,+ to.end_of_day)+ expect(instance.results.to_sql).to eql expected.to_sql+ end+ end++ context 'with dates out of the default range' do+ let(:from) { 5.days.from_now.to_date }+ let(:to) { 5.months.from_now.to_date }++ it 'returns all the days' do+ expect(instance.results.size).to be 149+ end++ context 'with dates missing the to date' do+ let(:date_range) { [from.iso8601, ""] }++ it 'returns days until the end of next month' do+ expected_size = (from.next_month.at_end_of_month - from).to_i + 1+ expect(instance.results.size).to be expected_size+ end+ end++ context 'with dates missing the from date' do+ let(:date_range) { ["", to.iso8601] }++ it 'returns days from the beginning of the month' do+ expected_size = (to - to.at_beginning_of_month).to_i + 1+ expect(instance.results.size).to be expected_size+ end+ end+ end+ end+enddiff --git a/spec/models/queries/days/filters/dates_interval_filter_spec.rb b/spec/models/queries/days/filters/dates_interval_filter_spec.rbnew file mode 100644index 00000000000..8b00f783931--- /dev/null+++ b/spec/models/queries/days/filters/dates_interval_filter_spec.rb@@ -0,0 +1,50 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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'++describe Queries::Days::Filters::DatesIntervalFilter, type: :model do+ it_behaves_like 'basic query filter' do+ let(:type) { :date }+ let(:class_key) { :date }++ describe '#available?' do+ it 'is true' do+ expect(instance).to be_available+ end+ end++ describe '#allowed_values' do+ it 'is nil' do+ expect(instance.allowed_values).to be_nil+ end+ end++ it_behaves_like 'non ar filter'+ end+enddiff --git a/spec/models/queries/days/filters/working_filter_spec.rb b/spec/models/queries/days/filters/working_filter_spec.rbnew file mode 100644index 00000000000..7705dfc9980--- /dev/null+++ b/spec/models/queries/days/filters/working_filter_spec.rb@@ -0,0 +1,36 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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'++describe Queries::Days::Filters::WorkingFilter, type: :model do+ it_behaves_like 'boolean query filter' do+ let(:model) { Day }+ let(:attribute) { :working }+ end+enddiff --git a/spec/requests/api/v3/days/day_spec.rb b/spec/requests/api/v3/days/day_spec.rbindex 869c962ee6e..f86699dd0ea 100644--- a/spec/requests/api/v3/days/day_spec.rb+++ b/spec/requests/api/v3/days/day_spec.rb@@ -33,12 +33,13 @@ describe ::API::V3::Days::DaysAPI,include API::V3::Utilities::PathHelperlet(:parsed_response) { JSON.parse(last_response.body) }+ let(:filters) { [] }current_user { user }before docreate(:week_days)- get api_v3_paths.days+ get api_v3_paths.path_for :days, filters:endcontext 'for an admin user' do@@ -46,5 +47,14 @@ describe ::API::V3::Days::DaysAPI,nb_days = Time.zone.today.end_of_month.day + Time.zone.today.next_month.end_of_month.dayit_behaves_like 'API V3 collection response', nb_days, nb_days, 'Day'++ context 'when filtering by date' do+ let(:filters) do+ [{ date: { operator: '<>d',+ values: [Time.zone.today.iso8601, 5.days.from_now.to_date.iso8601] } }]+ end++ it_behaves_like 'API V3 collection response', 6, 6, 'Day'+ endendend