Skip to content

Commit

Permalink
Merge pull request #4906 from sul-dlss/t4903-return-version-data
Browse files Browse the repository at this point in the history
Return version content from RepositoryObject if it exists
  • Loading branch information
aaron-collier authored Apr 19, 2024
2 parents aa62a8b + c846b31 commit a428cab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
17 changes: 16 additions & 1 deletion app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ class VersionsController < ApplicationController
before_action :load_version, only: %i[current close_current openable status]

def index
repository_object = RepositoryObject.find_by(external_identifier: params[:object_id])
object_versions = ObjectVersion.where(druid: params[:object_id])

return render json: {} if object_versions.empty?
# return an empty JSON object if there are no versions and no repository object
return render json: {} if object_versions.empty? && repository_object.nil?

# add an entry with version id and description for each RepositoryObjectVersion

return render json: { versions: repository_object_version_content(repository_object.versions) } if repository_object

# add an entry with version id and description for each version
versions = object_versions.map do |object_version|
Expand Down Expand Up @@ -109,4 +115,13 @@ def boolean_param(params_hash, key)
def load_version
@version = CocinaObjectStore.version(params[:object_id])
end

def repository_object_version_content(repository_object_versions)
repository_object_versions.map do |repository_object_version|
{
versionId: repository_object_version.version,
message: repository_object_version.version_description
}
end
end
end
52 changes: 40 additions & 12 deletions spec/requests/versions_inventory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,49 @@
RSpec.describe 'Release tags' do
let(:druid) { 'druid:mx123qw2323' }

before do
allow(CocinaObjectStore).to receive(:exists!).with(druid)
context 'with an ObjectVersion' do
before do
allow(CocinaObjectStore).to receive(:exists!).with(druid)

ObjectVersion.create(druid:, version: 1, description: 'Initial Version')
ObjectVersion.create(druid:, version: 2, description: 'pre-assembly re-accession')
ObjectVersion.create(druid:, version: 3, description: 'pre-assembly re-accession')
ObjectVersion.create(druid:, version: 1, description: 'Initial Version')
ObjectVersion.create(druid:, version: 2, description: 'pre-assembly re-accession')
ObjectVersion.create(druid:, version: 3, description: 'pre-assembly re-accession')
end

it 'returns a 200' do
get "/v1/objects/#{druid}/versions",
headers: { 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:ok)
expect(response.body).to eq '{"versions":[{"versionId":1,"message":"Initial Version"},' \
'{"versionId":2,"message":"pre-assembly re-accession"},' \
'{"versionId":3,"message":"pre-assembly re-accession"}]}'
end
end

it 'returns a 200' do
get "/v1/objects/#{druid}/versions",
headers: { 'Authorization' => "Bearer #{jwt}" }
context 'with a RepositoryObject' do
let(:repository_object) { build(:repository_object, **attrs) }

let(:attrs) do
{
external_identifier: druid,
object_type: 'dro',
source_id: 'sul:dlss:testing'
}
end

before do
repository_object.save # we need at least one persisted version so we can run this test
repository_object.versions.create!(version: 2, version_description: 'draft')
end

it 'returns a 200' do
get "/v1/objects/#{druid}/versions",
headers: { 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:ok)
expect(response.body).to eq '{"versions":[{"versionId":1,"message":"Initial Version"},' \
'{"versionId":2,"message":"pre-assembly re-accession"},' \
'{"versionId":3,"message":"pre-assembly re-accession"}]}'
expect(response).to have_http_status(:ok)
expect(response.body).to eq '{"versions":[{"versionId":1,"message":"Initial version"},' \
'{"versionId":2,"message":"draft"}]}'
end
end
end

0 comments on commit a428cab

Please sign in to comment.