Module: Lab::ResultSerializer

Defined in:
app/serializers/lab/result_serializer.rb

Overview

Serialize a Lab order result

Class Method Summary collapse

Class Method Details

.get_test_catalog_concept_name(concept_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/serializers/lab/result_serializer.rb', line 34

def self.get_test_catalog_concept_name(concept_id)
  return nil unless concept_id

  concept_name = ::ConceptAttribute.find_by(concept_id:, attribute_type: ConceptAttributeType.test_catalogue_name)&.value_reference
  return concept_name if concept_name.present?

  # If the concept does not have a test catalog name, check if it is UA or HCT and return the appropriate name
  # Otherwise, return the first concept name associated with the concept_id
  # Handles the case where a concept has multiple names, such as UA and HCT, which are both associated with the same concept_id
  # NB: Mostly for Old Lab tests that have been migrated to the new system, where the concept_id is the same for both UA and HCT, but the concept_name is different
  # A Case of AETC - Name was not present in the concept attribute table, but was present in the concept name table, so we need to check both tables to get the correct name
  concepts = %w[UA HCT]
  concept_names = ::ConceptName.where(concept_id: concept_id)
  if concept_names.any? { |cn| concepts.include?(cn.name) }
    concept_name = concept_names.find { |cn| concepts.include?(cn.name) }&.name
  else
    concept_name = concept_names.first&.name
  end
  concept_name
end

.read_value(measure) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/serializers/lab/result_serializer.rb', line 55

def self.read_value(measure)
  %w[value_numeric value_coded value_boolean value_text].each do |field|
    value = measure.send(field) if measure.respond_to?(field)

    return [value, field.split('_')[1]] if value
  end

  [nil, 'unknown']
end

.serialize(result) ⇒ Object



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

def self.serialize(result)
  result.children.map do |measure|
    value, value_type = read_value(measure)
    # Get the test catalog name instead of any random concept name
    concept_name = get_test_catalog_concept_name(measure.concept_id)
    program_id = ''
    if measure.obs_id.present?
      obs = Observation.unscope(where: :obs_group_id).find_by(obs_id: measure.obs_id)
      encounter = Encounter.find_by(encounter_id: obs&.encounter_id)
      program_id = encounter&.program_id
    end

    {
      id: measure.obs_id,
      indicator: {
        concept_id: measure.concept_id,
        name: concept_name
      },
      date: measure.obs_datetime,
      value:,
      value_type:,
      value_modifier: measure.value_modifier,
      program_id: program_id
    }
  end
end