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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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 = begin
      Lab::LabTest.find(test_id)
    rescue StandardError
      nil
    end
    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

  # force commit all transactions
  ActiveRecord::Base.connection.commit_db_transaction

  # Execute job synchronously
  ProcessLabResultJob.perform_now(results_obs.id, serializer, result_enter_by)

  Rails.logger.info("Lab::ResultsService: Result created for test #{test_id} #{serializer}")

  # Publish notification that results have been created
  ActiveSupport::Notifications.instrument(
    'lab.results_created',
    patient_id: results_obs.person_id,
    order_id: results_obs.order_id,
    result_id: results_obs.obs_id,
    test_id: test_id,
    timestamp: Time.current
  )

  serializer
end

.process_result_completion(results_obs, serializer, result_enter_by) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/services/lab/results_service.rb', line 63

def process_result_completion(results_obs, serializer, result_enter_by)
  process_acknowledgement(results_obs, result_enter_by)
  precess_notification_message(results_obs, serializer, result_enter_by)
rescue StandardError => e
  Rails.logger.error("Lab::ResultsService: Error in post-result processing: #{e.message}")
  Rails.logger.error(e.backtrace.join("\n"))
  # Don't re-raise - result is already saved
end