Skip to content

Commit

Permalink
* Allow users to set DE_NORMALIZE_DATES=false to have the REST API …
Browse files Browse the repository at this point in the history
…denormalize UTC dates to zoned ISO dates in the API (#4826)
  • Loading branch information
sulkaharo authored Jul 29, 2019
1 parent 7947e30 commit 35c7dc4
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 260 deletions.
36 changes: 25 additions & 11 deletions lib/api/devicestatus/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict';

var consts = require('../../constants');
const consts = require('../../constants');
const moment = require('moment');

function configure (app, wares, ctx) {
var express = require('express'),
api = express.Router( );
function configure (app, wares, ctx, env) {
var express = require('express')
, api = express.Router();

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
api.use(wares.bodyParser.raw());
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
api.use(wares.bodyParser.json());
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

Expand All @@ -23,7 +24,21 @@ function configure (app, wares, ctx) {
if (!q.count) {
q.count = 10;
}
ctx.devicestatus.list(q, function (err, results) {

ctx.devicestatus.list(q, function(err, results) {

// Support date de-normalization for older clients
if (env.settings.deNormalizeDates) {
results.forEach(function(e) {
// eslint-disable-next-line no-prototype-builtins
if (e.created_at && e.hasOwnProperty('utcOffset')) {
const d = moment(e.created_at).utcOffset(e.utcOffset);
e.created_at = d.toISOString(true);
delete e.utcOffset;
}
});
}

return res.json(results);
});
});
Expand All @@ -32,7 +47,7 @@ function configure (app, wares, ctx) {

function doPost (req, res) {
var obj = req.body;
ctx.devicestatus.create(obj, function (err, created) {
ctx.devicestatus.create(obj, function(err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
} else {
Expand All @@ -48,7 +63,7 @@ function configure (app, wares, ctx) {
* Delete devicestatus. The query logic works the same way as find/list. This
* endpoint uses same search logic to remove records from the database.
*/
function delete_records(req, res, next) {
function delete_records (req, res, next) {
var query = req.query;
if (!query.count) {
query.count = 10
Expand Down Expand Up @@ -91,12 +106,11 @@ function configure (app, wares, ctx) {
api.delete('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:delete'), delete_records);
}

if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/ ) {
config_authed(app, api, wares, ctx);
}

return api;
}

module.exports = configure;

Loading

0 comments on commit 35c7dc4

Please sign in to comment.