-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimr.js
177 lines (169 loc) · 6.47 KB
/
timr.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
'use strict'
const winston = require('winston')
const request = require('request')
const _ = require('underscore')
const URI = require('urijs')
const moment = require("moment")
const catOptOpers = require('./config/categoryOptionsOperations.json')
const timrDhisImm = require('./terminologies/timr-immunization-to-DHIS2-immunization-conceptmap.json')
module.exports = function (timrcnf,oauthcnf) {
const timrconfig = timrcnf
const timroauthconfig = oauthcnf
return {
getAccessToken: function (callback) {
let url = URI(timroauthconfig.url)
let before = new Date()
var options = {
url: url.toString(),
headers: {
Authorization: `BASIC ${timroauthconfig.token}`
},
body: `grant_type=password&username=${timroauthconfig.username}&password=${timroauthconfig.password}&scope=${timroauthconfig.scope}`
}
request.post(options, (err, res, body) => {
if (err) {
return callback(err)
}
callback(err, res, body)
})
},
hasAge: function (dhisData,callback) {
dhisData.catopts.forEach((catOpt,dhisdataindex) => {
catOptOpers.forEach((catOptOper) => {
if(catOptOper.code == catOpt.id) {
catOptOper.operations.forEach((oper,catoptoperindex) => {
if(_.contains(["years","months","weeks"],catOptOper.dimension[catoptoperindex]))
callback(true)
if(dhisdataindex === dhisData.catopts.length-1 & catOptOper.operations.length-1 === catoptoperindex)
callback(false)
})
}
})
})
},
getQueryParameters: function (dhisData,callback) {
var age = ["years","months","weeks"]
var query = ""
var queries = []
var ages = []
dhisData.catopts.forEach((catOpt,dhisdataindex) => {
//if(dhisData.catoptcomb == 'wGvxdkuPC9I')
catOptOpers.forEach((catOptOper) => {
if(catOptOper.code == catOpt.id) {
catOptOper.operations.forEach((oper,catoptoperindex) => {
var birthDate = ""
if(_.contains(age,catOptOper.dimension[catoptoperindex])) {
ages.push({"value":oper.value,"dimension":catOptOper.dimension[catoptoperindex],'operation':oper.operation})
}
else {
var dimension = catOptOper.dimension[catoptoperindex]
var value = oper.value
}
if(query & value)
query = query +"&" + dimension + oper.operation + value
else if(value)
query = dimension + oper.operation + value
if(dhisdataindex === dhisData.catopts.length-1 & catOptOper.operations.length-1 === catoptoperindex) {
//make sure patient.gender is the first parameter
if(query.includes('&patient.gender')) {
var genderPar = ""
var operArr = query.split('&')
operArr.forEach((par) => {
if(par.includes('patient.gender'))
genderPar = par
})
query = query.replace('&'+genderPar,'')
query = genderPar+'&'+query
}
//if categoryOptionCombo has Age,then we need to calculate Age for every vaccine date
if(ages.length>0) {
this.addAgeOnQueryParameter(ages,query,(queries) => {
callback(queries)
})
}
else {
var vaccineStartDate = moment().subtract(1,'month').startOf('month').format('YYYY-MM-DD')
var vaccineEndDate = moment().subtract(1,'month').endOf('month').format('YYYY-MM-DD')
query = query + '&date=ge' + vaccineStartDate + '&date=le' + vaccineEndDate
queries.push({'query':query})
callback(queries)
}
}
})
}
})
})
},
addAgeOnQueryParameter: function (ages,query,callback) {
var endDay = moment().subtract(1,'month').endOf('month').format('D') //getting the last day of last month
var startDay = 1;
var queries = []
for(var day=startDay;day<=endDay;day++) {
var birthDatePar = ''
ages.forEach((age,index) => {
if(day<10)
var dateDay = '0' + day
else
var dateDay = day
var vaccineDate = moment().subtract(1,'month').format('YYYY-MM') + '-' + dateDay
var birthDate = moment(vaccineDate).subtract(age.value,age.dimension).format('YYYY-MM-DDTHH:mm:ss')
birthDatePar = birthDatePar + '&patient.birthDate' + age.operation + birthDate
if(ages.length-1 == index) {
if(query)
var newQuery = query + '&date=eq' + vaccineDate + birthDatePar
else
var newQuery = 'date=eq' + vaccineDate + birthDatePar
queries.push({'query':newQuery})
}
if(day == endDay & ages.length-1 == index) {
callback (queries)
}
})
}
},
getVaccineCode: function (dataelement,callback) {
timrDhisImm.group.forEach((groups) => {
groups.element.forEach((element)=>{
if(element.code == dataelement) {
element.target.forEach((target) => {
callback(target.code)
})
}
})
})
},
getImmunizationData: function (access_token,dhisData,facilityid,callback) {
var dataelement = dhisData.dataelement
var catOptComb = dhisData.catoptcomb
this.getVaccineCode(dataelement,(vaccinecode)=> {
if(vaccinecode == "") {
return
}
this.getQueryParameters(dhisData,(queries) => {
var totalValues = 0
queries.forEach((qry,index)=> {
let url = URI(timrconfig.url)
.segment('Immunization')
+'?'+qry.query+'&vaccine-code='+vaccinecode+'&_format=json&_count=0'
.toString()
var options = {
url: url.toString(),
headers: {
Authorization: `BEARER ${access_token}`
}
}
request.get(options, (err, res, body) => {
if (err) {
return callback(err)
}
totalValues = totalValues + JSON.parse(body).total
if(queries.length-1 == index) {
callback(err,totalValues,url)
}
})
})
})
})
}
}
}