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

Add notification group_by for projects with transformed keys

Oliver Günthere5dec2dc31d8
diff --git a/app/models/queries/base_query.rb b/app/models/queries/base_query.rb
index de7e658bf02..7e1401ca824 100644
--- a/app/models/queries/base_query.rb
+++ b/app/models/queries/base_query.rb
@@ -73,9 +73,8 @@ class Queries::BaseQuery
end
def group_values
- groups
- .pluck(group_by.name, Arel.sql('COUNT(*)'))
- .to_h
+ groups_hash = groups.pluck(group_by.name, Arel.sql('COUNT(*)')).to_h
+ instantiate_group_keys groups_hash
end
def where(attribute, operator, values)
@@ -211,6 +210,16 @@ class Queries::BaseQuery
end
end
+ def instantiate_group_keys(groups)
+ return groups unless group_by&.association_class
+
+ ar_keys = group_by.association_class.where(id: groups.keys.compact)
+
+ groups.transform_keys do |key|
+ ar_keys.detect { |ar_key| ar_key.id == key } || "#{key} #{I18n.t(:label_not_found)}"
+ end
+ end
+
def already_ordered_by_id?(scope)
scope.order_values.any? do |order|
order.respond_to?(:value) && order.value.respond_to?(:relation) &&
diff --git a/app/models/queries/filters/available_filters.rb b/app/models/queries/filters/available_filters.rb
index 9c022ebbef2..675a97876ed 100644
--- a/app/models/queries/filters/available_filters.rb
+++ b/app/models/queries/filters/available_filters.rb
@@ -59,7 +59,7 @@ module Queries
initialized_filters.select(&:available?)
end
- def filter_for(key, no_memoization = false)
+ def filter_for(key, no_memoization: false)
filter = get_initialized_filter(key, no_memoization)
raise ::Queries::Filters::MissingError if filter.nil?
@@ -67,11 +67,11 @@ module Queries
filter
rescue ::Queries::Filters::InvalidError => e
Rails.logger.error "Failed to register filter for #{key}: #{e} \n" \
- "Falling back to non-existing filter."
+ "Falling back to non-existing filter."
non_existing_filter(key)
rescue ::Queries::Filters::MissingError => e
Rails.logger.error "Failed to find filter for #{key}: #{e} \n" \
- "Falling back to non-existing filter."
+ "Falling back to non-existing filter."
non_existing_filter(key)
end
@@ -129,4 +129,3 @@ module Queries
end
end
end
-
diff --git a/app/models/queries/filters/not_existing_filter.rb b/app/models/queries/filters/not_existing_filter.rb
index ddd7da6d678..db2f3653ec2 100644
--- a/app/models/queries/filters/not_existing_filter.rb
+++ b/app/models/queries/filters/not_existing_filter.rb
@@ -44,7 +44,7 @@ module Queries
end
def human_name
- name.to_s.blank? ? type : name.to_s
+ name.to_s.presence || type
end
validate :always_false
@@ -81,6 +81,5 @@ module Queries
nil
end
end
-
end
end
diff --git a/app/models/queries/group_bys/base.rb b/app/models/queries/group_bys/base.rb
index 6863b7af82f..89b79e6537d 100644
--- a/app/models/queries/group_bys/base.rb
+++ b/app/models/queries/group_bys/base.rb
@@ -48,14 +48,24 @@ module Queries
raise NotImplementedError
end
+ def association_class
+ nil
+ end
+
def scope
- group_by
+ scope = model
+ scope = model.joins(joins) if joins
+ group_by scope
end
def name
attribute
end
+ def joins
+ nil
+ end
+
# Default to the same key for order
# as the one for group
def order_key
@@ -64,8 +74,8 @@ module Queries
protected
- def group_by
- model.group(name)
+ def group_by(scope)
+ scope.group(name)
end
end
end
diff --git a/app/models/queries/group_bys/not_existing_group_by.rb b/app/models/queries/group_bys/not_existing_group_by.rb
index b1b5ca9ff2a..86d2b4c7ff9 100644
--- a/app/models/queries/group_bys/not_existing_group_by.rb
+++ b/app/models/queries/group_bys/not_existing_group_by.rb
@@ -44,4 +44,4 @@ module Queries
end
end
end
-end
\ No newline at end of file
+end
diff --git a/app/models/queries/notifications.rb b/app/models/queries/notifications.rb
index 39bbd27525f..03381508756 100644
--- a/app/models/queries/notifications.rb
+++ b/app/models/queries/notifications.rb
@@ -39,12 +39,14 @@ module Queries::Notifications
[Queries::Notifications::Orders::DefaultOrder,
Queries::Notifications::Orders::ReasonOrder,
+ Queries::Notifications::Orders::ProjectOrder,
Queries::Notifications::Orders::ReadIanOrder].each do |order|
Queries::Register.order Queries::Notifications::NotificationQuery,
order
end
- [Queries::Notifications::GroupBys::GroupByReason].each do |group|
+ [Queries::Notifications::GroupBys::GroupByReason,
+ Queries::Notifications::GroupBys::GroupByProject].each do |group|
Queries::Register.group_by Queries::Notifications::NotificationQuery,
group
end
diff --git a/app/models/queries/notifications/group_bys/group_by_project.rb b/app/models/queries/notifications/group_bys/group_by_project.rb
new file mode 100644
index 00000000000..ed3f2a4e2d2
--- /dev/null
+++ b/app/models/queries/notifications/group_bys/group_by_project.rb
@@ -0,0 +1,45 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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::Notifications::GroupBys::GroupByProject < Queries::GroupBys::Base
+ self.model = Notification
+
+ def self.key
+ :project
+ end
+
+ def name
+ :project_id
+ end
+
+ def association_class
+ Project
+ end
+end
diff --git a/app/models/queries/notifications/orders/project_order.rb b/app/models/queries/notifications/orders/project_order.rb
new file mode 100644
index 00000000000..27712c4109b
--- /dev/null
+++ b/app/models/queries/notifications/orders/project_order.rb
@@ -0,0 +1,50 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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::Notifications::Orders::ProjectOrder < Queries::Orders::Base
+ self.model = Notification
+
+ def self.key
+ :project
+ end
+
+ def joins
+ :project
+ end
+
+ protected
+
+ def order
+ order_string = "projects.name"
+ order_string += " DESC" if direction == :desc
+
+ model.order(order_string)
+ end
+end
diff --git a/app/models/queries/register.rb b/app/models/queries/register.rb
index 6ee9a6a2d22..bd4d3c68395 100644
--- a/app/models/queries/register.rb
+++ b/app/models/queries/register.rb
@@ -70,6 +70,5 @@ module Queries::Register
:orders,
:columns,
:group_bys
-
end
end
diff --git a/app/models/queries/work_packages/filter_serializer.rb b/app/models/queries/work_packages/filter_serializer.rb
index 5fb9e7b23d6..52396fbdbfe 100644
--- a/app/models/queries/work_packages/filter_serializer.rb
+++ b/app/models/queries/work_packages/filter_serializer.rb
@@ -41,7 +41,7 @@ module Queries::WorkPackages::FilterSerializer
(YAML.load(yaml) || {}).each_with_object([]) do |(field, options), array|
options = options.with_indifferent_access
- filter = filter_for(field, true)
+ filter = filter_for(field, no_memoization: true)
filter.operator = options['operator']
filter.values = options['values']
array << filter
diff --git a/app/services/api/v3/work_package_collection_from_query_service.rb b/app/services/api/v3/work_package_collection_from_query_service.rb
index 4a09c772541..ded1386b8fa 100644
--- a/app/services/api/v3/work_package_collection_from_query_service.rb
+++ b/app/services/api/v3/work_package_collection_from_query_service.rb
@@ -104,7 +104,9 @@ module API
sums = generate_group_sums
results.work_package_count_by_group.map do |group, count|
- ::API::V3::WorkPackages::WorkPackageAggregationGroup.new(group, count, query: query, sums: sums[group], current_user: current_user)
+ ::API::V3::WorkPackages::WorkPackageAggregationGroup.new(
+ group, count, query: query, sums: sums[group], current_user: current_user
+ )
end
end
diff --git a/lib/api/v3/utilities/resource_link_generator.rb b/lib/api/v3/utilities/resource_link_generator.rb
index 2700c283dd7..1207888d953 100644
--- a/lib/api/v3/utilities/resource_link_generator.rb
+++ b/lib/api/v3/utilities/resource_link_generator.rb
@@ -51,6 +51,8 @@ module API
# since not all things are equally named between APIv3 and the rails code,
# we need to convert some names manually
case record
+ when Project
+ :project
when IssuePriority
:priority
when AnonymousUser, DeletedUser, SystemUser
diff --git a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb
index 10035792b8f..a80401670d3 100644
--- a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb
+++ b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb
@@ -72,7 +72,7 @@ describe ::API::V3::Notifications::NotificationCollectionRepresenter do
let(:groups) do
[
{ value: 'mentioned', count: 34 },
- { value: 'involved', count: 5}
+ { value: 'involved', count: 5 }
]
end
diff --git a/spec/models/queries/filters/available_filters_spec.rb b/spec/models/queries/filters/available_filters_spec.rb
index a48a56a6310..a086cd1c28d 100644
--- a/spec/models/queries/filters/available_filters_spec.rb
+++ b/spec/models/queries/filters/available_filters_spec.rb
@@ -53,24 +53,24 @@ describe Queries::Filters::AvailableFilters, type: :model do
end
describe '#filter_for' do
- let(:filter_1_available) { true }
- let(:filter_2_available) { true }
- let(:filter_1_key) { :filter_1 }
- let(:filter_2_key) { /f_\d+/ }
- let(:filter_1_name) { :filter_1 }
- let(:filter_2_name) { :f_1 }
- let(:registered_filters) { [filter_1, filter_2] }
+ let(:filter1_available) { true }
+ let(:filter2_available) { true }
+ let(:filter1_key) { :filter1 }
+ let(:filter2_key) { /f_\d+/ }
+ let(:filter1_name) { :filter1 }
+ let(:filter2_name) { :f1 }
+ let(:registered_filters) { [filter1, filter_2] }
- let(:filter_1_instance) do
- instance = double("filter_1_instance")
+ let(:filter1_instance) do
+ instance = double("filter1_instance") # rubocop:disable Rspec/VerifiedDoubles
allow(instance)
.to receive(:available?)
- .and_return(:filter_1_available)
+ .and_return(:filter1_available)
allow(instance)
.to receive(:name)
- .and_return(:filter_1)
+ .and_return(filter1_name)
allow(instance)
.to receive(:name=)
@@ -78,35 +78,35 @@ describe Queries::Filters::AvailableFilters, type: :model do
instance
end
- let(:filter_1) do
- filter = double('filter_1')
+ let(:filter1) do
+ filter = double('filter1') # rubocop:disable Rspec/VerifiedDoubles
allow(filter)
.to receive(:key)
- .and_return(:filter_1)
+ .and_return(filter1_key)
allow(filter)
.to receive(:create!)
- .and_return(filter_1_instance)
+ .and_return(filter1_instance)
allow(filter)
.to receive(:all_for)
.with(context)
- .and_return(filter_1_instance)
+ .and_return(filter1_instance)
filter
end
let(:filter_2_instance) do
- instance = double("filter_2_instance")
+ instance = double("filter_2_instance") # rubocop:disable Rspec/VerifiedDoubles
allow(instance)
.to receive(:available?)
- .and_return(:filter_2_available)
+ .and_return(filter2_available)
allow(instance)
.to receive(:name)
- .and_return(:f_1)
+ .and_return(:f1)
allow(instance)
.to receive(:name=)
@@ -115,11 +115,11 @@ describe Queries::Filters::AvailableFilters, type: :model do
end
let(:filter_2) do
- filter = double('filter_2')
+ filter = double('filter_2') # rubocop:disable Rspec/VerifiedDoubles
allow(filter)
.to receive(:key)
- .and_return(/f_\d+/)
+ .and_return(/f\d+/)
allow(filter)
.to receive(:all_for)
@@ -131,7 +131,7 @@ describe Queries::Filters::AvailableFilters, type: :model do
context 'for a filter identified by a symbol' do
let(:filter_3_available) { true }
- let(:registered_filters) { [filter_3, filter_1, filter_2] }
+ let(:registered_filters) { [filter_3, filter1, filter_2] }
# As we use regexp to find the filters
# we have to ensure that a filter identified a substring symbol
@@ -161,7 +161,7 @@ describe Queries::Filters::AvailableFilters, type: :model do
let(:filter_3_available) { false }
it 'returns an instance of the matching filter' do
- expect(includer.filter_for(:filter_1)).to eql filter_1_instance
+ expect(includer.filter_for(:filter1)).to eql filter1_instance
end
it 'returns the NotExistingFilter if the name is not matched' do
@@ -170,7 +170,7 @@ describe Queries::Filters::AvailableFilters, type: :model do
end
context 'if not available' do
- let(:filter_1_available) { false }
+ let(:filter1_available) { false }
let(:filter_3_available) { true }
it 'returns the NotExistingFilter if the name is not matched' do
@@ -178,7 +178,7 @@ describe Queries::Filters::AvailableFilters, type: :model do
end
it 'returns an instance of the matching filter if not caring for availablility' do
- expect(includer.filter_for(:filter_1, true)).to eql filter_1_instance
+ expect(includer.filter_for(:filter1, no_memoization: true)).to eql filter1_instance
end
end
end
@@ -186,23 +186,23 @@ describe Queries::Filters::AvailableFilters, type: :model do
context 'for a filter identified by a regexp' do
context 'if available' do
it 'returns an instance of the matching filter' do
- expect(includer.filter_for(:f_1)).to eql filter_2_instance
+ expect(includer.filter_for(:f1)).to eql filter_2_instance
end
it 'returns the NotExistingFilter if the key is not matched' do
- expect(includer.filter_for(:f_i1)).to be_a Queries::Filters::NotExistingFilter
+ expect(includer.filter_for(:fi1)).to be_a Queries::Filters::NotExistingFilter
end
it 'returns the NotExistingFilter if the key is matched but the name is not' do
- expect(includer.filter_for(:f_2)).to be_a Queries::Filters::NotExistingFilter
+ expect(includer.filter_for(:f2)).to be_a Queries::Filters::NotExistingFilter
end
end
context 'is false if unavailable' do
- let(:filter_2_available) { false }
+ let(:filter2_available) { false }
it 'returns the NotExistingFilter' do
- expect(includer.filter_for(:f_i)).to be_a Queries::Filters::NotExistingFilter
+ expect(includer.filter_for(:fi)).to be_a Queries::Filters::NotExistingFilter
end
end
end
diff --git a/spec/models/queries/notifications/notification_query_spec.rb b/spec/models/queries/notifications/notification_query_spec.rb
index 93c70e1ae9f..5321cf1c3d1 100644
--- a/spec/models/queries/notifications/notification_query_spec.rb
+++ b/spec/models/queries/notifications/notification_query_spec.rb
@@ -182,6 +182,25 @@ describe Queries::Notifications::NotificationQuery, type: :model do
end
end
+ context 'with a project group_by' do
+ before do
+ instance.group(:project)
+ end
+
+ describe '#results' do
+ it 'is the same as handwriting the query' do
+ expected = <<~SQL.squish
+ SELECT "notifications"."project_id", COUNT(*) FROM "notifications"
+ WHERE "notifications"."recipient_id" = #{recipient.id}
+ GROUP BY "notifications"."project_id"
+ ORDER BY "notifications"."project_id" ASC
+ SQL
+
+ expect(instance.groups.to_sql).to eql expected
+ end
+ end
+ end
+
context 'with a non existing group_by' do
before do
instance.group(:does_not_exist)
diff --git a/spec/requests/api/v3/notifications/index_resource_spec.rb b/spec/requests/api/v3/notifications/index_resource_spec.rb
index 3113379c3e9..cd15b71cb40 100644
--- a/spec/requests/api/v3/notifications/index_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/index_resource_spec.rb
@@ -43,8 +43,18 @@ describe ::API::V3::Notifications::NotificationsAPI,
member_in_project: work_package.project,
member_with_permissions: %i[view_work_packages]
end
- shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient, resource: work_package }
- shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient, resource: work_package }
+ shared_let(:notification1) do
+ FactoryBot.create :notification,
+ recipient: recipient,
+ resource: work_package,
+ project: work_package.project
+ end
+ shared_let(:notification2) do
+ FactoryBot.create :notification,
+ recipient: recipient,
+ resource: work_package,
+ project: work_package.project
+ end
let(:notifications) { [notification1, notification2] }
@@ -141,18 +151,50 @@ describe ::API::V3::Notifications::NotificationsAPI,
let(:groups) { parsed_response['groups'] }
- context 'with the filter being set to false' do
- it_behaves_like 'API V3 collection response', 3, 3, 'Notification'
+ it_behaves_like 'API V3 collection response', 3, 3, 'Notification'
- it 'contains the reason groups', :aggregate_failures do
- expect(groups).to be_a Array
- expect(groups.count).to eq 2
+ it 'contains the reason groups', :aggregate_failures do
+ expect(groups).to be_a Array
+ expect(groups.count).to eq 2
- keyed = groups.index_by { |el| el['value'] }
- expect(keyed.keys).to contain_exactly 'mentioned', 'involved'
- expect(keyed['mentioned']['count']).to eq 2
- expect(keyed['involved']['count']).to eq 1
- end
+ keyed = groups.index_by { |el| el['value'] }
+ expect(keyed.keys).to contain_exactly 'mentioned', 'involved'
+ expect(keyed['mentioned']['count']).to eq 2
+ expect(keyed['involved']['count']).to eq 1
+ end
+ end
+
+ context 'with a project groupBy' do
+ let(:work_package2) { FactoryBot.create :work_package }
+ let(:other_project_notification) do
+ FactoryBot.create :notification,
+ resource: work_package2,
+ project: work_package2.project,
+ recipient: recipient,
+ reason_ian: :involved
+ end
+
+ let(:notifications) { [notification1, notification2, other_project_notification] }
+
+ let(:send_request) do
+ get api_v3_paths.path_for :notifications, group_by: :project
+ end
+
+ let(:groups) { parsed_response['groups'] }
+
+ it_behaves_like 'API V3 collection response', 3, 3, 'Notification'
+
+ it 'contains the project groups', :aggregate_failures do
+ expect(groups).to be_a Array
+ expect(groups.count).to eq 2
+
+ keyed = groups.index_by { |el| el['value'] }
+ expect(keyed.keys).to contain_exactly work_package2.project.name, work_package.project.name
+ expect(keyed[work_package.project.name]['count']).to eq 2
+ expect(keyed[work_package2.project.name]['count']).to eq 1
+
+ expect(keyed.dig(work_package.project.name, '_links', 'valueLink')[0]['href'])
+ .to eq "/api/v3/projects/#{work_package.project.id}"
end
end
end
diff --git a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb b/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
index 03c37feacc0..d4d228f331e 100644
--- a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
+++ b/spec/services/api/v3/work_package_collection_from_query_service_spec.rb
@@ -159,14 +159,9 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
describe '#call' do
subject { instance.call(params) }
- it 'is successful' do
- is_expected
- .to be_success
- end
-
before do
stub_const('::API::V3::WorkPackages::WorkPackageCollectionRepresenter', mock_wp_representer)
- stub_const('::API::Decorators::AggregationGroup', mock_aggregation_representer)
+ stub_const('::API::V3::WorkPackages::WorkPackageAggregationGroup', mock_aggregation_representer)
allow(::API::V3::UpdateQueryFromV3ParamsService)
.to receive(:new)
@@ -174,11 +169,15 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
.and_return(mock_update_query_service)
end
+ it 'is successful' do
+ expect(subject).to be_success
+ end
+
context 'result' do
subject { instance.call(params).result }
it 'is a WorkPackageCollectionRepresenter' do
- is_expected
+ expect(subject)
.to be_a(::API::V3::WorkPackages::WorkPackageCollectionRepresenter)
end