-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhis2.js
113 lines (112 loc) · 3.9 KB
/
dhis2.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
'use strict'
const winston = require('winston')
const request = require('request')
const URI = require('urijs')
const imm_dataelements = require('./terminologies/dhis-immunization-valuesets.json')
module.exports = function (cnf) {
const config = cnf
return {
getDhisDataMapping: function (callback) {
var total = imm_dataelements.compose.include[0].concept.length
var concept = imm_dataelements.compose.include[0].concept
var url = config.url
var username = config.username
var password = config.password
var dhisDataMapping = []
concept.forEach ((dataelement,dataelementindex) => {
this.getCategoryCombo (dataelement.code,(err,catComb,dataelement) => {
this.getCategoryOptionCombo(catComb,(err,catOptCombs) => {
catOptCombs.forEach ((catOptComb,catoptcombindex) => {
var catOptCombCode = catOptComb.id
this.getCategoryOptions(catOptCombCode,(err,catOpt) => {
dhisDataMapping.push({"dataelement":dataelement,"catoptcomb":catOptCombCode,"catopts":catOpt})
if(dataelementindex===concept.length-1 & catoptcombindex===catOptCombs.length-1) {
callback("",dhisDataMapping)
}
})
})
})
})
})
},
getCategoryCombo: function (dataelement,callback) {
var url = URI(config.url).segment('api/dataElements/'+dataelement)
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
url: url.toString(),
headers: {
Authorization: auth
}
}
request.get(options, (err, res, body) => {
if (err) {
return callback(err)
}
var catComb = JSON.parse(body).categoryCombo.id
callback(err,catComb,dataelement)
})
},
getCategoryOptionCombo: function (categoryCombo,callback) {
var url = URI(config.url).segment('api/categoryCombos/'+categoryCombo)
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
url: url.toString(),
headers: {
Authorization: auth
}
}
request.get(options, (err, res, body) => {
if (err) {
return callback(err)
}
callback(err,JSON.parse(body).categoryOptionCombos)
})
},
getCategoryOptions: function (categoryOptionCombo,callback) {
var url = URI(config.url).segment('api/categoryOptionCombos/'+categoryOptionCombo)
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
url: url.toString(),
headers: {
Authorization: auth
}
}
request.get(options, (err, res, body) => {
if (err) {
return callback(err)
}
callback(err,JSON.parse(body).categoryOptions)
})
},
saveImmunizationData: function (dataElement,catOptCombo,period,orgCode,value,callback) {
var url = URI(config.url).segment('api/dataValueSets')
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
url: url.toString(),
headers: {
'Content-Type': 'application/json',
Authorization: auth
},
json:{
"dataValues": [
{'dataElement':dataElement,'categoryOptionCombo':catOptCombo,'period':period,'orgUnit':orgCode,'value':value}
]
}
}
request.post(options, function (err, res, body) {
if (err) {
return callback(err)
}
callback(null,res,body)
})
}
}
}