Skip to content

Commit

Permalink
clean up prepReqModel and add delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Aug 25, 2015
1 parent a511ebe commit 269ba27
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
53 changes: 17 additions & 36 deletions lib/api/entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ var consts = require('../../constants');
var es = require('event-stream');
var sgvdata = require('sgvdata');

var ID_PATTERN = /^[a-f\d]{24}$/;
function isId (value) {
//TODO: why did we need tht length check?
return value && ID_PATTERN.test(value) && value.length === 24;
}

/**********\
* Entries
\**********/
Expand All @@ -13,6 +19,11 @@ function configure (app, wares, ctx) {
api = express.Router( )
;

var specialModels = {
treatments: ctx.treatments
, devicestatus: ctx.devicestatus
};

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
Expand Down Expand Up @@ -123,39 +134,11 @@ function configure (app, wares, ctx) {
}

function prepReqModel(req, model) {
var find = { };
switch (model) {
case 'treatments':
case 'devicestatus':
//TODO: profile not working now, maybe profiles are special
//case 'profile':
req.model = ctx[model];
break;
var find = {
type: model || 'sgv'
};

case 'meter':
case 'mbg':
find.type = 'mbg';
req.model = ctx.entries;
break;
case 'cal':
find.type = 'cal';
req.model = ctx.entries;
break;
case 'sensor':
find.type = 'sensor';
req.model = ctx.entries;
break;
case 'sgv':
find.type = 'sgv';
req.model = ctx.entries;
break;
default:
if (model && model.length > 0) {
find.type = model;
}
req.model = ctx.entries;
break;
}
req.model = specialModels[model] || ctx.entries;

if (!req.query.find) {
req.query.find = find;
Expand All @@ -181,8 +164,7 @@ function configure (app, wares, ctx) {

// Fetch one entry by id
api.get('/entries/:id', function(req, res, next) {
var ID_PATTERN = /^[a-f\d]{24}$/;
if (ID_PATTERN.test(req.params.id) && req.params.id.length === 24) {
if (isId(req.params.id)) {
//assume sgv
req.params.model = 'sgv';
entries.getEntry(req.params.id, function(err, entry) {
Expand Down Expand Up @@ -217,8 +199,7 @@ function configure (app, wares, ctx) {
}

api.delete('/entries/:id', function(req, res, next) {
var ID_PATTERN = /^[a-f\d]{24}$/;
if (ID_PATTERN.test(req.params.id) && req.params.id.length === 24) {
if (isId(req.params.id)) {
//assume sgv
req.params.model = 'sgv';
prepReqModel(req, req.params.model);
Expand Down
23 changes: 23 additions & 0 deletions tests/api.entries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,27 @@ describe('Entries REST api', function ( ) {
done();
});
});

it('allow deletes', function (done) {
var app = this.app;

request(app)
.delete('/entries/sgv?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
request(app)
.get('/entries/sgv.json?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(0);
done();
});
}
});
});


});

0 comments on commit 269ba27

Please sign in to comment.