From 40ad9fbf00897d31097861848d058143b12d2a3c Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Wed, 27 May 2020 12:10:48 +0200 Subject: [PATCH 1/2] Check duplicate protocol name during import --- app/services/team_importer.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/services/team_importer.rb b/app/services/team_importer.rb index c5572fdcb..d239c1d97 100644 --- a/app/services/team_importer.rb +++ b/app/services/team_importer.rb @@ -696,6 +696,26 @@ class TeamImporter protocols_json.each do |protocol_json| protocol = Protocol.new(protocol_json['protocol']) orig_protocol_id = protocol.id + if protocol.name + protocol_name_unique = false + until protocol_name_unique + protocol_exist = if protocol.protocol_type == :in_repository_public + Protocol.where(protocol_type: protocol.protocol_type) + .where(team: team) + .find_by(name: protocol.name) + else + Protocol.where(protocol_type: protocol.protocol_type) + .where(team: team) + .where(added_by_id: find_user(protocol.added_by_id)) + .find_by(name: protocol.name) + end + if protocol_exist + protocol.name = protocol.name + '(1)' + else + protocol_name_unique = true + end + end + end protocol.id = nil protocol.added_by_id = find_user(protocol.added_by_id) protocol.team = team || my_module.experiment.project.team From 8f998ceee9c0ceccaed1e47a80aab21d70af3f1a Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Wed, 27 May 2020 16:11:10 +0200 Subject: [PATCH 2/2] Fix counter --- app/services/team_importer.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/services/team_importer.rb b/app/services/team_importer.rb index d239c1d97..da4e96910 100644 --- a/app/services/team_importer.rb +++ b/app/services/team_importer.rb @@ -698,7 +698,10 @@ class TeamImporter orig_protocol_id = protocol.id if protocol.name protocol_name_unique = false + original_name = protocol.name + counter = 0 until protocol_name_unique + counter += 1 protocol_exist = if protocol.protocol_type == :in_repository_public Protocol.where(protocol_type: protocol.protocol_type) .where(team: team) @@ -710,7 +713,7 @@ class TeamImporter .find_by(name: protocol.name) end if protocol_exist - protocol.name = protocol.name + '(1)' + protocol.name = original_name + "(#{counter})" else protocol_name_unique = true end