Module: Lab::ConceptsService

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

Overview

A read-only repository of sort for all lab-centric concepts.

Class Method Summary collapse

Class Method Details

.reasons_for_testObject



75
76
77
78
79
80
# File 'app/services/lab/concepts_service.rb', line 75

def self.reasons_for_test
  ConceptSet.find_members_by_name(Lab::Metadata::REASON_FOR_TEST_CONCEPT_NAME)
            .joins('INNER JOIN concept_name ON concept_name.concept_id = concept_set.concept_id')
            .select('concept_name.concept_id, concept_name.name, concept_name.uuid')
            .map { |concept| { name: concept.name, concept_id: concept.concept_id, uuid: concept.uuid } }
end

.specimen_types(name: nil, test_type: nil) ⇒ Object



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
# File 'app/services/lab/concepts_service.rb', line 32

def self.specimen_types(name: nil, test_type: nil)
  specimen_types = ConceptSet.find_members_by_name(Lab::Metadata::SPECIMEN_TYPE_CONCEPT_NAME)
  specimen_types = specimen_types.filter_members(name: name) if name

  unless test_type
    return specimen_types.select('concept_name.concept_id, concept_name.name')
                         .joins('INNER JOIN concept_name ON concept_name.concept_id = concept_set.concept_id')
                         .group('concept_name.concept_id')
  end

  # Retrieve only those specimen types that belong to concept
  # set of the selected test_type
  test_types = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
                         .filter_members(name: test_type)
                         .select(:concept_id)

  concept_set = ConceptSet.where(
    concept_id: specimen_types.select(:concept_id),
    concept_set: test_types
  )

  concept_set.select('concept_name.concept_id, concept_name.name')
             .joins('INNER JOIN concept_name ON concept_name.concept_id = concept_set.concept_id')
             .group('concept_name.concept_id')
end

.test_result_indicators(test_type_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/lab/concepts_service.rb', line 58

def self.test_result_indicators(test_type_id)
  # Verify that the specified test_type is indeed a test_type
  test = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
                   .where(concept_id: Concept.find(test_type_id)&.id)
                   .select(:concept_id)

  # From the members above, filter out only those concepts that are result indicators
  measures = ConceptSet.find_members_by_name(Lab::Metadata::TEST_RESULT_INDICATOR_CONCEPT_NAME)
                       .select(:concept_id)

  ConceptSet.where(concept_set: measures, concept_id: test)
            .joins('INNER JOIN concept_name AS measure ON measure.concept_id = concept_set.concept_set')
            .select('measure.concept_id, measure.name, measure.uuid')
            .group('measure.concept_id')
            .map { |concept| { name: concept.name, concept_id: concept.concept_id, uuid: concept.uuid } }
end

.test_types(name: nil, specimen_type: nil) ⇒ Object



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

def self.test_types(name: nil, specimen_type: nil)
  test_types = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
  test_types = test_types.filter_members(name: name) if name

  unless specimen_type
    return test_types.joins('INNER JOIN concept_name ON concept_set.concept_id = concept_name.concept_id AND concept_name.voided = 0 AND concept_name.locale_preferred = 1')
                     .select('concept_name.name, concept_name.concept_id')
                     .group('concept_name.concept_id')
  end

  # Filter out only those test types that have the specified specimen
  # type.
  specimen_types = ConceptSet.find_members_by_name(Lab::Metadata::SPECIMEN_TYPE_CONCEPT_NAME)
                             .filter_members(name: specimen_type)
                             .select(:concept_id)

  concept_set = ConceptSet.where(
    concept_id: specimen_types,
    concept_set: test_types.select(:concept_id)
  )

  concept_set.joins('INNER JOIN concept_name ON concept_set.concept_set = concept_name.concept_id')
             .select('concept_name.concept_id, concept_name.name')
             .group('concept_name.concept_id')
end