Skip to content

Commit

Permalink
extracted needed files
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Aug 30, 2015
1 parent acadbfa commit 257cb4a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `BG_LOW` (`55`) - must be set using mg/dl units; the low BG outside the target range that is considered urgent
* `ALARM_TYPES` (`simple` if any `BG_`* ENV's are set, otherwise `predict`) - currently 2 alarm types are supported, and can be used independently or combined. The `simple` alarm type only compares the current BG to `BG_` thresholds above, the `predict` alarm type uses highly tuned formula that forecasts where the BG is going based on it's trend. `predict` **DOES NOT** currently use any of the `BG_`* ENV's
* `BASE_URL` - Used for building links to your sites api, ie pushover callbacks, usually the URL of your Nightscout site you may want https instead of http
* `TREATMENTS_AUTH` (`off`) - possible values `on` or `off`. When on device must be authenticated by entering `API_SECRET` to create treatments


### Core
Expand Down
3 changes: 3 additions & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function config ( ) {
setMongo();
updateSettings();

// require authorization for entering treatments
env.treatments_auth = readENV('TREATMENTS_AUTH',false);

return env;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function create (env, ctx) {

app.set('title', [app.get('name'), 'API', app.get('version')].join(' '));

app.set('treatments_auth', env.treatments_auth);

// Start setting up routes
if (app.enabled('api')) {
// experiments
Expand Down
27 changes: 25 additions & 2 deletions lib/api/treatments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,40 @@ function configure (app, wares, ctx) {

function config_authed (app, api, wares, ctx) {

api.post('/treatments/', /*TODO: auth disabled for now, need to get login figured out... wares.verifyAuthorization, */ function(req, res) {
function post_response(req, res) {
var treatment = req.body;
ctx.treatments.create(treatment, function (err, created) {
if (err)
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
res.json(created);
});
}
if (app.treatments_auth)
api.post('/treatments/', wares.verifyAuthorization, post_response);
else
api.post('/treatments/', post_response);

api.delete('/treatments/:_id', wares.verifyAuthorization, function(req, res) {
ctx.treatments.remove(req.params._id, function ( ) {
res.json({ });
});
});

// update record
api.put('/treatments/', wares.verifyAuthorization, function(req, res) {
var data = req.body;
ctx.treatments.save(data, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error saving treatment');
console.log(err);
} else {
res.json(created);
console.log('Treatment saved');
}
});
});

}

if (app.enabled('api') && app.enabled('careportal')) {
Expand Down
34 changes: 27 additions & 7 deletions lib/treatments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
var find_options = require('./query');

function storage (env, ctx) {
var ObjectID = require('mongodb').ObjectID;

// allow regexp search
// /api/v1/treatments.json?find[notes]=/sometext/i
function find_query (opts) {
var reg;
['notes','eventType'].forEach(function(d) {
if (opts && opts.find && opts.find[d] && (reg=/\/(.*)\/(.*)/.exec(opts.find[d]))) {
opts.find[d] = new RegExp(reg[1],reg[2]);
}
});
return opts;
}

function create (obj, fn) {

Expand Down Expand Up @@ -41,13 +54,25 @@ function storage (env, ctx) {
.toArray(fn);
}

function remove (_id, fn) {
return api( ).remove({ "_id": new ObjectID(_id) }, fn);
}

function save (obj, fn) {
obj._id = new ObjectID(obj._id);
api().save(obj, fn);
}


function api ( ) {
return ctx.store.db.collection(env.treatments_collection);
}

api.list = list;
api.create = create;
api.indexedFields = ['created_at', 'eventType', 'insulin', 'carbs', 'glucose'];
api.indexedFields = ['created_at', 'eventType', 'insulin', 'carbs', 'glucose', 'boluscalc.foods._id', 'notes'];
api.remove = remove;
api.save = save;
return api;
}

Expand All @@ -59,11 +84,6 @@ function prepareData(obj) {
, preBolusCarbs: ''
};

obj.glucose = Number(obj.glucose);
obj.carbs = Number(obj.carbs);
obj.insulin = Number(obj.insulin);
obj.preBolus = Number(obj.preBolus);

var eventTime;
if (obj.eventTime) {
eventTime = new Date(obj.eventTime);
Expand Down Expand Up @@ -94,7 +114,7 @@ function prepareData(obj) {
deleteIfEmpty('notes');
deleteIfEmpty('preBolus');

if (obj.glucose === 0 || isNaN(obj.glucose)) {
if (!obj.glucose || obj.glucose === 0) {
delete obj.glucose;
delete obj.glucoseType;
delete obj.units;
Expand Down

0 comments on commit 257cb4a

Please sign in to comment.