Skip to content

Commit

Permalink
Merge pull request #2297 from sparc-request/jl-protocol-merge-ssr-ord…
Browse files Browse the repository at this point in the history
…ering

Jl - Protocol merge ssr ordering and new validations
  • Loading branch information
Stuart-Johnson committed Apr 17, 2020
2 parents 3802672 + 75aade2 commit 4441747
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
15 changes: 14 additions & 1 deletion app/controllers/dashboard/protocol_merges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ def perform_protocol_merge
@errors[:merged_protocol_id] = t(:dashboard)[:protocol_merge][:errors][:one_calendar]
elsif Setting.get_value("fulfillment_contingent_on_catalog_manager") && @merged_protocol.fulfillment_protocols.any?
@errors[:merged_protocol_id] = t(:dashboard)[:protocol_merge][:errors][:cannot_merge]
elsif requests_not_merge_eligible?(@master_protocol, @merged_protocol)
@errors[:master_protocol_id] = t(:dashboard)[:protocol_merge][:errors][:split_notify_error]
elsif @errors.empty? && !confirmed
@no_errors = true
return
else
ActiveRecord::Base.transaction do
merged_ssr_ids = @merged_protocol.sub_service_requests.map(&:id)
merge_srs = Dashboard::MergeSrs.new()
fix_ssr_ids = Dashboard::FixSsrIds.new(@master_protocol)
fix_ssr_ids = Dashboard::FixSsrIds.new(@master_protocol, merged_ssr_ids)
#transfer the project roles as needed
@merged_protocol.project_roles.each do |role|
if role.role != 'primary-pi' && role_should_be_assigned?(role, @master_protocol)
Expand Down Expand Up @@ -159,4 +162,14 @@ def role_should_be_assigned?(role_to_be_assigned, protocol)
def has_research?(protocol, research_type)
protocol.research_types_info.try(research_type) || false
end

# We can not merge if 2 requests are under the same process ssrs org AND either of those requests are locked or incomplete
# Grab all locked / incomplete requests from each protocol
# Take the intersection of the potentially ineligible requests to see if any of them have the same "organization_id"
def requests_not_merge_eligible?(master, merged)
master_ineligible_requests = master.sub_service_requests.select{ |ssr| ssr.is_locked? || !ssr.is_complete?}
merged_ineligible_requests = merged.sub_service_requests.select{ |ssr| ssr.is_locked? || !ssr.is_complete?}

return (master_ineligible_requests.map(&:organization_id) & merged_ineligible_requests.map(&:organization_id)).any?
end
end
40 changes: 12 additions & 28 deletions app/lib/dashboard/fix_ssr_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,24 @@ module Dashboard

class FixSsrIds

def initialize(protocol)
def initialize(protocol, merged_ssr_ids)
@protocol = protocol
@next_ssr_id = protocol.next_ssr_id
@merged_ssr_ids = merged_ssr_ids
end

def perform_id_fix
requests = @protocol.sub_service_requests.group_by(&:ssr_id)
if @protocol.sub_service_requests.present?
last_ssr_id = @protocol.sub_service_requests.sort_by(&:ssr_id).last.ssr_id.to_i
dup_requests_to_be_incremented = []

requests.each do |ssr_id, ssr_array|
if ssr_array.size > 1 # we have duplicate ssr_ids
ssr_array.each_with_index do |ssr, index|
if index > 0
dup_requests_to_be_incremented << ssr # place all requests with dup ids in an array to deal with later
end
end
end
end

if dup_requests_to_be_incremented.size > 0
dup_requests_to_be_incremented.each do |ssr|
ssr.ssr_id = "%04d" % (last_ssr_id + 1)
ssr.save(validate: false)
last_ssr_id += 1
end
id = @next_ssr_id
if @merged_ssr_ids.count != 0
@merged_ssr_ids.each do |ssr_id|
ssr = SubServiceRequest.find(ssr_id)
ssr.ssr_id = "%04d" % id
id += 1
ssr.save(validate: false)
end

# we need to increment the protocol's next_ssr_id if we had some duplicates
new_last_ssr_id = @protocol.sub_service_requests.sort_by(&:ssr_id).last.ssr_id.to_i
if @protocol.next_ssr_id? && (@protocol.next_ssr_id <= new_last_ssr_id)
@protocol.next_ssr_id = new_last_ssr_id + 1
@protocol.save(validate: false)
end
@protocol.next_ssr_id = id
@protocol.save(validate: false)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/locales/dashboard.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ en:
merged_does_not_exist: "Protocol to be merged ID does not exist"
one_calendar: "Only one protocol may have a calendar to be merged"
cannot_merge: "Protocol to be merged has already been pushed to SPARCFulfillment and can't be merged"
split_notify_error: "One or more of the requests are under the same split/notify and can't be merged"
success: "Protocol merge successful"

#####################
Expand Down

0 comments on commit 4441747

Please sign in to comment.