eirenerx-sdk
Version:
EireneRx JavaScript SDK
68 lines (64 loc) • 2.13 kB
JavaScript
// eprescribe 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
// * email - user email
// 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
// passes err, url in callback
var request = require('request');
var PATIENTS = 'api/vendor/v1/patients';
var USERS = 'api/vendor/v1/users';
module.exports = function(opts) {
var params = {
auth: {
user: opts.key,
pass: opts.secret
},
headers: {
'X-Sponsor-Id': opts.sponsor,
'X-Center-Id': opts.center
},
json: true,
};
var tokenUrl = [opts.api, USERS, opts.email, 'authentication_token'].join('/');
var patientUrl = [opts.api, PATIENTS, opts.identifier].join('/');
var urls = [tokenUrl, patientUrl];
//get token by e-mail
//get eRx id by identifier
var getUrl = function(callback) {
var token = null;
var patientId = null;
var errors = [];
var done = function(e, r, b) {
if(e) { errors.push(e); }
// if two errors or one error and one success, then return callback
if (errors.length === 2 || (errors.length === 1 && (token || patientId))) {
return callback(errors);
}
if (b.authentication_token) { token = b.authentication_token; }
if (b.demographics) { patientId = b.demographics.id; }
// got both token and patientId build url
if (token && patientId) {
var url = [opts.baseUrl, patientId, 'new_rx_requests', 'new?auth_token=' + token].join('/');
callback(null, url);
}
};
urls.forEach(function(url) {
request.get(url, params, done);
});
};
return Object.freeze({
getUrl: getUrl
});
};