Module: Lab::ResultsService

Defined in:
app/services/lab/results_service.rb

Class Method Summary collapse

Class Method Details

.create_results(test_id, params, result_enter_by = 'LIMS') ⇒ Object

Attach results to a test

Params:

test_id: The tests id (maps to obs_id of the test's observation in OpenMRS)
params: A hash comprising the following fields
  - encounter_id: Encounter to create result under (can be ommitted but provider_id has to specified)
  - provider_id: Specify a provider for an encounter the result is going to be created under
  - date: Retrospective date when the result was received (can be ommitted, defaults to today)
  - measures: An array of measures. A measure is an object of the following structure
      - indicator: An object that has a concept_id field (concept_id of the indicator)
      - value_type: An enum that's limited to 'numeric', 'boolean', 'text', and 'coded'
result_enter_by: A string that specifies who created the result


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/lab/results_service.rb', line 19

def create_results(test_id, params, result_enter_by = 'LIMS')
  serializer = {}
  results_obs = {}
  ActiveRecord::Base.transaction do
    test = Lab::LabTest.find(test_id) rescue nil
    test = Lab::LabTest.find_by_uuid(test_id) if test.blank?
    encounter = find_encounter(test, encounter_id: params[:encounter_id],
                                     encounter_uuid: params[:encounter],
                                     date: params[:date]&.to_date,
                                     provider_id: params[:provider_id])

    results_obs = create_results_obs(encounter, test, params[:date], params[:comments])
    params[:measures].map { |measure| add_measure_to_results(results_obs, measure, params[:date]) }
    OrderExtension.create!(creator: User.current, value: result_enter_by, order_id: results_obs.order_id,
                           date_created: Time.now)

    serializer = Lab::ResultSerializer.serialize(results_obs)
  end
  process_acknowledgement(results_obs, result_enter_by)
  precess_notification_message(results_obs, serializer, result_enter_by)
  Rails.logger.info("Lab::ResultsService: Result created for test #{test_id} #{serializer}")
  serializer
end