Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Treatment, Entry and Device Status object dates to be all in UTC Strings #4658

Merged
merged 11 commits into from
Jul 17, 2019
Merged
25 changes: 23 additions & 2 deletions lib/server/treatments.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,31 @@ function storage (env, ctx) {

function prepareData(obj) {

// Convert all dates to UTC dates

let d = new Date();

try {
if (obj.created_at) d = new Date(obj.created_at);
} catch (error) {
console.error(error);
sulkaharo marked this conversation as resolved.
Show resolved Hide resolved
}

// Normalize all dates to UTC ISO strings
obj.created_at = d.toISOString();

var results = {
//TODO: validate format of created_at
created_at: obj.created_at || new Date().toISOString()
created_at: obj.created_at
, preBolusCarbs: ''
};

const offset = d.getTimezoneOffset();

if (offset) {
obj.timeZoneOffset = offset;
results.offset = offset;
}

obj.glucose = Number(obj.glucose);
obj.targetTop = Number(obj.targetTop);
obj.targetBottom = Number(obj.targetBottom);
Expand Down Expand Up @@ -210,6 +229,8 @@ function prepareData(obj) {
delete obj.units;
}

console.log(obj);

return results;
}

Expand Down
41 changes: 40 additions & 1 deletion tests/api.treatments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _ = require('lodash');
var request = require('supertest');
var should = require('should');
var language = require('../lib/language')();
var _moment = require('moment');

describe('Treatment API', function ( ) {
this.timeout(10000);
Expand Down Expand Up @@ -60,6 +61,43 @@ describe('Treatment API', function ( ) {
});
});


it('post single treatments in zoned time format', function (done) {

var current_time = Date.now();
console.log('Testing date with local format: ', _moment(current_time).format("YYYY-MM-DDTHH:mm:ss.SSSZZ"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use moment.toISOString() instead of the format, in a few places

Copy link
Member Author

@sulkaharo sulkaharo May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasoncalabrese Note the formats are intentionally different in order to test inserting a zoned time, so changing those to toISOString() would break the test


self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
.set('api-secret', self.env.api_secret || '')
.send({eventType: 'Meal Bolus', created_at: _moment(current_time).format("YYYY-MM-DDTHH:mm:ss.SSSZZ"), carbs: '30', insulin: '2.00', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
console.log(sorted);
sorted.length.should.equal(1);
sorted[0].glucose.should.equal(100);
should.not.exist(sorted[0].eventTime);
sorted[0].insulin.should.equal(2);
sorted[0].carbs.should.equal(30);
var zonedTime = _moment(current_time).utc().format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z";
sorted[0].created_at.should.equal(zonedTime);
done();
});
}
});

});
});


it('post a treatment array', function (done) {
self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
Expand Down Expand Up @@ -101,7 +139,7 @@ describe('Treatment API', function ( ) {
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'}
, {eventType: 'Meal Bolus', created_at: now, carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'}
])
.expect(200)
.end(function (err) {
Expand All @@ -125,6 +163,7 @@ describe('Treatment API', function ( ) {
});
});
});

it('post a treatment, query, delete, verify gone', function (done) {
// insert a treatment - needs to be unique from example data
console.log('Inserting treatment entry');
Expand Down