forked from ere-health/ere-ps-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-e-prescription-with-runtime-config.js
126 lines (114 loc) · 9.16 KB
/
create-e-prescription-with-runtime-config.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
function getUrl(urlString, accept, content, headers) {
var url = new java.net.URL(urlString);
// Open connection to url
var conn = url.openConnection();
if(accept) {
conn.setRequestProperty ("Accept", accept);
}
if(content) {
conn.setRequestProperty ("Content-Type", content);
}
if(headers) {
for(var prop in headers) {
conn.setRequestProperty (prop, headers[prop]);
}
}
return conn.getInputStream().readAllBytes();
}
function postToUrl(urlString, data, accept, content, headers) {
var url = new java.net.URL(urlString);
// Open connection to url
var conn = url.openConnection();
conn.setDoOutput(true);
if(accept) {
conn.setRequestProperty ("Accept", accept);
}
if(content) {
conn.setRequestProperty ("Content-Type", content);
}
if(headers) {
for(var prop in headers) {
conn.setRequestProperty (prop, headers[prop]);
}
}
// Send request
var outStream = conn.getOutputStream();
var outWriter = new java.io.OutputStreamWriter(outStream);
outWriter.write(data);
outWriter.close();
return conn.getInputStream().readAllBytes();
}
var task, taskId, prescriptionId, accessCode, signedBase64Document;
// https://github.com/gematik/api-erp/blob/master/docs/erp_bereitstellen.adoc
var runtimeConfig = {
"X-connectorBaseURL": "https://kon-instanz2.titus.gematik.solutions",
"X-clientCertificate": "data:application/x-pkcs12;base64,MIACAQMwgAYJKoZIhvcNAQcBoIAkgASCA+gwgDCABgkqhkiG9w0BBwGggCSABIID6DCCBVQwggVQBgsqhkiG9w0BDAoBAqCCBPswggT3M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jhtN79AwuziEmItGZtZgZnEyztySk7/wZp4MAv+Xb+aSXu3O9xIdH4kxBIRf7T1eJcif/RszX2zZtufVMxq/P1/oEV6c/z0QSB5Rqmq49vSvVD+XQgW6Foq7Zv9g1YjP3g5tOy1OSwHPJvRTOoWm9vT0LR2oc4MhZp5kvL+NcIgnaDlT6L1kvFrXUTcU1Ovexi8rtvxPuKZmN9ratuprNAhgxl0pqipQc8F2gl+LxZZeJh46S4qyO+M4FzkkKRCMwyX6LUQ1USXAAvc2te7lAAAAAAAAAAAAAAAAAAAAAAAAMD4wITAJBgUrDgMCGgUABBQ6B8AjOebmQg4ipAoBlADwP/1zxAQUq5nLhbnbphTOkV8GnY828gtc/qoCAwGQAAAA",
"X-clientCertificatePassword": "00",
"X-clientSystemId": "Incentergy",
"X-mandantId": "1786_A1",
"X-version": "PTV4+",
"X-workplaceId": "Incentergy"
};
var getCards = JSON.parse(new java.lang.String(getUrl("http://localhost:8080/workflow/cards", "application/json", undefined, runtimeConfig)));
runtimeConfig["X-eHBAHandle"] = getCards.cards.card.filter(function (o) { return o.cardType == "HBA";})[0].cardHandle;
runtimeConfig["X-SMCBHandle"] = getCards.cards.card.filter(function (o) { return o.cardType == "SMC_B";})[0].cardHandle;
// Create a task
try {
var taskString = new java.lang.String(postToUrl("http://localhost:8080/workflow/task", "", "application/json", undefined, runtimeConfig));
println(taskString);
task = JSON.parse(taskString);
taskId = task.id;
prescriptionId = task.identifier.filter(function (o) {
return o.system == "https://gematik.de/fhir/NamingSystem/PrescriptionID";
})[0].value;
accessCode = task.identifier.filter(function (o) {
return o.system == "https://gematik.de/fhir/NamingSystem/AccessCode";
})[0].value;
} catch(e) {
println("ERROR during creating task: "+e.message);
}
// Add the prescription id to the bundle and sign it
try {
var jsonBundle = JSON.parse(new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("src/test/resources/bundle-json/0428d416-149e-48a4-977c-394887b3d85c.json"))));
jsonBundle.id = java.util.UUID.randomUUID().toString();
jsonBundle.identifier.value = prescriptionId;
// TODO: show KBV xslt transformed stylesheet
var signedBase64Document = new java.lang.String(postToUrl("http://localhost:8080/workflow/sign", JSON.stringify(jsonBundle), "text/plain", undefined, runtimeConfig));
println(signedBase64Document);
} catch(e) {
println("ERROR during signing task: "+e.message);
}
// Update the task with the signed bundle
try {
var updateERezept = {
"taskId": taskId,
"accessCode": accessCode,
"signedBytes": signedBase64Document
};
postToUrl("http://localhost:8080/workflow/update", JSON.stringify(updateERezept), "text/plain", "application/json", runtimeConfig);
} catch(e) {
println("ERROR updating task: "+e.message);
}
// Create PDF from task
try {
var bundles = [{
"accessCode": accessCode,
"bundle": jsonBundle
}];
var bundlesString = JSON.stringify(bundles);
println(bundlesString);
var pdfDocument = postToUrl("http://localhost:8080/document/bundles", bundlesString, "application/pdf", "application/json");
java.nio.file.Files.write(java.nio.file.Paths.get("target/javascript-runtime-config.pdf"), pdfDocument);
} catch(e) {
println("ERROR during producing pdf: "+e.message);
}
// Abort the task
try {
var abortERezept = {
"taskId": taskId,
"accessCode": accessCode
};
postToUrl("http://localhost:8080/workflow/abort", JSON.stringify(abortERezept), "text/plain", "application/json", runtimeConfig);
} catch(e) {
println("ERROR abort task: "+e.message);
}