-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmit.js
60 lines (50 loc) · 1.36 KB
/
submit.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
const fs = require("fs").promises;
const fetch = require("node-fetch");
const mapLimit = require("async/mapLimit");
const dataDir = "./bundles/submit";
const fhirBaseUrl = "http://local.psbrandt.io:9876/hapi-fhir-jpaserver/fhir";
const batchSize = 10;
const submitBundle = (bundle, filename) => {
console.log(`Submitting ${filename}`);
return fetch(fhirBaseUrl, {
method: "post",
body: bundle,
headers: { "Content-Type": "application/fhir+json" }
}).then(res => {
console.log(
`Submitting ${filename} completed with response code ${res.status}`
);
return res.status;
});
};
const submitBatches = bundleFilenames => {
mapLimit(
bundleFilenames,
batchSize,
async function(bundleFilename) {
console.log(`Reading ${dataDir}/${bundleFilename}`);
return fs
.readFile(`${dataDir}/${bundleFilename}`)
.then(bundle => submitBundle(bundle, bundleFilename));
},
(err, results) => {
if (err) throw err;
// results is now an array of the response bodies
console.log(results);
}
);
};
const submit = () => {
const bundleFilenames = [];
fs.readdir(dataDir)
.then(items => {
items.forEach(item => {
bundleFilenames.push(item);
});
submitBatches(bundleFilenames);
})
.catch(e => {
console.log(`Error submitting: ${e}`);
});
};
submit();