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

Extend queries by projections

Wieland Lindenthald4336f5ad6fd
diff --git a/app/models/queries/queries.rb b/app/models/queries/queries.rb
index 20e34d96c6f..ec878751106 100644
--- a/app/models/queries/queries.rb
+++ b/app/models/queries/queries.rb
@@ -42,4 +42,5 @@ module Queries::Queries
register.filter query_ns, filters_ns::HiddenFilter
register.filter query_ns, filters_ns::UpdatedAtFilter
register.filter query_ns, filters_ns::IdFilter
+ register.filter query_ns, filters_ns::ModuleIdentifierFilter
end
diff --git a/app/models/queries/queries/filters/module_identifier_filter.rb b/app/models/queries/queries/filters/module_identifier_filter.rb
new file mode 100644
index 00000000000..45e0ebe8fc3
--- /dev/null
+++ b/app/models/queries/queries/filters/module_identifier_filter.rb
@@ -0,0 +1,47 @@
+#-- 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::Queries::Filters::ModuleIdentifierFilter < Queries::Queries::Filters::QueryFilter
+ def type
+ :list_optional
+ end
+
+ def self.key
+ :module_identifier
+ end
+
+ def where
+ operator_strategy.sql_for_field(values, 'queries', 'projections')
+ end
+
+ def allowed_values
+ [['Work packages', 'work_packages'], ['Calendar', 'calendar'], ['BIM', 'bim']]
+ end
+end
diff --git a/db/migrate/20211116155545_add_column_projections_to_queries.rb b/db/migrate/20211116155545_add_column_projections_to_queries.rb
new file mode 100644
index 00000000000..b5f58639476
--- /dev/null
+++ b/db/migrate/20211116155545_add_column_projections_to_queries.rb
@@ -0,0 +1,5 @@
+class AddColumnProjectionsToQueries < ActiveRecord::Migration[6.1]
+ def change
+ add_column :queries, :projections, :jsonb, default: {}
+ end
+end
diff --git a/lib/api/v3/queries/query_helper.rb b/lib/api/v3/queries/query_helper.rb
index fbc62a24594..b38031d4483 100644
--- a/lib/api/v3/queries/query_helper.rb
+++ b/lib/api/v3/queries/query_helper.rb
@@ -67,7 +67,6 @@ module API
rep = representer.new Query.new, current_user: current_user
query = rep.from_hash request_body
call = ::Queries::CreateService.new(user: current_user).call query
-
if call.success?
representer.new call.result, current_user: current_user, embed_links: true
else
diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rb
index bdd1db69cab..805d0ac5b65 100644
--- a/lib/api/v3/queries/query_representer.rb
+++ b/lib/api/v3/queries/query_representer.rb
@@ -307,6 +307,10 @@ module API
property :highlighting_mode,
render_nil: false
+ # Projections
+ property :projections,
+ writeable: true
+
attr_accessor :results,
:params
@@ -327,7 +331,7 @@ module API
{ project: :work_package_custom_fields }]
def _type
- 'Query'
+ represented.class.name
end
def filters
diff --git a/spec/models/queries/queries/filters/module_identifier_filter_spec.rb b/spec/models/queries/queries/filters/module_identifier_filter_spec.rb
new file mode 100644
index 00000000000..046d7fbbeeb
--- /dev/null
+++ b/spec/models/queries/queries/filters/module_identifier_filter_spec.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.
+#++
+
+require 'spec_helper'
+
+describe Queries::Queries::Filters::ModuleIdentifierFilter, type: :model do
+ it_behaves_like 'basic query filter' do
+ let(:class_key) { :module_identifier }
+ let(:type) { :list_optional }
+ end
+
+ it_behaves_like 'list_optional query filter' do
+ let(:attribute) { :module_identifier }
+ let(:model) { Query }
+ let(:valid_values) { ['calendar'] }
+ end
+end
diff --git a/spec/requests/api/v3/queries/create_query_spec.rb b/spec/requests/api/v3/queries/create_query_spec.rb
index d29a0d8d218..d5bf5aec4ce 100644
--- a/spec/requests/api/v3/queries/create_query_spec.rb
+++ b/spec/requests/api/v3/queries/create_query_spec.rb
@@ -33,7 +33,7 @@ describe "POST /api/v3/queries", type: :request do
let(:status) { FactoryBot.create :status }
let(:project) { FactoryBot.create :project }
- let(:params) do
+ let(:default_params) do
{
name: "Dummy Query",
showHierarchies: false,
@@ -90,6 +90,7 @@ describe "POST /api/v3/queries", type: :request do
}
}
end
+ let(:params) { default_params }
before do
login_as user
@@ -129,6 +130,27 @@ describe "POST /api/v3/queries", type: :request do
expect(filter.operator).to eq "="
expect(filter.values).to eq [status.id.to_s]
end
+
+ context 'with a module specific projection' do
+ let(:params) do
+ default_params.merge(
+ projections: [
+ {
+ _type: "calendar",
+ options: {}
+ }
+ ]
+ )
+ end
+
+ it 'creates the Query successfully' do
+ query = Query.find_by(name: params[:name])
+ json = JSON.parse(last_response.body)
+
+ expect(query).to be_present
+ expect(json["projections"][0]["_type"]).to eq "calendar"
+ end
+ end
end
context "with invalid parameters" do
diff --git a/spec/requests/api/v3/queries/query_resource_spec.rb b/spec/requests/api/v3/queries/query_resource_spec.rb
index 1c5d4de7519..840723c8572 100644
--- a/spec/requests/api/v3/queries/query_resource_spec.rb
+++ b/spec/requests/api/v3/queries/query_resource_spec.rb
@@ -204,6 +204,32 @@ describe 'API v3 Query resource', type: :request, content_type: :json do
.at_path("_embedded/elements/0/name")
end
end
+
+ context 'with filters for module' do
+ let(:module_identifier) { "calendar" }
+ let(:path) do
+ filter = [module_identifier: { operator: "=", values: [module_identifier] }]
+
+ api_v3_paths.path_for(:queries, filters: filter)
+ end
+ let(:query) { FactoryBot.create(:public_query, project: project, projections: [{ _type: "calendar" }]) }
+
+ let(:prepare) do
+ query
+ end
+
+ it 'includes only queries with projections for the specified module' do
+ expect(last_response.body)
+ .to be_json_eql(1)
+ .at_path("count")
+ expect(last_response.body)
+ .to be_json_eql(1)
+ .at_path("total")
+ expect(last_response.body)
+ .to be_json_eql(query.name.to_json)
+ .at_path("_embedded/elements/0/name")
+ end
+ end
end
describe '#get queries/:id' do