Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jl - Protocol merge ssr ordering and new validations #2297

Merged
merged 8 commits into from
Apr 17, 2020
22 changes: 21 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)
jleonardw9 marked this conversation as resolved.
Show resolved Hide resolved
#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,21 @@ 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
def requests_not_merge_eligible?(master, merged)
master_requests = master.sub_service_requests
merged_requests = merged.sub_service_requests

master_requests.each do |master_request|
jleonardw9 marked this conversation as resolved.
Show resolved Hide resolved
merged_requests.each do |merged_request|
master_merge_ineligible = (master_request.is_locked? || !master_request.is_complete?)
merged_merge_ineligible = (merged_request.is_locked? || !merged_request.is_complete?)
if (master_request.process_ssrs_organization == merged_request.process_ssrs_organization) && (master_protocol_ineligible || merged_protocol_ineligible)
return true
end
end
end
return false
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