UNPKG

4.91 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Rob Tweed, M/Gateway Developments Ltd |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 8 March 2017
28
29*/
30
31var openEHR = require('../openEHR/openEHR');
32var headingsLib = require('../headings/headings');
33var headings = headingsLib.headings;
34var transform = require('qewd-transform-json').transform;
35var flatten = require('../objectToFlatJSON');
36var dateTime = require('../dateTime');
37
38function deleteSessionCaches(patientId, heading, currentSessionId) {
39
40 // delete cached heading data in all active sessions
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
51function 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 // force a reload of this heading after the update
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 // delete any cached data for this patient and heading for all other active sessions
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
83function 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 // ready to post
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
114module.exports = postHeading;