Class: TransferRequest

Inherits:
ApplicationRecord show all
Extended by:
Request::Statemachine::ClassMethods
Includes:
AASM, Uuid::Uuidable
Defined in:
app/models/transfer_request.rb

Overview

Every request “moving” an asset from somewhere to somewhere else without really transforming it (chemically) as, cherrypicking, pooling, spreading on the floor etc

Constant Summary collapse

ACTIVE_STATES =

States which are still considered to be processable (ie. not failed or cancelled)

%w[pending started passed qc_complete].freeze

Instance Method Summary collapse

Methods included from Request::Statemachine::ClassMethods

destroy_aasm, redefine_aasm

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Instance Method Details

#outer_requestObject


120
121
122
# File 'app/models/transfer_request.rb', line 120

def outer_request
  asset.outer_request(submission_id)
end

#outer_request=(request) ⇒ Object


111
112
113
114
# File 'app/models/transfer_request.rb', line 111

def outer_request=(request)
  @outer_request = request
  self.submission_id = request.submission_id
end

#outer_request_candidates_lengthObject


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/transfer_request.rb', line 133

def outer_request_candidates_length
  # Its a simple scenario, we avoid doing anything fancy and just give the thumbs up
  return true if one_or_fewer_outer_requests?

  # If we're a bit more complicated attempt to match up requests
  # This operation is a bit expensive, but needs to handle scenarios where:
  # 1) We've already done some pooling, and have multiple requests in and out
  # 2) We've got multiple aliquots from a single request, such as in Chromium
  # Failing silently at this point could result in aliquots being assigned to the wrong study
  # or the correct request information being missing downstream. (Which is then tricky to diagnose and repair)
  asset.aliquots.reduce(true) do |valid, aliquot|
    compatible = next_request_index[aliquot.id].present?
    errors.add(:outer_request, "not found for aliquot #{aliquot.id} with previous request #{aliquot.request}") unless compatible
    valid && compatible
  end
end

#outer_request_id=(request_id) ⇒ Object


116
117
118
# File 'app/models/transfer_request.rb', line 116

def outer_request_id=(request_id)
  self.outer_request = Request.find(request_id)
end

#sibling_requestsObject

A sibling request is a customer request out of the same asset and in the same submission


125
126
127
128
129
130
131
# File 'app/models/transfer_request.rb', line 125

def sibling_requests
  if associated_requests.loaded?
    associated_requests.select { |r| r.submission_id == submission_id }
  else
    associated_requests.where(submission: submission_id)
  end
end

#source_and_target_assets_are_differentObject

validation method


99
100
101
102
103
104
105
# File 'app/models/transfer_request.rb', line 99

def source_and_target_assets_are_different
  return true unless asset_id.present? && asset_id == target_asset_id

  errors.add(:asset, 'cannot be the same as the target')
  errors.add(:target_asset, 'cannot be the same as the source')
  false
end

#transition_to(target_state) ⇒ Object


107
108
109
# File 'app/models/transfer_request.rb', line 107

def transition_to(target_state)
  aasm.fire!(suggested_transition_to(target_state))
end