forked from funsocietyirc/MMM-Wunderlist-Enhanced
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanydotest.js
More file actions
111 lines (96 loc) · 2.78 KB
/
anydotest.js
File metadata and controls
111 lines (96 loc) · 2.78 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
const Api = require('anydo-api');
require('dotenv').config()
const api = new Api();
if(process.env.TOKEN) {
console.log("Using token from .env file");
api.setToken({token: process.env.TOKEN});
} else {
console.log("Logging in to obtain token");
api.login({email: process.env.EMAIL, password: process.env.PASSWORD})
.then(loginPromise => {
console.log(`Your API token is: ${api.authToken}`);
console.log();
console.log(` echo TOKEN="${api.authToken}" >> .env`);
console.log();
});
}
class ResultSet {
constructor(res) {
this.res = res;
this.models = this.res.models;
}
categoryMap(){
const categoryMap = {};
this.models.category.items.forEach(category => {
categoryMap[category.id] = category;
});
return categoryMap;
}
tasksByCategory(){
const categoryMap = this.categoryMap();
const itemCategoryMap = {};
this.models.task.items.forEach(item => {
//console.log(item.subTasks.length);
if(item.parentGlobalTaskId === null && item.status == "UNCHECKED"){
//console.log(item.title, item.categoryId);
if(item.categoryId in itemCategoryMap){
itemCategoryMap[item.categoryId].items.push(item);
} else {
itemCategoryMap[item.categoryId] = {
category: categoryMap[item.categoryId],
items: new Array(item),
};
}
} else {
// this is a child item or done
// console.log(item);
}
});
return itemCategoryMap;
}
scheduledTasks(){
const hasDue = [];
this.models.task.items.forEach(item => {
if(item.dueDate != null && item.status == "UNCHECKED"){
//console.log(item);
hasDue.push(item);
}
});
return hasDue;
}
dueTasks(dueDate=null){
if(dueDate == null){
dueDate = new Date();
}
const dueTasks = this.scheduledTasks();
const due = [];
dueTasks.forEach(item => {
const myDueDate = new Date(item.dueDate);
//console.log(dueDate);
//console.log(item.dueDate);
if(myDueDate <= dueDate){
item.dueDateObj = myDueDate;
due.push(item);
}
});
return due;
}
}
api.sync()
.then(res => {
const rs = new ResultSet(res);
const itemCategoryMap = rs.tasksByCategory();
Object.keys(itemCategoryMap).forEach(key => {
var category = itemCategoryMap[key];
console.log(`${category.category.name} has ${category.items.length} items`);
});
const hasDue = rs.scheduledTasks();
console.log(`There are ${rs.models.task.items.length} items and ${hasDue.length} items with due dates`);
const rightNow = new Date();
const dueToday = rs.dueTasks(rightNow);
console.log(`There are ${dueToday.length} items due today`);
dueToday.forEach(item => {
console.log(item.title);
});
});