-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoist.js
157 lines (136 loc) · 3.12 KB
/
todoist.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
const _ = require("lodash");
const baseUrl = "https://api.todoist.com/rest/v2";
const axios = require("axios");
const REPEAT_WEEKDAYS = {
su: false,
m: true,
t: true,
w: true,
th: true,
f: true,
s: false,
};
module.exports = class Todoist {
constructor(myAxios, logger) {
this.axios = myAxios;
this.logger = logger;
}
static from(apiToken, logger = console) {
const headers = {
Authorization: "Bearer " + apiToken,
};
const baseURL = "https://api.todoist.com";
return new Todoist(axios.create({ baseURL: baseUrl, headers }), logger);
}
createTask(item) {
return this.axios.post("/tasks", item);
}
deleteTask(id) {
return this.axios.delete(`${baseUrl}/tasks/${id}`);
}
deleteAllTasks() {
return this.listTasks().then((items) =>
Promise.all(items.map((i) => this.deleteTask(i.id)))
);
}
getStats() {
return this.axios
.get("https://api.todoist.com/sync/v9/completed/get_stats")
.then((r) => r.data);
}
getTask(taskId) {
return this.axios.get(`${baseUrl}/tasks/${taskId}`).then((r) => r.data);
}
isTaskRecurring(task) {
return _.get(task, "due.is_recurring", false);
}
listProjects() {
return this.axios.get(`${baseUrl}/projects`).then((r) => r.data);
}
listTasks() {
return this.axios.get(`${baseUrl}/tasks`).then((r) => {
this.logger.info("Done fetching all todoist tasks");
return r.data;
});
}
sync(token) {
const url =
'https://api.todoist.com/sync/v9/sync?resource_types=["all"]&sync_token=' +
token;
this.logger.info("Sync Token:", token);
return this.axios.get(url).then((r) => r.data);
}
calculateFrequency(dateExpr) {
if (dateExpr.includes("weekday")) {
return {
frequency: "weekly",
repeat: REPEAT_WEEKDAYS,
};
}
if (dateExpr === "every week" || dateExpr === "weekly") {
return {
frequency: "weekly",
};
}
if (dateExpr.startsWith("every other")) {
const dayExpr = dateExpr.substr(12).trim();
return {
everyX: 2,
frequency: "weekly",
repeat: this.getRepeat(this.getDay(dayExpr)),
};
}
if (dateExpr === "every month" || dateExpr === "monthly") {
return {
frequency: "monthly",
};
}
if (dateExpr === "every year" || dateExpr === "yearly") {
return {
frequency: "yearly",
};
}
return {
frequency: "daily",
};
}
getDay(dayExpr) {
switch (_.toLower(dayExpr)) {
case "monday":
case "mon":
case "m":
return "m";
case "tuesday":
case "tues":
case "tue":
case "t":
return "t";
case "wednesday":
case "wed":
case "w":
return "w";
case "thursday":
case "thurs":
case "th":
return "th";
case "friday":
case "fri":
case "f":
return "f";
}
}
getRepeat(day) {
const repeat = {
su: false,
m: false,
t: false,
w: false,
th: false,
f: false,
s: false,
};
repeat[day] = true;
return repeat;
}
};