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

adding reason filter to notifications api

ulferts80718153d707
diff --git a/app/models/queries/notifications.rb b/app/models/queries/notifications.rb
index 82863fc2b02..9887653d423 100644
--- a/app/models/queries/notifications.rb
+++ b/app/models/queries/notifications.rb
@@ -32,6 +32,7 @@ module Queries::Notifications
[Queries::Notifications::Filters::ReadIanFilter,
Queries::Notifications::Filters::IdFilter,
Queries::Notifications::Filters::ProjectFilter,
+ Queries::Notifications::Filters::ReasonFilter,
Queries::Notifications::Filters::ResourceIdFilter,
Queries::Notifications::Filters::ResourceTypeFilter].each do |filter|
Queries::Register.filter Queries::Notifications::NotificationQuery,
diff --git a/app/models/queries/notifications/filters/reason_filter.rb b/app/models/queries/notifications/filters/reason_filter.rb
new file mode 100644
index 00000000000..8edb8da063b
--- /dev/null
+++ b/app/models/queries/notifications/filters/reason_filter.rb
@@ -0,0 +1,44 @@
+#-- 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::Filters::ReasonFilter < Queries::Notifications::Filters::NotificationFilter
+ def allowed_values
+ Notification.reason_ians.keys.map { |reason| [reason, reason] }
+ end
+
+ def type
+ :list
+ end
+
+ def where
+ id_values = values.map { |value| Notification.reason_ians[value] }
+ operator_strategy.sql_for_field(id_values, self.class.model.table_name, :reason_ian)
+ end
+end
diff --git a/spec/requests/api/v3/notifications/index_resource_spec.rb b/spec/requests/api/v3/notifications/index_resource_spec.rb
index 94bbd87004d..f2d55d342e7 100644
--- a/spec/requests/api/v3/notifications/index_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/index_resource_spec.rb
@@ -166,6 +166,61 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
end
+ context 'with a reason filter' do
+ let(:notification3) do
+ FactoryBot.create :notification,
+ reason_ian: :assigned,
+ recipient: recipient,
+ resource: work_package,
+ project: work_package.project
+ end
+ let(:notification4) do
+ FactoryBot.create :notification,
+ reason_ian: :responsible,
+ recipient: recipient,
+ resource: work_package,
+ project: work_package.project
+ end
+ let(:notifications) { [notification1, notification2, notification3, notification4] }
+
+ let(:filters) do
+ [
+ {
+ 'reason' => {
+ 'operator' => '=',
+ 'values' => [notification1.reason_ian.to_s, notification4.reason_ian.to_s]
+ }
+ }
+ ]
+ end
+
+ it_behaves_like 'API V3 collection response', 3, 3, 'Notification' do
+ let(:elements) { [notification4, notification2, notification1] }
+ end
+
+ context 'with an invalid reason' do
+ let(:filters) do
+ [
+ {
+ 'reason' => {
+ 'operator' => '=',
+ 'values' => ['bogus']
+ }
+ }
+ ]
+ end
+
+ it 'returns an error' do
+ expect(last_response.status)
+ .to be 400
+
+ expect(last_response.body)
+ .to be_json_eql("Filters Reason filter has invalid values.".to_json)
+ .at_path('message/0')
+ end
+ end
+ end
+
context 'with a reason groupBy' do
let(:responsible_notification) { FactoryBot.create :notification, recipient: recipient, reason_ian: :responsible }