Class: MalawiHivProgramReports::Clinic::PatientsWithOutdatedDemographics

Inherits:
Object
  • Object
show all
Defined in:
app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, variant: 'poc', **_kwargs) ⇒ PatientsWithOutdatedDemographics

Returns a new instance of PatientsWithOutdatedDemographics.



11
12
13
14
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 11

def initialize(start_date:, variant: 'poc', **_kwargs)
  @date = start_date
  @variant = parse_variant(variant)
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 9

def date
  @date
end

#variantObject (readonly)

Returns the value of attribute variant.



9
10
11
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 9

def variant
  @variant
end

Instance Method Details

#arv_number_idObject



147
148
149
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 147

def arv_number_id
  @arv_number_id ||= ::PatientIdentifierType.find_by(name: 'ARV Number').patient_identifier_type_id
end

#died_state_idObject



151
152
153
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 151

def died_state_id
  @died_state_id ||= ::ProgramWorkflowState.find_by_name_and_program(name: 'Died', program_id: 1).id
end

#emastercard_patients_missing_demographicsObject

Find all patients missing any of the following variables:

- birthdate
- gender
- name
- landmark

NOTE: Old emastercard application did not collect a detailed and properly formatted address. The address was collected as free text and there was no standard provided for the address thus it’s treated as a landmark by this system. Even for the new eMastercard application running atop this API, a free text alternative is provided if the



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 106

def emastercard_patients_missing_demographics
  quoted_date = ActiveRecord::Base.connection.quote(date.to_s)

  ::Patient.find_by_sql(
    <<~SQL
      SELECT patient.patient_id AS patient_id,
             patient_identifier.identifier AS arv_number,
             person_name.given_name AS given_name,
             person_name.family_name AS family_name,
             person.birthdate AS birthdate,
             person.gender AS gender,
             person_address.city_village AS current_village,
             person_address.township_division AS current_traditional_authority,
             person_address.state_province AS current_district,
             person_address.neighborhood_cell AS home_village,
             person_address.county_district AS home_traditional_authority,
             person_address.address2 AS home_district,
             landmark.value AS landmark,
             person_address.date_created AS address_last_updated_date
      FROM patient
      INNER JOIN person ON person.person_id = patient.patient_id AND person.voided = 0
      INNER JOIN patient_program ON patient_program.patient_id = patient.patient_id AND patient_program.program_id = 1
      INNER JOIN patient_state ON patient_state.patient_program_id = patient_program.patient_program_id
      LEFT JOIN patient_identifier ON patient_identifier.patient_id = patient.patient_id
        AND patient_identifier.voided = 0 AND patient_identifier.identifier_type = #{arv_number_id}
      LEFT JOIN person_name ON person_name.person_id = patient.patient_id AND person_name.voided = 0
      LEFT JOIN person_address ON person_address.person_id = patient.patient_id AND person_address.voided = 0
      LEFT JOIN person_attribute landmark ON landmark.person_id = patient.patient_id
        AND landmark.person_attribute_type_id = #{landmark_attribute_type_id}
      WHERE patient.voided = 0
        AND (patient_state.state != #{died_state_id}
             OR (patient_state.state = #{transferred_out_state_id} AND patient_state.start_date < #{quoted_date}
                 AND (patient_state.end_date IS NULL OR patient_state.end_date > #{quoted_date})))
        AND ((person.birthdate IS NULL OR TRIM(COALESCE(person.gender, '')) = '')
             OR (TRIM(COALESCE(person_name.given_name, '')) = '' OR TRIM(COALESCE(person_name.family_name, '')) = '')
             OR (TRIM(COALESCE(landmark.value, '')) = '' OR landmark.date_created < #{quoted_date}))
      GROUP BY patient.patient_id
    SQL
  )
end

#find_reportObject



16
17
18
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/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 16

def find_report
  patients = if variant == 'poc'
               poc_patients_missing_demographics
             else
               emastercard_patients_missing_demographics
             end

  patients.collect do |patient|
    {
      patient_id: patient.patient_id,
      arv_number: patient.arv_number,
      given_name: patient.given_name,
      family_name: patient.family_name,
      birthdate: patient[:birthdate],
      gender: patient[:gender],
      current_village: patient.current_village,
      current_traditional_authority: patient.current_traditional_authority,
      current_district: patient.current_district,
      home_village: patient.home_village,
      home_traditional_authority: patient.home_traditional_authority,
      home_district: patient.home_district,
      landmark: patient.landmark,
      address_last_updated_date: patient.address_last_updated_date
    }
  end
end

#landmark_attribute_type_idObject



160
161
162
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 160

def landmark_attribute_type_id
  @landmark_attribute_type_id ||= ::PersonAttributeType.find_by_name('Landmark Or Plot Number').id
end

#parse_variant(variant) ⇒ Object

Raises:

  • (::InvalidParameterError)


164
165
166
167
168
169
170
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 164

def parse_variant(variant)
  variant = variant.downcase

  return variant if %w[poc emastercard].include?(variant)

  raise ::InvalidParameterError, "Invalid report variant '#{variant}'; expected poc or emastercard"
end

#poc_patients_missing_demographicsObject

Find all patients missing any of the following variables:

- birthdate
- gender
- name
- address


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 48

def poc_patients_missing_demographics
  quoted_date = ActiveRecord::Base.connection.quote(date)

  ::Patient.find_by_sql(
    <<~SQL
      SELECT patient.patient_id AS patient_id,
             patient_identifier.identifier AS arv_number,
             person_name.given_name AS given_name,
             person_name.family_name AS family_name,
             person.birthdate AS birthdate,
             person.gender AS gender,
             person_address.city_village AS current_village,
             person_address.township_division AS current_traditional_authority,
             person_address.state_province AS current_district,
             person_address.neighborhood_cell AS home_village,
             person_address.county_district AS home_traditional_authority,
             person_address.address2 AS home_district,
             landmark.value AS landmark,
             person_address.date_created AS address_last_updated_date
      FROM patient
      INNER JOIN person ON person.person_id = patient.patient_id AND person.voided = 0
      INNER JOIN patient_program ON patient_program.patient_id = patient.patient_id AND patient_program.program_id = 1
      INNER JOIN patient_state ON patient_state.patient_program_id = patient_program.patient_program_id
      LEFT JOIN patient_identifier ON patient_identifier.patient_id = patient.patient_id
        AND patient_identifier.voided = 0 AND patient_identifier.identifier_type = #{arv_number_id}
      LEFT JOIN person_name ON person_name.person_id = patient.patient_id AND person_name.voided = 0
      LEFT JOIN person_address ON person_address.person_id = patient.patient_id AND person_address.voided = 0
      LEFT JOIN person_attribute landmark ON landmark.person_id = patient.patient_id
        AND landmark.person_attribute_type_id = #{landmark_attribute_type_id}
      WHERE patient.voided = 0
        AND (patient_state.state != #{died_state_id}
             OR (patient_state.state = #{transferred_out_state_id} AND patient_state.start_date < #{quoted_date}
                 AND (patient_state.end_date IS NULL OR patient_state.end_date > #{quoted_date})))
        AND ((person.birthdate IS NULL OR TRIM(COALESCE(person.gender, '')) = '')
             OR (TRIM(COALESCE(person_name.given_name, '')) = '' OR TRIM(COALESCE(person_name.family_name, '')) = '')
             OR (TRIM(COALESCE(person_address.city_village, '')) = ''
                 OR TRIM(COALESCE(person_address.township_division, '')) = ''
                 OR TRIM(COALESCE(person_address.state_province, '')) = ''
                 OR TRIM(COALESCE(person_address.neighborhood_cell, '') = '')
                 OR TRIM(COALESCE(person_address.county_district, '') = '')
                 OR TRIM(COALESCE(person_address.address2, '') = '')
                 OR person_address.date_created < #{quoted_date}))
      GROUP BY patient.patient_id
    SQL
  )
end

#transferred_out_state_idObject



155
156
157
158
# File 'app/services/malawi_hiv_program_reports/clinic/patients_with_outdated_demographics.rb', line 155

def transferred_out_state_id
  @transferred_out_state_id ||= ::ProgramWorkflowState.find_by_name_and_program(name: 'Patient transferred out',
                                                                                program_id: 1).id
end