From 4715e8ec4bbef9f0837b65f3c7db1385f5d6fdc7 Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Wed, 3 Jul 2019 09:34:15 +0200 Subject: [PATCH] Fix building of zero steps protocls Close SCI-3648 --- .../build_protocol_from_client_service.rb | 16 ++-------- .../search_protocols_service.rb | 14 ++------- .../protocols_io/v3/protocol_normalizer.rb | 30 +++++++++++-------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/services/protocol_importers/build_protocol_from_client_service.rb b/app/services/protocol_importers/build_protocol_from_client_service.rb index fdc863533..4de8c0946 100644 --- a/app/services/protocol_importers/build_protocol_from_client_service.rb +++ b/app/services/protocol_importers/build_protocol_from_client_service.rb @@ -2,8 +2,8 @@ module ProtocolImporters class BuildProtocolFromClientService - extend Service require 'protocol_importers/protocols_io/v3/errors' + extend Service attr_reader :errors, :built_protocol, :steps_assets @@ -35,15 +35,9 @@ module ProtocolImporters @steps_assets = pio.steps_assets unless @build_with_assets self - rescue api_errors => e + rescue client_errors => e @errors[e.error_type] = e.message self - rescue normalizer_errors => e - @errors[e.error_type] = e.message - self - rescue StandardError => e - @errors[:build_protocol] = e.message - self end def succeed? @@ -99,12 +93,8 @@ module ProtocolImporters "ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new end - def api_errors + def client_errors "ProtocolImporters::#{endpoint_name}::Error".constantize end - - def normalizer_errors - "ProtocolImporters::#{endpoint_name}::NormalizerError".constantize - end end end diff --git a/app/services/protocol_importers/search_protocols_service.rb b/app/services/protocol_importers/search_protocols_service.rb index fdd61e5c5..6a0c25be3 100644 --- a/app/services/protocol_importers/search_protocols_service.rb +++ b/app/services/protocol_importers/search_protocols_service.rb @@ -25,15 +25,9 @@ module ProtocolImporters @protocols_list = normalizer.normalize_list(api_response) self - rescue api_errors => e + rescue client_errors => e @errors[e.error_type] = e.message self - rescue normalizer_errors => e - @errors[e.error_type] = e.message - self - rescue StandardError => e - @errors[:build_protocol] = e.message - self end def succeed? @@ -69,12 +63,8 @@ module ProtocolImporters "ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new end - def api_errors + def client_errors "ProtocolImporters::#{endpoint_name}::Error".constantize end - - def normalizer_errors - "ProtocolImporters::#{endpoint_name}::NormalizerError".constantize - end end end 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 decb3a068..e38adc8d5 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb @@ -44,21 +44,25 @@ module ProtocolImporters end # set positions - first_step_id = normalized_data[:steps].find { |s| s[:position].zero? }[:source_id] - next_step_id = protocol_hash[:steps].find { |s| s[:previous_id] == first_step_id }.try(:[], :id) - steps = normalized_data[:steps].map { |s| [s[:source_id], s] }.to_h - original_order = protocol_hash[:steps].map { |m| [m[:previous_id], m[:id]] }.to_h - current_position = 0 - while next_step_id + if protocol_hash[:steps].any? + first_step_id = normalized_data[:steps].find { |s| s[:position].zero? }[:source_id] + next_step_id = protocol_hash[:steps].find { |s| s[:previous_id] == first_step_id }.try(:[], :id) + steps = normalized_data[:steps].map { |s| [s[:source_id], s] }.to_h + original_order = protocol_hash[:steps].map { |m| [m[:previous_id], m[:id]] }.to_h - current_position += 1 - steps[next_step_id][:position] = current_position - next_step_id = original_order[next_step_id] - end + current_position = 0 + while next_step_id + current_position += 1 + steps[next_step_id][:position] = current_position + next_step_id = original_order[next_step_id] + end - # Check if step name are valid - steps.each do |step| - step[1][:name] = "Step #{(step[1][:position] + 1)}" if step[1][:name].blank? + # Check if step name are valid + steps.each do |step| + step[1][:name] = "Step #{(step[1][:position] + 1)}" if step[1][:name].blank? + end + else + normalized_data[:steps] = [] end { protocol: normalized_data }