-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (70 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var express = require('express');
var session = require('express-session');
var Grant = require('grant-express');
var _ = require('lodash');
var pug = require('pug');
var SyncTasks = require('synctasks');
var WunderlistSDK = require('wunderlist');
var config = require('./config.json');
function Finito(access_token) {
this.wunderlistApi = new WunderlistSDK({
'accessToken': access_token,
'clientID': config.wunderlist.key
});
}
Finito.prototype.getAllLists = function() {
return this.wunderlistApi.http.lists.all().done(lists => {
console.log('Got ' + lists.length + ' list(s)!');
return lists;
})
.fail(() => {
console.log('Error getting lists.');
});
}
Finito.prototype.getAllCompletedTasksFromLists = function(lists) {
var promises = [];
var tasks = [];
var promises = _.map(lists, list => {
return this.wunderlistApi.http.tasks.forList(list.id, true /* completed */).done(completedTasks => {
_.each(completedTasks, task => task['list_title'] = list.title);
tasks.push(...completedTasks);
}).fail(() => {
console.log('Error getting tasks for list: ' + list.id);
});
});
return SyncTasks.all(promises).then(() => {
return tasks;
}).fail(() => {
console.log('Error getting tasks.');
});
}
Finito.prototype.filterForCompletedInTheLastTwentyFourHours = function(tasks) {
return _.filter(tasks, task => {
var completedDate = new Date(task.completed_at);
var twentyFourHoursInMilisec = 24 * 60 * 60 * 1000;
var twentyFourHoursAgoTimeStamp = Date.now() - twentyFourHoursInMilisec;
return completedDate.getTime() > twentyFourHoursAgoTimeStamp;
});
}
var app = express();
app.locals._ = require('lodash');
app.set('view engine', 'pug');
var grant = new Grant(require('./config.json'));
app.use(session({secret:'very secret'}));
app.use(grant);
app.get('/', function (req, res) {
res.render('index', { title: 'Finito', header: "Login to view your completed tasks" });
});
app.get('/wunderlistCallback', function (req, res) {
var finito = new Finito(req.query.access_token);
finito.getAllLists().then(lists => {
finito.getAllCompletedTasksFromLists(lists).then(tasks => {
res.render('tasklist', { title: 'Finito', tasks: _.sortBy(tasks, task => -1 * (new Date(task.completed_at)).getTime()) });
});
}).fail(() => {
res.end('Something went wrong.');
});
});
app.listen(process.env.port || 3000, function () {
console.log('Server is running...');
});