mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-06 06:56:54 +08:00
SCI-3648 Error at importing one protocol without steps (#1883)
SCI-3648 Error at importing one protocol without steps
This commit is contained in:
commit
e965866d57
3 changed files with 22 additions and 38 deletions
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
module ProtocolImporters
|
module ProtocolImporters
|
||||||
class BuildProtocolFromClientService
|
class BuildProtocolFromClientService
|
||||||
extend Service
|
|
||||||
require 'protocol_importers/protocols_io/v3/errors'
|
require 'protocol_importers/protocols_io/v3/errors'
|
||||||
|
extend Service
|
||||||
|
|
||||||
attr_reader :errors, :built_protocol, :steps_assets
|
attr_reader :errors, :built_protocol, :steps_assets
|
||||||
|
|
||||||
|
@ -35,15 +35,9 @@ module ProtocolImporters
|
||||||
@steps_assets = pio.steps_assets unless @build_with_assets
|
@steps_assets = pio.steps_assets unless @build_with_assets
|
||||||
|
|
||||||
self
|
self
|
||||||
rescue api_errors => e
|
rescue client_errors => e
|
||||||
@errors[e.error_type] = e.message
|
@errors[e.error_type] = e.message
|
||||||
self
|
self
|
||||||
rescue normalizer_errors => e
|
|
||||||
@errors[e.error_type] = e.message
|
|
||||||
self
|
|
||||||
rescue StandardError => e
|
|
||||||
@errors[:build_protocol] = e.message
|
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def succeed?
|
def succeed?
|
||||||
|
@ -99,12 +93,8 @@ module ProtocolImporters
|
||||||
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
|
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def api_errors
|
def client_errors
|
||||||
"ProtocolImporters::#{endpoint_name}::Error".constantize
|
"ProtocolImporters::#{endpoint_name}::Error".constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalizer_errors
|
|
||||||
"ProtocolImporters::#{endpoint_name}::NormalizerError".constantize
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,15 +25,9 @@ module ProtocolImporters
|
||||||
@protocols_list = normalizer.normalize_list(api_response)
|
@protocols_list = normalizer.normalize_list(api_response)
|
||||||
|
|
||||||
self
|
self
|
||||||
rescue api_errors => e
|
rescue client_errors => e
|
||||||
@errors[e.error_type] = e.message
|
@errors[e.error_type] = e.message
|
||||||
self
|
self
|
||||||
rescue normalizer_errors => e
|
|
||||||
@errors[e.error_type] = e.message
|
|
||||||
self
|
|
||||||
rescue StandardError => e
|
|
||||||
@errors[:build_protocol] = e.message
|
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def succeed?
|
def succeed?
|
||||||
|
@ -69,12 +63,8 @@ module ProtocolImporters
|
||||||
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
|
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def api_errors
|
def client_errors
|
||||||
"ProtocolImporters::#{endpoint_name}::Error".constantize
|
"ProtocolImporters::#{endpoint_name}::Error".constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalizer_errors
|
|
||||||
"ProtocolImporters::#{endpoint_name}::NormalizerError".constantize
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,21 +44,25 @@ module ProtocolImporters
|
||||||
end
|
end
|
||||||
|
|
||||||
# set positions
|
# set positions
|
||||||
first_step_id = normalized_data[:steps].find { |s| s[:position].zero? }[:source_id]
|
if protocol_hash[:steps].any?
|
||||||
next_step_id = protocol_hash[:steps].find { |s| s[:previous_id] == first_step_id }.try(:[], :id)
|
first_step_id = normalized_data[:steps].find { |s| s[:position].zero? }[:source_id]
|
||||||
steps = normalized_data[:steps].map { |s| [s[:source_id], s] }.to_h
|
next_step_id = protocol_hash[:steps].find { |s| s[:previous_id] == first_step_id }.try(:[], :id)
|
||||||
original_order = protocol_hash[:steps].map { |m| [m[:previous_id], m[:id]] }.to_h
|
steps = normalized_data[:steps].map { |s| [s[:source_id], s] }.to_h
|
||||||
current_position = 0
|
original_order = protocol_hash[:steps].map { |m| [m[:previous_id], m[:id]] }.to_h
|
||||||
while next_step_id
|
|
||||||
|
|
||||||
current_position += 1
|
current_position = 0
|
||||||
steps[next_step_id][:position] = current_position
|
while next_step_id
|
||||||
next_step_id = original_order[next_step_id]
|
current_position += 1
|
||||||
end
|
steps[next_step_id][:position] = current_position
|
||||||
|
next_step_id = original_order[next_step_id]
|
||||||
|
end
|
||||||
|
|
||||||
# Check if step name are valid
|
# Check if step name are valid
|
||||||
steps.each do |step|
|
steps.each do |step|
|
||||||
step[1][:name] = "Step #{(step[1][:position] + 1)}" if step[1][:name].blank?
|
step[1][:name] = "Step #{(step[1][:position] + 1)}" if step[1][:name].blank?
|
||||||
|
end
|
||||||
|
else
|
||||||
|
normalized_data[:steps] = []
|
||||||
end
|
end
|
||||||
|
|
||||||
{ protocol: normalized_data }
|
{ protocol: normalized_data }
|
||||||
|
|
Loading…
Reference in a new issue