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

baseline api: pass `query` and `timestamps` to the `WorkPackageCollectionRepresenter`

Sebastian Fiedlschuster17db827554f1
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 f6e88fbfb65..f5a78ccb3b1 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
@@ -167,7 +167,9 @@ module API
groups:,
total_sums: sums,
embed_schemas: true,
- current_user:
+ current_user:,
+ timestamps: query.timestamps,
+ _query: query
)
end
end
diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb
index 46800f0d943..cd716cdd259 100644
--- a/lib/api/v3/work_packages/work_package_collection_representer.rb
+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb
@@ -30,6 +30,8 @@ module API
module V3
module WorkPackages
class WorkPackageCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection
+ attr_accessor :timestamps, :_query
+
def initialize(models,
self_link:,
groups:,
@@ -39,10 +41,14 @@ module API
project: nil,
page: nil,
per_page: nil,
- embed_schemas: false)
+ embed_schemas: false,
+ timestamps: [],
+ _query: nil)
@project = project
@total_sums = total_sums
@embed_schemas = embed_schemas
+ @timestamps = timestamps
+ @_query = _query
super(models,
self_link:,
diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
index 3275d63920e..76af1feab5e 100644
--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
@@ -45,6 +45,8 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
let(:default_page_size) { 30 }
let(:total) { 5 }
let(:embed_schemas) { false }
+ let(:timestamps) { nil }
+ let(:_query) { nil }
let(:representer) do
described_class.new(
@@ -57,7 +59,9 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
page: page_parameter,
per_page: page_size_parameter,
current_user: user,
- embed_schemas:
+ embed_schemas:,
+ timestamps:,
+ _query:
)
end
let(:collection_inner_type) { 'WorkPackage' }
@@ -469,4 +473,60 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
.at_path('_embedded/schemas/_embedded/elements/0/_links/self/href')
end
end
+
+ context 'when passing timestamps' do
+ let(:work_pacakges) { WorkPackage.where(id: work_package.id) }
+ let(:work_package) do
+ new_work_package = create(:work_package, subject: "The current work package")
+ new_work_package.update_columns created_at: baseline_time - 1.day
+ new_work_package
+ end
+ let(:original_journal) do
+ create_journal(journable: work_package, timestamp: baseline_time - 1.day,
+ version: 1,
+ attributes: { subject: "The original work package" })
+ end
+ let(:current_journal) do
+ create_journal(journable: work_package, timestamp: 1.day.ago,
+ version: 2,
+ attributes: { subject: "The current work package" })
+ end
+ let(:baseline_time) { "2022-01-01".to_time }
+
+ def create_journal(journable:, version:, timestamp:, attributes: {})
+ work_package_attributes = work_package.attributes.except("id")
+ journal_attributes = work_package_attributes \
+ .extract!(*Journal::WorkPackageJournal.attribute_names) \
+ .symbolize_keys.merge(attributes)
+ create(:work_package_journal, version:,
+ journable:, created_at: timestamp, updated_at: timestamp,
+ data: build(:journal_work_package_journal, journal_attributes))
+ end
+
+ before do
+ WorkPackage.destroy_all
+ work_package
+ Journal.destroy_all
+ original_journal
+ current_journal
+ end
+
+ shared_examples_for 'includes the properties of the current work package' do
+ it 'includes the properties of the current work package' do
+ expect(collection)
+ .to be_json_eql("The current work package".to_json)
+ .at_path('_embedded/elements/0/subject')
+ end
+ end
+
+ context 'current only' do
+ let(:timestamps) { [Timestamp.parse("PT0S")] }
+ it_behaves_like 'includes the properties of the current work package'
+ end
+
+ context 'empty' do
+ let(:timestamps) { [] }
+ it_behaves_like 'includes the properties of the current work package'
+ 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 2d00fa9c035..fff79363b41 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
@@ -84,7 +84,9 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
:page,
:per_page,
:embed_schemas,
- :current_user) do
+ :current_user,
+ :timestamps,
+ :_query) do
def initialize(work_packages,
self_link:,
query:,
@@ -94,7 +96,9 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
page:,
per_page:,
embed_schemas:,
- current_user:)
+ current_user:,
+ timestamps:,
+ _query:)
super(work_packages,
self_link,
query,
@@ -104,7 +108,9 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
page,
per_page,
embed_schemas,
- current_user)
+ current_user,
+ timestamps,
+ _query)
end
end
end
@@ -229,6 +235,25 @@ describe ::API::V3::WorkPackageCollectionFromQueryService,
end
end
+ context 'timestamps' do
+ let(:timestamps) { [Timestamp.parse("P-1Y"), Timestamp.now] }
+ let(:query) { build_stubbed(:query, timestamps: timestamps) }
+
+ it 'has the query timestamps' do
+ expect(subject.timestamps)
+ .to match_array(timestamps)
+ end
+ end
+
+ context '_query' do
+ # This is a workaround to allow the representer to access the query object itself.
+ # The attribute 'query' is already used by the representer and holds a Hash.
+ it 'has the query' do
+ expect(subject._query)
+ .to eq(query)
+ end
+ end
+
context 'total_sums' do
context 'with query.display_sums? being false' do
it 'is nil' do