-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopeninfoman.js
111 lines (99 loc) · 3.67 KB
/
openinfoman.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
'use strict'
const request = require('request')
const URI = require('urijs')
const utils = require('./utils')
// openinfoman object factory function
module.exports = function (cnf) {
const config = cnf
return {
/**
* fetchAllEntities - fetches all entities in a particular CSD document and
* callsback with the full CSD document.
*
* @param {Function} callback The callback takes the form of
* callback(err, result, orchestrations).
*/
fetchAllEntities: function (last_sync, reset, callback) {
let uri = new URI(config.url)
.segment('/CSD/csr/')
.segment(config.queryDocument)
.segment('careServicesRequest')
.segment('/urn:ihe:iti:csd:2014:stored-function:provider-search')
if (reset) {
var record = '<csd:record updated="1970-01-01T00:00:00"/>'
} else
var record = '<csd:record updated="' + last_sync + '"/>'
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64")
var options = {
url: uri.toString(),
headers: {
Authorization: auth,
'Content-Type': 'text/xml'
},
body: `<csd:requestParams xmlns:csd="urn:ihe:iti:csd:2013">
${record}
</csd:requestParams>`
}
let before = new Date()
request.post(options, function (err, res, body) {
if (err) {
return callback(err)
}
callback(null, body, [utils.buildOrchestration('OpenInfoMan fetch all entities', before, 'POST', options.url, options.body, res, body)])
})
},
/**
* loadProviderDirectory - loads a complete provider directory into OpenInfoMan.
* Note that this will clear any existing data in the directory and then load the new contents.
*
* @param {Array} providers a string array containing the xml provider entities
* @param {Function} callback (err, orchestrations)
*/
loadProviderDirectory: function (providers, callback) {
let orchestrations = []
var username = config.username
var password = config.password
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64")
let emptyDirectoryURI = new URI(config.url)
.segment('/CSD/emptyDirectory/')
.segment(config.rapidProDocument)
var options = {
url: emptyDirectoryURI.toString(),
headers: {
Authorization: auth
}
}
let before = new Date()
request.get(options, (err, res, body) => {
if (err) {
return callback(err)
}
orchestrations.push(utils.buildOrchestration('OpenInfoMan clear RapidPro directory', before, 'GET', emptyDirectoryURI.toString(), null, res, body))
let updateURI = new URI(config.url)
.segment('/CSD/csr/')
.segment(config.rapidProDocument)
.segment('/careServicesRequest/update/urn:openhie.org:openinfoman:provider_create')
var options = {
url: updateURI.toString(),
headers: {
Authorization: auth,
'Content-Type': 'text/xml'
},
body: `<requestParams xmlns="urn:ihe:iti:csd:2013" xmlns:csd="urn:ihe:iti:csd:2013">
${providers.join('\n')}
</requestParams>`
}
before = new Date()
request.post(options, (err, res, body) => {
if (err) {
return callback(err)
}
orchestrations.push(utils.buildOrchestration('OpenInfoMan load RapidPro directory', before, 'POST', options.url, options.body, res, body))
callback(null, orchestrations)
})
})
}
}
}