Module: Lab::TestsService

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

Overview

Manage tests that have been ordered through the ordering service.

Class Method Summary collapse

Class Method Details

.create_tests(order, date, tests_params) ⇒ Object

Raises:

  • (InvalidParameterError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/lab/tests_service.rb', line 29

def create_tests(order, date, tests_params)
  raise InvalidParameterError, 'tests are required' if tests_params.nil? || tests_params.empty?

  
  Lab::LabTest.transaction do
    tests_params.map do |params|
      concept_id = params[:concept_id]
      concept_id = Concept.find_concept_by_uuid(params[:concept]).id if concept_id.nil?
      
      test = Lab::LabTest.create!(
        concept_id: ConceptName.find_by_name!(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
                               .concept_id,
        encounter_id: order.encounter_id,
        order_id: order.order_id,
        person_id: order.patient_id,
        obs_datetime: date&.to_time || Time.now,
        value_coded: concept_id
      )

      Lab::TestSerializer.serialize(test, order: order)
    end
  end
end

.find_tests(filters) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/lab/tests_service.rb', line 8

def find_tests(filters)
  tests = Lab::LabTest.all
  patient_id = filters.delete(:patient_id)
  patient_id ||= ::Patient.find(filters.delete(:patient))&.id if filters[:patient]

  Lab::UpdatePatientOrdersJob.perform_later(patient_id) if patient_id

  tests = filter_tests(tests, test_type_id: filters.delete(:test_type_id),
                              patient_id: filters.delete(:patient_id),
                              patient: filters.delete(:patient)
                              )

  tests = filter_tests_by_results(tests) if %w[1 true].include?(filters[:pending_results]&.downcase)

  tests = filter_tests_by_order(tests, accession_number: filters.delete(:accession_number),
                                       order_date: filters.delete(:order_date),
                                       specimen_type_id: filters.delete(:specimen_type_id))

  tests.map { |test| Lab::TestSerializer.serialize(test) }
end