From b08c61512cfd019e0087a0f1df13d9b0ed51eaf8 Mon Sep 17 00:00:00 2001 From: Alex Nizamov Date: Tue, 1 Oct 2024 20:13:26 +0500 Subject: [PATCH] Allow to fetch envelopes by their resources' IDs --- app/api/v1/envelopes.rb | 6 ++++- spec/api/v1/single_envelope_spec.rb | 38 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/api/v1/envelopes.rb b/app/api/v1/envelopes.rb index 9e662312..764c2559 100644 --- a/app/api/v1/envelopes.rb +++ b/app/api/v1/envelopes.rb @@ -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) diff --git a/spec/api/v1/single_envelope_spec.rb b/spec/api/v1/single_envelope_spec.rb index 4004dd64..c5aa4e45 100644 --- a/spec/api/v1/single_envelope_spec.rb +++ b/spec/api/v1/single_envelope_spec.rb @@ -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