-
Notifications
You must be signed in to change notification settings - Fork 1
/
clickup.js
218 lines (182 loc) · 5.33 KB
/
clickup.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* A fluent API to fetch clickup data to local memory.
* USAGE:
* $clickup
* .notify(callback)
* .auth('key')
* .team('Different')
* .space('Product - 2020')
* .folder('TDTU')
* .list('Apr 27 - May 08')
* .tasks();
* console.log($clickup.current.tasks);
*/
function $clickup() {
return this;
}
$clickup.notify = function(callback) {
$clickup._notify = callback;
return $clickup;
};
$clickup.auth = function(key) {
$clickup.base = 'https://api.clickup.com/api/v2';
$clickup.headers = {
Authorization: key,
method: 'get',
contentType :
'application/json; charset=utf-8'
};
$clickup.current = {};
return $clickup;
}
$clickup._fetch = function(path) {
$clickup._notify({message:'fetching ' + path});
var data = $http($clickup.base + path, $clickup.headers).data('json');
if ($http.error()) {
$clickup._notify($http.error());
return null;
}
return data;
}
$clickup.team = function(name) {
$clickup.current.team = null;
$clickup.current.space = null;
$clickup.current.folder = null;
$clickup.current.list = null;
$clickup.current.tasks = null;
$clickup.current.error = null;
$clickup._fetch('/team').teams.forEach(function(team) {
if (team.name === name) {
$clickup.current.team = team;
}
});
if ($clickup.current.team == null) {
$clickup.current.error = 'Team ' + name + ' could not be set';
}
return $clickup;
}
$clickup.space = function(name) {
$clickup.current.space = null;
$clickup.current.folder = null;
$clickup.current.list = null;
$clickup.current.tasks = null;
$clickup.current.error = null;
$clickup._fetch('/team/' + $clickup.current.team.id + '/space').spaces.forEach(function(space) {
if (space.name === name) {
$clickup.current.space = space;
}
});
if ($clickup.current.space == null) {
$clickup.current.error = 'Space ' + name + ' could not be set';
}
return $clickup;
}
$clickup.folder = function(name) {
$clickup.current.folder = null;
$clickup.current.list = null;
$clickup.current.tasks = null;
$clickup.current.error = null;
var data = $clickup._fetch('/space/' + $clickup.current.space.id + '/folder');
data.folders.forEach(function(folder) {
if (folder.name === name) {
$clickup.current.folder = folder;
}
});
if ($clickup.current.folder == null) {
$clickup.current.error = 'Folder ' + name + ' could not be set';
}
return $clickup;
}
$clickup.list = function(name) {
$clickup.current.list = null;
$clickup.current.tasks = null;
$clickup.current.error = null;
$clickup._fetch('/folder/' + $clickup.current.folder.id + '/list').lists.forEach(function(list) {
if (list.name === name) {
$clickup.current.list = list;
}
});
if ($clickup.current.list == null) {
$clickup.current.error = 'List ' + name + ' could not be set';
$clickup._notify({error: true, message:'Failed to fetch list ' + name});
} else {
$clickup._notify({message:'Fetched list: ' + name + ' = ' + $clickup.current.list.id});
}
return $clickup;
}
$clickup.tasks = function(options = {}) {
$clickup.current.tasks = null;
$clickup.current.error = null;
var query = '?';
var allowedOptions = {
'subtasks': false,
'include_closed': false
};
for (option in options) {
if (option in allowedOptions) {
query += (option + '=' + options[option] + '&');
}
}
var page = 0, limit = 100, data = null;
$clickup.current.tasks = [];
do {
data = $clickup._fetch('/list/' + $clickup.current.list.id + '/task' + query + 'page=' + page++);
$clickup.current.tasks = $clickup.current.tasks.concat(data.tasks);
} while (data.tasks.length == limit);
return $clickup;
}
$clickup.calc = {};
$clickup.util = {};
$clickup.util.hasTag = function(task, tag) {
for (var i in task.tags) {
if (task.tags[i].name.toUpperCase() === tag)
return true;
}
return false;
};
$clickup.util.hasAnyTag = function(task, tags) {
for (var i in tags) {
if ($clickup.util.hasTag(task, tags[i])) {
return true;
}
}
return false;
};
$clickup.calc.plannedEffort = function(tasks, startDate) {
var effort = 0;
var dayAfterStartDate = startDate.getTime() + (3600 * 1000 * 24);
tasks.forEach(function(task) {
if (task.date_created < dayAfterStartDate) {
effort += (task.time_estimate / 3600000);
}
});
return effort;
};
$clickup.calc.actualEffort = function(tasks, doneStates) {
var effort = 0;
tasks.forEach(function(task) {
if (doneStates.includes(task.status.status.toUpperCase())) {
effort += (task.time_estimate / 3600000);
}
});
return effort;
};
$clickup.calc.unplannedEffort = function(tasks, startDate, doneStates) {
var effort = 0;
var dayAfterStartDate = startDate.getTime() + (3600 * 1000 * 24);
tasks.forEach(function(task) {
if ((task.date_created > dayAfterStartDate) && (doneStates.includes(task.status.status.toUpperCase()))) {
effort += (task.time_estimate / 3600000);
}
});
return effort;
};
$clickup.calc.reworkEffort = function(tasks, reworkTags, doneStates) {
var effort = 0;
tasks.forEach(function(task) {
if ($clickup.util.hasAnyTag(task, reworkTags) && (doneStates.includes(task.status.status.toUpperCase()))) {
effort += (task.time_estimate / 3600000);
}
});
return effort;
};