data export commit, supports exporting task history via csv

*adds the following new dependencies: moment, express-csv
*recommend replacing functionality of 'relative-date' with features from 'moment'
*supports retrieval of CSV history via the API and in browser
**/api/v1/export/history (requires typical API authorization)
**/export/history.csv (requires session authorization)
*adds new routes for data export
This commit is contained in:
Nick Gordon
2013-11-15 06:18:16 -08:00
parent 528fd5f067
commit 7bf1bf2af0
7 changed files with 78 additions and 1 deletions
+51
View File
@@ -0,0 +1,51 @@
var _ = require('lodash');
var csv = require('express-csv');
var User = require('../models/user').model;
var nconf = require('nconf');
var moment = require('moment');
var dataexport = module.exports;
/*
------------------------------------------------------------------------
Data export
------------------------------------------------------------------------
*/
dataexport.history = function(req, res) {
var user = res.locals.user;
var output = [
["Task Name", "Task ID", "Task Type", "Date", "Value"]
];
_.each(user.tasks, function(task) {
_.each(task.history, function(history) {
output.push(
[task.text, task.id, task.type, moment(history.date).format("MM-DD-YYYY HH:mm:ss"), history.value]
);
});
});
return res.csv(output);
}
dataexport.auth = function(req, res, next) { //[todo] there is probably a more elegant way of doing this...
var uid;
uid = req.session.userId;
if (!(req.session && req.session.userId)) {
return res.json(401, "You must be logged in.");
}
return User.findOne({
_id: uid,
}, function(err, user) {
if (err) {
return res.json(500, {
err: err
});
}
if (_.isEmpty(user)) {
return res.json(401, "No user found.");
}
res.locals.user = user;
return next();
});
};