Add local sorting for ProtocolsIO [SCI-3827]

This commit is contained in:
Oleksii Kriuchykhin 2019-09-05 14:49:16 +02:00
parent 15dc25cee9
commit 6f7ef25514
2 changed files with 20 additions and 0 deletions

View file

@ -43,6 +43,7 @@ module ProtocolImporters
# id of page. # id of page.
# Default is 1. # Default is 1.
def protocol_list(query_params = {}) def protocol_list(query_params = {})
local_sorting = false
response = with_handle_network_errors do response = with_handle_network_errors do
sort_mappings = CONSTANTS[:sort_mappings] sort_mappings = CONSTANTS[:sort_mappings]
query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params) query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
@ -56,6 +57,7 @@ module ProtocolImporters
# If key is blank access show latest publications, otherwise use # If key is blank access show latest publications, otherwise use
# normal endpoint # normal endpoint
if query['key'].blank? if query['key'].blank?
local_sorting = true
query = CONSTANTS.dig(:endpoints, :publications, :default_query_params) query = CONSTANTS.dig(:endpoints, :publications, :default_query_params)
self.class.get('/publications', query: query) self.class.get('/publications', query: query)
else else
@ -63,6 +65,10 @@ module ProtocolImporters
end end
end end
check_for_response_errors(response) check_for_response_errors(response)
if local_sorting && %w(alpha_asc alpha_desc newest oldest).include?(query_params[:sort_by])
response.parsed_response[:local_sorting] = query_params[:sort_by]
end
response
end end
# Returns full representation of given protocol ID # Returns full representation of given protocol ID

View file

@ -75,6 +75,20 @@ module ProtocolImporters
protocols_hash = client_data.parsed_response.with_indifferent_access[:items] protocols_hash = client_data.parsed_response.with_indifferent_access[:items]
pagination = client_data.parsed_response.with_indifferent_access[:pagination] pagination = client_data.parsed_response.with_indifferent_access[:pagination]
if client_data.parsed_response[:local_sorting]
protocols_hash =
case client_data.parsed_response[:local_sorting]
when 'alpha_asc'
protocols_hash.sort_by { |p| p[:title] }
when 'alpha_desc'
protocols_hash.sort_by { |p| p[:title] }.reverse
when 'oldest'
protocols_hash.sort_by { |p| p[:created_on] }
else
protocols_hash.sort_by { |p| p[:created_on] }.reverse
end
end
normalized_data = {} normalized_data = {}
normalized_data[:protocols] = protocols_hash.map do |e| normalized_data[:protocols] = protocols_hash.map do |e|
{ {