Skip to content

Commit

Permalink
Merge pull request #727 from learningtapestry/fetch-envelope-by-resou…
Browse files Browse the repository at this point in the history
…rce-id

Allow to fetch envelopes by their resources' IDs
  • Loading branch information
excelsior authored Oct 1, 2024
2 parents ef61bef + b08c615 commit 923b741
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/api/v1/envelopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def find_community_envelopes
id = params[:envelope_id]&.downcase

@envelope = find_envelopes.find_by(envelope_id: id) ||
find_envelopes.where(envelope_ceterms_ctid: id).last
find_envelopes.where(envelope_ceterms_ctid: id).last ||
find_envelopes
.joins(:envelope_resources)
.where(envelope_resources: { resource_id: id })
.last

if @envelope.nil?
error!({ errors: ['Couldn\'t find Envelope'] }, 404)
Expand Down
38 changes: 38 additions & 0 deletions spec/api/v1/single_envelope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@
)
end
end

context 'by resource ID' do
let(:resource) { create(:envelope_resource, envelope: subject) }
let(:resource_id) { resource.resource_id }

include_examples 'missing envelope', :get

subject { envelopes.first }

before(:each) do
with_versioned_envelope(subject) do
get "/learning-registry/envelopes/#{resource_id}"
end
end

it { expect_status(:ok) }

it 'retrieves the desired envelope' do
expect_json(envelope_community: subject.envelope_community.name)
expect_json(envelope_id: subject.envelope_id)
expect_json(resource_format: 'json')
expect_json(resource_encoding: 'jwt')
end

it 'displays the appended node headers' do
base_url = "/learning-registry/envelopes/#{subject.envelope_id}"

expect_json_keys('node_headers', %i[resource_digest revision_history
created_at updated_at deleted_at])
expect_json('node_headers.revision_history.1', head: true)
expect_json('node_headers.revision_history.1', url: base_url)
expect_json('node_headers.revision_history.0', head: false)
expect_json(
'node_headers.revision_history.0',
url: "#{base_url}/revisions/#{subject.versions.last.id}"
)
end
end
end

context 'PATCH /:community/envelopes/:id' do
Expand Down

0 comments on commit 923b741

Please sign in to comment.