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 | var openEHR = require('../openEHR/openEHR');
|
32 | var headingsLib = require('../headings/headings');
|
33 | var headings = headingsLib.headings;
|
34 | var transform = require('qewd-transform-json').transform;
|
35 | var flatten = require('../objectToFlatJSON');
|
36 | var dateTime = require('../dateTime');
|
37 |
|
38 | function deleteSessionCaches(patientId, heading, currentSessionId) {
|
39 |
|
40 |
|
41 |
|
42 | var sessions = this.sessions.active();
|
43 | sessions.forEach(function(session) {
|
44 | if (session.id !== currentSessionId) {
|
45 | session.data.$(['patients', patientId, 'headings', heading]).delete();
|
46 | session.data.$(['patients', patientId, 'headingIndex', heading]).delete();
|
47 | }
|
48 | });
|
49 | }
|
50 |
|
51 | function postHeading(nhsNo, heading, dataArr, ewdSession, callback) {
|
52 | if (headings[heading] && headings[heading].post) {
|
53 | var q = this;
|
54 | if (!Array.isArray(dataArr)) dataArr = [dataArr];
|
55 | var total = dataArr.length;
|
56 | var count = 0;
|
57 | dataArr.forEach(function(data) {
|
58 | var host = headings[heading].post.destination || 'ethercis';
|
59 | openEHR.startSession(host, function(session) {
|
60 | console.log('**** inside startSession callback - sessionId = ' + session.id);
|
61 | openEHR.mapNHSNoByHost(nhsNo, host, session.id, function(ehrId) {
|
62 |
|
63 | var patient = ewdSession.data.$(['patients', nhsNo]);
|
64 | patient.$(['headings', heading]).delete();
|
65 | patient.$(['headingIndex', heading]).delete();
|
66 | postHeadingData.call(q, heading, ehrId, host, session.id, data, function() {
|
67 | openEHR.stopSession(host, session.id);
|
68 | count++;
|
69 | if (callback && count === total) callback({info: heading + ' saved'});
|
70 | });
|
71 | });
|
72 | });
|
73 | });
|
74 |
|
75 | deleteSessionCaches.call(this, nhsNo, heading);
|
76 | }
|
77 | else {
|
78 | console.log('*** Heading ' + heading + ' either not recognised, or no POST definition available');
|
79 | callback({error: 'heading ' + heading + ' not recognised, or no POST definition available'});
|
80 | }
|
81 | }
|
82 |
|
83 | function postHeadingData(heading, ehrId, host, sessionId, data, callback) {
|
84 | if (headings[heading] && headings[heading].post) {
|
85 | var post = headings[heading].post;
|
86 |
|
87 | var helpers = post.helperFunctions || {};
|
88 | helpers.now = dateTime.now;
|
89 |
|
90 | var output = transform(post.transformTemplate, data, helpers);
|
91 | var body = flatten(output);
|
92 |
|
93 |
|
94 | var params = {
|
95 | host: host,
|
96 | callback: callback,
|
97 | url: '/rest/v1/composition',
|
98 | queryString: {
|
99 | templateId: post.templateId,
|
100 | ehrId: ehrId,
|
101 | format: 'FLAT'
|
102 | },
|
103 | method: 'POST',
|
104 | session: sessionId,
|
105 | options: {
|
106 | body: body
|
107 | }
|
108 | };
|
109 | console.log('**** about to post data: ' + JSON.stringify(params, null, 2));
|
110 | openEHR.request(params);
|
111 | }
|
112 | }
|
113 |
|
114 | module.exports = postHeading;
|