Files
cgm-remote-monitor/lib/api/treatments/index.js
T
2017-01-02 00:12:28 +02:00

130 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
var _ = require('lodash');
var consts = require('../../constants');
function configure(app, wares, ctx) {
var express = require('express')
, api = express.Router();
api.use(wares.compression());
api.use(wares.bodyParser({
limit: 1048576 * 50
}));
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw({
limit: 1048576
}));
// json body types get handled as parsed json
api.use(wares.bodyParser.json({
limit: 1048576
}));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({
limit: 1048576
, extended: true
}));
// invoke common middleware
api.use(wares.sendJSONStatus);
api.use(ctx.authorization.isPermitted('api:treatments:read'));
// List treatments available
api.get('/treatments', function(req, res) {
var ifModifiedSince = req.get('If-Modified-Since');
ctx.treatments.list(req.query, function(err, results) {
var d1 = null;
_.forEach(results, function clean(t) {
t.carbs = Number(t.carbs);
t.insulin = Number(t.insulin);
var d2 = new Date(t.timestamp);
if (d1 == null || d2 > d1) {
d1 = d2;
}
});
if (!_.isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString());
if (ifModifiedSince && d1 <= new Date(ifModifiedSince)) {
res.status(304).send({
status: 304
, message: 'Not modified'
, type: 'internal'
});
return;
} else {
return res.json(results);
}
});
});
function config_authed(app, api, wares, ctx) {
function post_response(req, res) {
var treatments = req.body;
if (!_.isArray(treatments)) {
treatments = [treatments];
};
var insertTreatments = [];
_.forEach(treatments, function checkRequireFields(treatment) {
if (!_.isNil(treatment.timestamp) && !_.isNil(treatment.eventType)) {
insertTreatments.push(treatment);
}
});
if (insertTreatments.length == 0) {
return res.sendJSONStatus(res, 200, '200 OK - No valid treatments found, skipping insert');
//return res.sendJSONStatus(res, 409, '409 Conflict - No valid treatments found');
}
ctx.treatments.create(insertTreatments, function(err, created) {
if (err) {
console.log('Error adding treatment', err);
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
} else {
console.log('Treatment created');
res.json(created);
}
});
}
api.post('/treatments/', wares.bodyParser({
limit: 1048576 * 50
}), ctx.authorization.isPermitted('api:treatments:create'), post_response);
api.delete('/treatments/:_id', ctx.authorization.isPermitted('api:treatments:delete'), function(req, res) {
ctx.treatments.remove(req.params._id, function() {
res.json({});
});
});
// update record
api.put('/treatments/', ctx.authorization.isPermitted('api:treatments:update'), 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', data);
}
});
});
}
if (app.enabled('api') && app.enabled('careportal')) {
config_authed(app, api, wares, ctx);
}
return api;
}
module.exports = configure;