From 1b85f026f9ab8be80c1ea6b753fec857bfecad91 Mon Sep 17 00:00:00 2001 From: Bilel KIHAL Date: Thu, 18 Jul 2024 15:22:46 +0200 Subject: [PATCH 1/2] perform serach on agents using name, acronym and identifiers --- controllers/agents_controller.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/controllers/agents_controller.rb b/controllers/agents_controller.rb index 1bf86321..07d7a106 100644 --- a/controllers/agents_controller.rb +++ b/controllers/agents_controller.rb @@ -15,6 +15,14 @@ class AgentsController < ApplicationController agents = query.to_a end + if params[:query] + agents = agents.select do |agent| + params[:query].downcase.include?(agent.name&.downcase) || + params[:query].downcase.include?(agent.acronym&.downcase) || + agent.identifiers.any? { |identifier| params[:query].include?(identifier.notation) } + end + end + if includes_param.include?(:all) || includes_param.include?(:usages) LinkedData::Models::Agent.load_agents_usages(agents) end @@ -147,4 +155,4 @@ def update_agent(agent, params) end end -end \ No newline at end of file +end From e2ce607a91a2b1c86f9a0857d2ba4ba5a7e25d30 Mon Sep 17 00:00:00 2001 From: Bilel KIHAL Date: Thu, 18 Jul 2024 15:23:07 +0200 Subject: [PATCH 2/2] test serach agents using name, acronym and identifiers --- test/controllers/test_agents_controller.rb | 48 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/controllers/test_agents_controller.rb b/test/controllers/test_agents_controller.rb index 658ef38b..614e0963 100644 --- a/test/controllers/test_agents_controller.rb +++ b/test/controllers/test_agents_controller.rb @@ -45,6 +45,30 @@ def test_all_agents end end + def test_search_agents + agent_1 = _create_agent(name: 'testagent1234', type: 'person', acronym: 'ta1234', identifier_type: 'ORCID', identifier_notation: '123456') + agent_2 = _create_agent(name: 'testagent5678', type: 'organization', acronym: 'ta5678', identifier_type: 'ROR', identifier_notation: '78910') + # Search agent by name: testagent1234 + get '/agents?query=testagent1234' + response = MultiJson.load(last_response.body) + assert_equal agent_1.name, response.first["name"] + + # Search agent by acronym: ta1234 + get '/agents?query=ta1234' + response = MultiJson.load(last_response.body) + assert_equal agent_1.acronym, response.first["acronym"] + + # Search agent by orcid: 123456 + get '/agents?query=123456' + response = MultiJson.load(last_response.body) + assert_equal agent_1.identifiers.first.notation, response.first["identifiers"].first["notation"] + + # Search agent by ROR: 78910 + get '/agents?query=78910' + response = MultiJson.load(last_response.body) + assert_equal agent_2.identifiers.first.notation, response.first["identifiers"].first["notation"] + end + def test_single_agent @agents.each do |agent| agent_obj = _find_agent(agent['name']) @@ -207,4 +231,26 @@ def _test_agent_creation(agent) assert_equal agent[:affiliations].map { |x| x["name"] }.sort, created_agent["affiliations"].map { |x| x['name'] }.sort created_agent end -end \ No newline at end of file + + def _create_agent(name: 'name', type: 'person', acronym: 'acronym', identifier_type: 'ORCID', identifier_notation: '123456') + agent = LinkedData::Models::Agent.new({ + agentType: type, + name: name, + creator: User.find('tim').first, + acronym: acronym, + identifiers: [_create_identifier(notation: identifier_notation, schemaAgency: identifier_type)] + }) + agent.save + agent + end + + def _create_identifier(notation: '123456', schemaAgency: 'ORCID') + identifier = LinkedData::Models::AgentIdentifier.new({ + notation: notation, + schemaAgency: schemaAgency, + creator: User.find('tim').first, + }) + identifier.save + identifier + end +end