eirenerx-sdk
Version:
EireneRx JavaScript SDK
96 lines (84 loc) • 2.91 kB
JavaScript
// patient api
//
// Required Options
//
// * key - vendor key for authorization
// * secret - vendor secret for authorization
// * sponsor - eirenerx sponsor id (ie customer number)
// * center - eirenerx center id (ie office/location number)
// * identifier - EHR Unique Patient Identifier
// if identifier is set, then this module will find
// the patient and return a patient object
// if the indentifier is not set, then this module will
// create an empty instance of a patient object that
// can be filled and pushed to EireneRx
// * api url
// eirenerx api url, https://io.eirenerx.com https://io-staging.eirenerx.com
var request = require('request');
var PATIENTS = 'api/vendor/v1/patients';
var validate = require('./validate-patient');
module.exports = function(options, identifier, callback) {
var patient = function(identifier, data) {
var isNew = data ? false : true;
var demographics = data ? data.demographics : { identifier: identifier };
var allergies = data ? data.allergies : [];
var medications = data ? data.medications : [];
var new_rx_requests = data ? data.new_rx_requests : [];
var save = function(p, callback) {
if (typeof p == 'function') { p(new Error('Patient Object must be first param')); }
var params = {
auth: {
user: options.key,
pass: options.secret
},
headers: {
'X-Sponsor-Id': options.sponsor,
'X-Center-Id': options.center
},
json: {
demographics: p.demographics || {},
allergies: p.allergies || []
}
};
if (isNew) {
// if validate returns null then data is valid
// if validate returns object then data has errors
var results = validate(params.json);
if (!results.valid) { return callback(results); }
// POST Patient Data
request.post([options.api, PATIENTS].join('/'), params, handler);
} else {
// PUT Patient Data
request.put([options.api, PATIENTS, identifier].join('/'), params, handler);
}
function handler(e,r,b) {
if (e) { return callback(e); }
if (b.errors) { return callback(b.errors); }
callback(null, b);
}
};
return {
demographics: demographics,
allergies: allergies,
medications: medications,
new_rx_requests: new_rx_requests,
save: save
};
};
request.get([options.api, PATIENTS, identifier].join('/'), {
auth: {
user: options.key,
pass: options.secret
},
headers: {
'X-Sponsor-Id': options.sponsor,
'X-Center-Id': options.center
},
json: true,
}, function(e, r, b) {
if (e) { return callback(e); }
// If Patient Not Found.... Then create new Patient Object
if (b.length) { return callback(null, patient(identifier)); }
callback(null, patient(identifier, b));
});
};