-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.js
executable file
·402 lines (384 loc) · 15 KB
/
middleware.js
1
2
3
4
5
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
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
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
94
95
96
97
98
99
100
101
102
103
104
105
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const {
Pool
} = require('pg')
const async = require('async')
const winston = require('winston')
const nconf = require('nconf')
let pool
setTimeout(() => {
pool = new Pool({
user: nconf.get("timrPostgres:username"),
password: nconf.get("timrPostgres:password"),
host: nconf.get("timrPostgres:host"),
database: nconf.get("timrPostgres:database"),
port: nconf.get("timrPostgres:port"),
})
},1000)
module.exports = {
getImmunizationCoverageData: (startDate, endDate, ages, callback) => {
let ageQuery = ''
async.eachSeries(ages, (age, nxtAge) => {
ageQuery += ` and sbadm_tbl.act_utc - pat_vw.dob ${age.operator} '${age.age}'::INTERVAL`
return nxtAge()
})
let query = `select
ext_id as facility_id,
mat_tbl.type_mnemonic,
sbadm_tbl.seq_id,
pat_vw.gender_mnemonic,
count(case when lower(catchment.tag_value) = 'true' then 1 else null end) as in_service_area,
count(case when lower(catchment.tag_value) <> 'true' then 1 else null end) as in_catchment
from
sbadm_tbl
-- Join material
inner join mat_tbl on (mat_tbl.mat_id = sbadm_tbl.mat_id)
-- join facility information
inner join fac_vw on (sbadm_tbl.fac_id = fac_vw.fac_id)
-- fetch HIE FRID for the facility
inner join fac_id_tbl on (fac_vw.fac_id = fac_id_tbl.fac_id and nsid = 'TZ_HFR_ID')
-- Fetch patient information for gender
inner join pat_vw on (pat_vw.pat_id = sbadm_tbl.pat_id)
inner join enc_tbl using (enc_id)
left join act_list_act_rel_tbl on (enc_tbl.enc_id = sbadm_act_id)
left join act_list_tbl on (act_list_tbl.act_id = act_list_act_rel_Tbl.act_id)
-- fetch catchment indicator extension
left join act_tag_tbl catchment on (catchment.act_id = sbadm_tbl.act_id and catchment.tag_name = 'catchmentIndicator')
left join (SELECT act_id, CASE WHEN tag_value = '1' THEN 'ActType-TimrOutreachSession' END AS typ_mnemonic FROM act_tag_tbl WHERE tag_name = 'outreach') enc_or ON (enc_or.act_id = sbadm_tbl.enc_id )
left join act_ext_tbl population ON (population.act_id = sbadm_tbl.act_id and population.ext_typ = 'http://openiz.org/extensions/contrib/timr/batchPopulationType')
where
-- we don't want back-entered data
sbadm_tbl.enc_id is not null
-- we dont want supplements
and sbadm_tbl.type_mnemonic != 'DrugTherapy'
and sbadm_tbl.type_mnemonic NOT IN ('DrugTherapy','BoosterImmunization')
-- we don't want those vaccinations not done
-- and not sbadm_tbl.neg_ind
-- action occurred during month
and sbadm_tbl.act_utc::DATE between '${startDate}' and '${endDate}'
${ageQuery}
group by ext_id, mat_tbl.type_mnemonic, pat_vw.gender_mnemonic, sbadm_tbl.seq_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period")
return callback([])
}
})
},
getSupplementsData: (startDate, endDate, ages, callback) => {
let ageQuery = ''
if (ages.length == 2) {
let age1 = ages[0]
let age2 = ages[1]
let ageNumber1 = parseFloat(age1.age.split(' ').shift())
let ageNumber2 = parseFloat(age2.age.split(' ').shift())
if (ageNumber1 < ageNumber2) {
ageQuery = ` and act_utc - dob BETWEEN '${age1.age}'::INTERVAL AND '${age2.age}'::INTERVAL`
} else {
ageQuery = ` and act_utc - dob BETWEEN '${age2.age}'::INTERVAL AND '${age1.age}'::INTERVAL`
}
} else {
async.eachSeries(ages, (age, nxtAge) => {
ageQuery += ` and act_utc - dob ${age.operator} '${age.age}'::INTERVAL`
return nxtAge()
})
}
let query = `select
ext_id as facility_id,
mat_tbl.type_mnemonic as code,
sbadm_tbl.seq_id,
pat_vw.gender_mnemonic,
count(*) as total
from
sbadm_tbl
-- inner join mat_tbl using (mat_id)
inner join mat_tbl on (mat_tbl.mat_id = sbadm_tbl.mat_id)
-- inner join pat_vw using (pat_id)
inner join pat_vw on (pat_vw.pat_id = sbadm_tbl.pat_id)
inner join fac_vw on (sbadm_tbl.fac_id = fac_vw.fac_id)
inner join fac_id_tbl on (fac_id_tbl.fac_id = fac_vw.fac_id and nsid = 'TZ_HFR_ID')
where
act_utc::DATE between '${startDate}' and '${endDate}'
${ageQuery}
and sbadm_tbl.type_mnemonic = 'DrugTherapy'
group by ext_id, mat_tbl.type_mnemonic, pat_vw.gender_mnemonic, sbadm_tbl.seq_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR")
return callback([])
}
})
},
getPMTCTData: (startDate, endDate, callback) => {
let query = `select
ext_id as facility_id,
pat_vw.gender_mnemonic,
ebf.ext_value,
count(*) as total
from
pat_vw
inner join ent_ext_tbl as ebf on (pat_vw.pat_id = ebf.ent_id and ebf.ext_typ = 'http://openiz.org/extensions/patient/contrib/timr/pctmtStatus')
inner join fac_id_tbl on (fac_id_tbl.fac_id = pat_vw.fac_id and nsid = 'TZ_HFR_ID')
where
crt_utc::DATE between '${startDate}' and '${endDate}' and ext_value='1'
group by
ext_id, ebf.ext_value, pat_vw.gender_mnemonic order by ext_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR")
return callback([])
}
})
},
getCTCReferal: (startDate, endDate, callback) => {
//add pat_vw.dob - pat_vw.crt_utc < '12 MONTH'::INTERVAL to filter by age
let query = `select
ext_id as facility_id,
pat_vw.gender_mnemonic,
ebf.ext_value,
count(*) as total
from
pat_vw
inner join ent_ext_tbl as ebf on (pat_vw.pat_id = ebf.ent_id and ebf.ext_typ = 'http://openiz.org/extensions/contrib/timr/ctcReferral')
inner join fac_id_tbl on (fac_id_tbl.fac_id = pat_vw.fac_id and nsid = 'TZ_HFR_ID')
where
crt_utc::DATE between '${startDate}' and '${endDate}'
group by
ext_id, ebf.ext_value, pat_vw.gender_mnemonic order by ext_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR")
return callback([])
}
})
},
getDispLLINMosqNet: (startDate, endDate, callback) => {
let query = `select
ext_id as facility_id,
gender_mnemonic,
count(*) as total
from
sply_tbl
-- ensure that the material given was a mosquito net
left join sply_mat_tbl on (sply_tbl.sply_id = sply_mat_tbl.sply_id and sply_mat_tbl.mat_id = '276d2ce0-6504-11e9-a923-1681be663d3e')
-- in a supply the source entity is the facility
inner join fac_vw on (src_ent_id = fac_id)
-- fetch HIE FRID for the facility
inner join fac_id_tbl on (fac_vw.fac_id = fac_id_tbl.fac_id and nsid = 'TZ_HFR_ID')
-- in a supply the target entity is the patient (i.e. the facility is supplying to the patient)
inner join enc_tbl using (enc_id)
inner join pat_Vw on (enc_tbl.pat_id = pat_vw.pat_id)
where
typ_mnemonic = 'ActType-SupplyToPatient'
and sply_tbl.act_utc::DATE between '${startDate}' and '${endDate}'
group by ext_id, gender_mnemonic`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR")
return callback([])
}
})
},
getBreastFeedingData: (startDate, endDate, ages, code, callback) => {
let ageQuery = ''
async.eachSeries(ages, (age, nxtAge) => {
if (ageQuery) {
ageQuery += 'and ' + `pat_vw.dob - pat_vw.crt_utc ${age.operator} '${age.age}'::INTERVAL`
} else {
ageQuery += `pat_vw.dob - pat_vw.crt_utc ${age.operator} '${age.age}'::INTERVAL`
}
return nxtAge()
}, () => {
if (ageQuery) {
ageQuery += ' and'
}
})
let query = `select
ext_id as facility_id,
pat_vw.gender_mnemonic,
ebf.ext_value,
count(*) as total
from
pat_vw
inner join ent_ext_tbl as ebf on (pat_vw.pat_id = ebf.ent_id and ebf.ext_typ = 'http://openiz.org/extensions/patient/contrib/timr/breastFeedingStatus')
inner join fac_id_tbl on (fac_id_tbl.fac_id = pat_vw.fac_id and nsid = 'TZ_HFR_ID')
where
${ageQuery} crt_utc::DATE between '${startDate}' and '${endDate}' and ext_value='${code}'
group by
ext_id, ebf.ext_value, pat_vw.gender_mnemonic order by ext_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period " + period.periodName)
return callback([])
}
})
},
getChildWithBirthCertData: (startDate, endDate, callback) => {
let query = `select
ext_id as facility_id,
pat_vw.gender_mnemonic,
ebf.ext_value,
count(*) as total
from
pat_vw
inner join ent_ext_tbl as ebf on (pat_vw.pat_id = ebf.ent_id and ebf.ext_typ = 'hasBirthCertificate')
inner join fac_id_tbl on (fac_id_tbl.fac_id = pat_vw.fac_id and nsid = 'TZ_HFR_ID')
where
crt_utc::DATE between '${startDate}' and '${endDate}' and ext_value='True'
group by
ext_id, ebf.ext_value, pat_vw.gender_mnemonic order by ext_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period " + period.periodName)
return callback([])
}
})
},
getWeightAgeRatio: (startDate, endDate, ages, callback) => {
let ageQuery = ''
async.eachSeries(ages, (age, nxtAge) => {
ageQuery += ' and ' + `act_utc - dob ${age.operator} '${age.age}'::INTERVAL`
return nxtAge()
})
let query = `select
ext_id as facility_id,
gender_mnemonic,
int_cs as code,
count(*) as total
from
qty_obs_tbl
-- inner join pat_vw using (pat_id)
inner join pat_vw on (pat_vw.pat_id = qty_obs_tbl.pat_id)
inner join fac_vw on (qty_obs_tbl.fac_id = fac_vw.fac_id)
inner join fac_id_tbl on (fac_id_tbl.fac_id = fac_vw.fac_id and nsid = 'TZ_HFR_ID')
where
typ_cs = 'VitalSign-Weight'
and act_utc::DATE between '${startDate}' and '${endDate}'
${ageQuery}
group by ext_id, gender_mnemonic, int_cs`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period " + period.periodName)
return callback([])
}
})
},
getChildVisitData: (startDate, endDate, ages, callback) => {
let ageQuery = ''
async.eachSeries(ages, (age, nxtAge) => {
ageQuery += ' and ' + `act_utc - dob ${age.operator} '${age.age}'::INTERVAL`
return nxtAge()
})
let query = `select
ext_id as facility_id,
gender_mnemonic,
count(*) as total
from
enc_tbl
inner join pat_vw using (pat_id)
inner join fac_vw on (enc_tbl.fac_id = fac_vw.fac_id)
inner join fac_id_tbl on (fac_id_tbl.fac_id = fac_vw.fac_id and nsid = 'TZ_HFR_ID')
where
act_utc::DATE between '${startDate}' and '${endDate}'
${ageQuery}
and pat_vw.sts_cs = 'ACTIVE'
group by ext_id, gender_mnemonic`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period " + period.periodName)
return callback([])
}
})
},
getTTData: (startDate, endDate, callback) => {
let query = `select
ext_id as facility_id,
pat_tbl.gender_mnemonic,
ebf.ext_value,
count(*) as total
from
pat_tbl
inner join psn_tbl using (psn_id)
inner join ent_ext_tbl as ebf on (pat_tbl.pat_id = ebf.ent_id and ebf.ext_typ = 'http://openiz.org/extensions/patient/contrib/timr/tetanusStatus')
inner join fac_id_tbl on (fac_id_tbl.fac_id = pat_tbl.reg_fac_id and nsid = 'TZ_HFR_ID')
where
crt_utc::DATE between '${startDate}' and '${endDate}' and (ext_value='0' or ext_value='1' or ext_value='2')
group by
ext_id, ebf.ext_value, pat_tbl.gender_mnemonic order by ext_id`
pool.query(query, (err, response) => {
if (err) {
winston.error(err)
return callback([])
}
if (response && response.hasOwnProperty('rows')) {
winston.info("TImR has returned with " + response.rows.length + " rows")
return callback(response.rows)
} else {
winston.warn("Invalid response has been received from TImR for period " + period.periodName)
return callback([])
}
})
}
}