Fix building of circular protocols for protocols.io [SCI-7388] (#4665)

This commit is contained in:
ajugo 2022-12-05 10:21:12 +01:00 committed by GitHub
parent 6f60a7cd4f
commit 369fffc467
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,16 +43,28 @@ module ProtocolImporters
# set positions
if protocol_hash[:steps].any?
first_step_id = normalized_data[:steps].find { |s| s[:position].zero? }[:source_id]
first_step = normalized_data[:steps].find { |s| s[:position].present? && s[:position].zero? }
first_step_id = if first_step.present?
first_step[:source_id]
else
protocol_hash[:steps].min_by do |el|
el[:id]
end[:id]
end
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
steps[first_step_id][:position] = current_position
while next_step_id
current_position += 1
steps[next_step_id][:position] = current_position
next_step_id = original_order[next_step_id]
next_step_id = if original_order[next_step_id] == first_step_id
nil
else
original_order[next_step_id]
end
end
# Check if step name are valid