diff --git a/app/utilities/protocol_importers/protocols_io/v3/api_client.rb b/app/utilities/protocol_importers/protocols_io/v3/api_client.rb index 4c739747f..eb7acb7b6 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/api_client.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/api_client.rb @@ -43,6 +43,7 @@ module ProtocolImporters # id of page. # Default is 1. def protocol_list(query_params = {}) + local_sorting = false response = with_handle_network_errors do sort_mappings = CONSTANTS[:sort_mappings] query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params) @@ -56,6 +57,7 @@ module ProtocolImporters # If key is blank access show latest publications, otherwise use # normal endpoint if query['key'].blank? + local_sorting = true query = CONSTANTS.dig(:endpoints, :publications, :default_query_params) self.class.get('/publications', query: query) else @@ -63,6 +65,10 @@ module ProtocolImporters end end 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 # Returns full representation of given protocol ID diff --git a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb index a7ab6695c..e73fdf0fc 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb @@ -75,6 +75,20 @@ module ProtocolImporters protocols_hash = client_data.parsed_response.with_indifferent_access[:items] 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[:protocols] = protocols_hash.map do |e| {