diff --git a/app/models/concerns/media_object_intercom.rb b/app/models/concerns/media_object_intercom.rb index 8c7f205031..ea328406b2 100644 --- a/app/models/concerns/media_object_intercom.rb +++ b/app/models/concerns/media_object_intercom.rb @@ -49,8 +49,8 @@ def to_ingest_api_hash(include_structure = true, remove_identifiers: false) comment: comment.to_a, bibliographic_id: (bibliographic_id.present? ? bibliographic_id[:id] : nil), bibliographic_id_label: (bibliographic_id.present? ? bibliographic_id[:source] : nil), - note: (note.present? ? note.collect { |n| n[:note] } : nil), - note_type: (note.present? ? note.collect { |n| n[:type] } : nil), + note: (note.present? ? note.select { |n| n[:type] != 'intercom' }.collect { |n| n[:note] } : nil), + note_type: (note.present? ? note.select { |n| n[:type] != 'intercom' }.collect { |n| n[:type] } : nil), language: (language.present? ? language.collect { |n| n[:code] } : nil), related_item_url: (related_item_url.present? ? related_item_url.collect { |n| n[:url] } : nil), related_item_label: (related_item_url.present? ? related_item_url.collect { |n| n[:label] } : nil), diff --git a/app/models/media_object.rb b/app/models/media_object.rb index 4d0c912008..6c7feae867 100644 --- a/app/models/media_object.rb +++ b/app/models/media_object.rb @@ -220,6 +220,11 @@ def section_physical_descriptions all_pds.uniq end + # Returns true if the media object has been pushed from one avalon to another, i.e. it has some notes with type "intercom" + def intercom_pushed? + note.present? && note.any? { |n| n[:type] == 'intercom' } + end + def to_solr super.tap do |solr_doc| solr_doc[ActiveFedora.index_field_mapper.solr_name("workflow_published", :facetable, type: :string)] = published? ? 'Published' : 'Unpublished' @@ -242,6 +247,7 @@ def to_solr solr_doc['avalon_resource_type_ssim'] = self.avalon_resource_type.map(&:titleize) solr_doc['identifier_ssim'] = self.identifier.map(&:downcase) solr_doc['all_comments_sim'] = all_comments + solr_doc['intercom_pushed_bsi'] = intercom_pushed? #Add all searchable fields to the all_text_timv field all_text_values = [] all_text_values << solr_doc["title_tesi"] diff --git a/lib/avalon/intercom.rb b/lib/avalon/intercom.rb index 151b1b175e..3896bf4af7 100644 --- a/lib/avalon/intercom.rb +++ b/lib/avalon/intercom.rb @@ -45,7 +45,12 @@ def push_media_object(media_object, collection_id, include_structure = true) verify_ssl: false, timeout: 3600 ) - { link: URI.join(@avalon['url'], 'media_objects/', JSON.parse(resp.body)['id']).to_s } + result = { link: URI.join(@avalon['url'], 'media_objects/', JSON.parse(resp.body)['id']).to_s } + # add a note to indicate the item has been successfully pushed + pushnote = [{ note: "Pushed by #{@user} at #{Time.now.utc.strftime('%m/%d/%Y %H:%M%p %Z')}", type: 'intercom' }] + media_object.note = media_object.note.present? ? media_object.note + pushnote : pushnote + result[:message] = "The media object has been pushed to #{avalon}, but the note for this push failed to be saved." unless media_object.save + return result rescue StandardError => e { message: e.message, status: e.respond_to?(:response) && e.response.present? ? e.response.code : 500 } end diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index c913d5b137..c335f8a825 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -122,6 +122,22 @@ expect(assigns(:document_list).count).to eq 1 expect(assigns(:document_list).map(&:id)).to eq([mo.id]) end + it "should show results for all pushed items" do + mo = FactoryBot.create(:fully_searchable_media_object, note: [{note: 'Pushed by user at time', type: 'intercom'}]) + get 'index', :q => "intercom_pushed_bsi:true" + expect(response).to be_success + expect(response).to render_template('catalog/index') + expect(assigns(:document_list).count).to eq 1 + expect(assigns(:document_list).map(&:id)).to eq([mo.id]) + end + it "should show results for all non-pushed items" do + mo = FactoryBot.create(:fully_searchable_media_object) + get 'index', :q => "intercom_pushed_bsi:false" + expect(response).to be_success + expect(response).to render_template('catalog/index') + expect(assigns(:document_list).count).to eq 1 + expect(assigns(:document_list).map(&:id)).to eq([mo.id]) + end end describe "as an lti user" do let!(:user) { login_lti 'student' } diff --git a/spec/lib/avalon/intercom_spec.rb b/spec/lib/avalon/intercom_spec.rb index 7120ca0282..c489908011 100644 --- a/spec/lib/avalon/intercom_spec.rb +++ b/spec/lib/avalon/intercom_spec.rb @@ -79,15 +79,18 @@ response = Avalon::Intercom.new(username).push_media_object(media_object, 'cupcake_collection', false) expect(response[:message]).to eq('Avalon intercom target is not configured.') end - it "should respond to a failed api call with error" do + it "should respond to a failed api call with error, while the media object push status shall be false" do allow(RestClient::Request).to receive(:execute).and_call_original allow(RestClient::Request).to receive(:execute).with(hash_including(method: :post)).and_raise(StandardError) response = intercom.push_media_object(media_object, 'cupcake_collection', false) expect(response).to eq({ message: 'StandardError', status: 500 }) + expect(media_object.intercom_pushed?).eql?(false) end - it "should respond with a link to the pushed object on target" do + it "should respond with a link to the pushed object on target, while the pushed content shall not include any intercom notes, and the media object push status shall be true" do media_object.ordered_master_files=[master_file_with_structure] media_object_hash = media_object.to_ingest_api_hash(false) + expect(media_object_hash[:fields][:note]).eql?(nil) + expect(media_object_hash[:fields][:notetype]).eql?(nil) media_object_hash.merge!( { 'collection_id' => 'cupcake_collection', 'import_bib_record' => true, 'publish' => false } ) @@ -96,6 +99,7 @@ with(body: media_object_json).to_return(status: 200, body: { 'id' => 'def456' }.to_json, headers: {}) response = intercom.push_media_object(media_object, 'cupcake_collection', false) expect(response).to eq({ link: 'https://target.avalon.com/media_objects/def456'}) + expect(media_object.intercom_pushed?).eql?(true) end end end