Skip to content

Commit 16bbecb

Browse files
committed
Add /export route, which exports a user's data in JSON format. Fixes #3
1 parent 5701894 commit 16bbecb

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

apps/trolls_goals/models/area.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ module.exports = (function() {
66
, Schema = mongoose.Schema
77
, ObjectId = Schema.ObjectId
88

9-
, getToday = function() {
10-
return Math.floor(new Date().getTime() / 86400000);
9+
, NUM_MS_IN_DAY = 86400000
10+
, dateToDayNum = function(date) {
11+
return Math.floor(date.getTime() / NUM_MS_IN_DAY);
12+
}
13+
, dayNumToDate = function(day_num) {
14+
return new Date(day_num * NUM_MS_IN_DAY);
1115
}
1216

1317
, AreaSchema = new Schema({
@@ -16,11 +20,14 @@ module.exports = (function() {
1620
, domain: { type: ObjectId, ref: 'domains', index: true }
1721
, user: { type: ObjectId, ref: 'users', index: true }
1822
, records: { type: Schema.Types.Mixed, default: function() { return {}; } } // Date: number of records
19-
, start_day: { type: Number, default: getToday }
23+
, start_day: { type: Number, default: function() { return dateToDayNum(new Date()); } }
2024
, prompt_for_details: Boolean
2125
, hidden: { type: Boolean } // whether this domain should be retrieved under normal circumstances
2226
});
2327

28+
AreaSchema.statics.dateToDayNum = dateToDayNum;
29+
AreaSchema.statics.dayNumToDate = dayNumToDate;
30+
2431
var Area = mongoose.model('Area', AreaSchema);
2532

2633
Area.update_fields = ['name', 'description', 'domain', 'prompt_for_details', 'hidden'];

apps/trolls_goals/routes.js

+73
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = function(app) {
22
var fs = require('fs')
33
, _ = require('underscore')
4+
, async = require('async')
45
, Record = require('./models/record')
56
, Area = require('./models/area')
67
, Domain = require('./models/domain');
@@ -38,6 +39,78 @@ module.exports = function(app) {
3839
.end();
3940
});
4041

42+
app.get('/export', function(req, res) {
43+
var query = { user: req.user._id };
44+
async.parallel({
45+
records: function findRecords(acb) {
46+
var select = 'day details area';
47+
Record.find(query, select, function(find_err, records) {
48+
if (find_err) {
49+
return acb(find_err);
50+
}
51+
records = _.groupBy(records, 'area');
52+
records = _.mapObject(records, function(area_records) {
53+
return _.sortBy(area_records, 'name');
54+
});
55+
acb(find_err, records);
56+
});
57+
},
58+
areas: function findAreas(acb) {
59+
var select = 'name description start_day domain';
60+
Area.find(query, select, function(find_err, areas) {
61+
if (find_err) {
62+
return acb(find_err);
63+
}
64+
areas = _.groupBy(areas, 'domain');
65+
areas = _.mapObject(areas, function(domain_areas) {
66+
return _.sortBy(domain_areas, 'name');
67+
});
68+
acb(find_err, areas);
69+
});
70+
},
71+
domains: function findDomains(acb) {
72+
var select = 'name description';
73+
Domain.find(query, select, function(find_err, domains) {
74+
if (find_err) {
75+
return acb(find_err);
76+
}
77+
domains = _.sortBy(domains, 'name');
78+
acb(find_err, domains);
79+
});
80+
}
81+
}, function(find_err, results) {
82+
if (find_err) {
83+
return res.error(find_err);
84+
}
85+
var records;
86+
var areas;
87+
var domains = _.map(results.domains, function(domain) {
88+
areas = _.map(results.areas[domain._id], function(area) {
89+
records = _.map(results.records[area._id], function(record) {
90+
return record.toObject({ transform: function(doc, ret, options) {
91+
delete ret._id;
92+
delete ret.area;
93+
ret.date_string = Area.dayNumToDate(ret.day).toUTCString();
94+
}});
95+
});
96+
return area.toObject({ transform: function(doc, ret, options) {
97+
delete ret._id;
98+
delete ret.domain;
99+
ret.records = records;
100+
}});
101+
});
102+
return domain.toObject({ transform: function(doc, ret, options) {
103+
delete ret._id;
104+
ret.areas = areas;
105+
}});
106+
});
107+
res.json({
108+
user: { username: req.user.username, _id: req.user._id }
109+
, domains: domains
110+
});
111+
})
112+
});
113+
41114
var models = {
42115
record: Record
43116
, area: Area

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rpwm-server",
3-
"version": "1.0.1-43",
3+
"version": "1.0.1-45",
44
"private": true,
55
"dependencies": {
66
"async": "latest",

0 commit comments

Comments
 (0)