-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
482 lines (406 loc) · 14.7 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
'use strict';
const { google } = require('googleapis'),
googleAPIGetAuth = require('./authorize.js'),
chalk = require('chalk'),
moment = require('moment-timezone');
/** Google Calendar Logger */
module.exports = class GoogleCalendarLogger {
/**
* Create a Google Calendar Logger instance
* @param {Object} options
* @param {String} options.credentialsPath Path to credentials.json (generate here: https://developers.google.com/calendar/quickstart/nodejs)
* @param {String} options.tokenPath Path to where token.json should be placed (including filename + .json)
* @param {String} options.calendar How much time can exist between logged activities, before the log gets interrupted.
* @param {Number} [options.minutesUntilInactivity=10] How much time can exist between logged activities, before the log gets interrupted.
* @param {Object} [options.strings={}] Strings overrides.
* @param {Boolean} [options.showLinks=false] Print links to events in the CLI?
*/
constructor (options) {
this.setDefaults();
const {
// Required
credentialsPath,
tokenPath,
calendar: calendarSummary,
// Optional
minutesUntilInactivity,
strings: stringsOverrides = {},
showLinks = false,
} = options;
this.setCredentialsPath(credentialsPath);
this.setTokenPath(tokenPath);
this.setCalendarSummary(calendarSummary);
this.setMinutesUntilInactivity(minutesUntilInactivity);
this.setStringsOverrides(stringsOverrides);
this.setShowLinks(showLinks);
// Init Google Calendar connection
this.initCalendarConnection();
}
/***********************************************
* Default setters
**********************************************/
setDefaults () {
this.minutesUntilInactivity = 10;
this.strings = this.getDefaultStrings();
}
getDefaultStrings () {
return {
activityStarted: projectName => `Started working on ${projectName}`,
activityInProgress: projectName => `Working on ${projectName}`,
activityConcluded: projectName => `Worked on ${projectName}`,
activityLogged: projectName => `Activity in ${projectName}`,
closedDueToInactivity: projectName => `(closed due to inactivity)`,
}
}
/***********************************************
* Option setters
**********************************************/
setCredentialsPath (credentialsPath) {
if (typeof credentialsPath === 'string' && credentialsPath !== '') {
this.credentialsPath = credentialsPath;
}
else {
throw new Error(`✗ Missing or incorrect value for required option 'credentialsPath'`);
}
}
setTokenPath (tokenPath) {
if (typeof tokenPath === 'string' && tokenPath !== '') {
this.tokenPath = tokenPath;
}
else {
throw new Error(`✗ Missing or incorrect value for required option 'tokenPath'`);
}
}
setCalendarSummary (calendarSummary) {
if (typeof calendarSummary === 'string' && calendarSummary !== '') {
this.calendarSummary = calendarSummary;
}
else {
throw new Error(`✗ Missing or incorrect value for required option 'calendar'`);
}
}
setMinutesUntilInactivity (minutes) {
if (typeof minutes === 'number' && minutes > 0) {
this.minutesUntilInactivity = minutes;
}
}
setStringsOverrides (stringsOverrides) {
if (stringsOverrides !== null && typeof stringsOverrides === 'object' && !Array.isArray(stringsOverrides)) {
this.strings = Object.assign(this.strings, stringsOverrides);
}
}
setShowLinks (showLinks) {
if (typeof showLinks === 'boolean') {
this.showLinks = showLinks;
}
}
/***********************************************
* Authorization & Google Calendar connection
**********************************************/
initCalendarConnection () {
this.calendarConnection = new Promise(async (resolve, reject) => {
try {
const auth = await googleAPIGetAuth(this.credentialsPath, this.tokenPath);
const googleCalendar = google.calendar({
version: 'v3',
auth
});
const calendar = await this.getOrCreateCalendar(googleCalendar);
this.calendarId = calendar.id;
resolve(googleCalendar);
}
catch (err) {
console.error(chalk.red(`✗ Could not establish connection with Google Calendar API.`));
reject(err);
}
});
}
async getOrCreateCalendar (googleCalendar) {
return new Promise((resolve, reject) => {
googleCalendar.calendarList.list({}, (err, response) => {
if (err) {
return reject(err);
}
const { calendarSummary } = this,
calendar = response.data.items.find(item => item.summary === calendarSummary);
// If cal exists, return it
if (calendar) {
console.log(chalk.green(`Found calendar “${calendarSummary}”.`)); // TODO: too verbose?
return resolve(calendar);
}
// If cal doesn't exist, create it
else {
console.log(chalk.blue(`Creating calendar “${calendarSummary}”.`));
const newCal = {
resource: {
summary: calendarSummary,
},
};
googleCalendar.calendars.insert(newCal, (err, response) => {
if (err) {
return reject(err);
}
console.log(chalk.green(`✔ Created calendar “${calendarSummary}”.`));
return resolve(response.data);
});
}
});
});
}
/***********************************************
* Helpers
**********************************************/
get msUntilInactivity () {
return this.minutesUntilInactivity * 1000 * 60;
}
getCurrentTime () {
return {
currentTime: new Date(),
timeZone: moment.tz.guess(),
};
}
/***********************************************
* Log methods
*
* These are the methods by people who
* install this module will actually use.
**********************************************/
/**
* Create a start event
*/
async logStart () {
const googleCalendar = await this.calendarConnection,
logName = this.getLogName(this.strings.activityStarted);
const {
timeZone,
currentTime: startTime,
} = this.getCurrentTime();
// Create starting event
const eventParams = {
calendarId: this.calendarId,
resource: {
summary: logName,
description: this.addToDescription(startTime, logName),
start: {
dateTime: startTime.toISOString(),
timeZone,
},
end: {
// Set initial duration of 1 second
dateTime: new Date(+startTime + 1000).toISOString(),
timeZone,
},
extendedProperties: {
private: {
completed: 'false',
},
},
},
};
return new Promise((resolve, reject) => {
googleCalendar.events.insert(eventParams, (err, response) => {
if (err) {
console.log(chalk.red(`✗ There was an error creating a start event.`));
return reject(err);
}
const linkOrDot = (this.showLinks === true) ? `: ${response.data.htmlLink}.` : '.';
console.log(chalk.green(`✔ Start event created${linkOrDot}`));
resolve();
});
});
}
/**
* Log activity
*/
async logActivity (activityDescription = this.strings.activityLogged) {
const googleCalendar = await this.calendarConnection,
logName = this.getLogName(this.strings.activityInProgress);
const {
currentTime: activityTime,
timeZone,
} = this.getCurrentTime();
// Get the latest incomplete work log event
const latestIncompleteLogEvent = await this.getLatestIncompleteLogEvent(activityTime, timeZone);
// Check for inactivity
const inactivityDetected = this.hasInactivity(latestIncompleteLogEvent, activityTime);
if (inactivityDetected) {
// End previous log first
await this.logEndDueToInactivity(latestIncompleteLogEvent, activityTime);
// Then start new, so we don't accidentally immediately end the newly started log
await this.logStart();
// Then log the activity after all
await this.logActivity(activityDescription);
}
else {
const patchParams = {
calendarId: this.calendarId,
eventId: latestIncompleteLogEvent.id,
resource: {
summary: logName,
description: this.addToDescription(activityTime, activityDescription, latestIncompleteLogEvent.description),
end: {
dateTime: activityTime.toISOString(),
timeZone,
},
extendedProperties: {
private: {
completed: 'false',
},
},
},
};
return new Promise((resolve, reject) => {
googleCalendar.events.patch(patchParams, (err, response) => {
if (err) {
console.log(chalk.red('✗ An error occured during updating the work log.'));
return reject(err);
}
const linkOrDot = (this.showLinks === true) ? `: ${response.data.htmlLink}.` : '.';
console.log(chalk.green(`✔ Work logged${linkOrDot}`));
resolve();
});
});
}
}
/**
* Conclude timelog.
*/
async logEnd () {
const googleCalendar = await this.calendarConnection,
logName = this.getLogName(this.strings.activityConcluded);;
const {
currentTime: endTime,
timeZone,
} = this.getCurrentTime();
const concludedLogName = this.getLogName(this.strings.activityConcluded);
// Get the latest incomplete work log event
const latestIncompleteLogEvent = await this.getLatestIncompleteLogEvent(endTime, timeZone);
// Check for inactivity
const inactivityDetected = this.hasInactivity(latestIncompleteLogEvent, endTime);
if (inactivityDetected) {
// End the previous log
await this.logEndDueToInactivity(latestIncompleteLogEvent, endTime);
}
else {
const patchParams = {
calendarId: this.calendarId,
eventId: latestIncompleteLogEvent.id,
resource: {
summary: logName,
description: this.addToDescription(endTime, concludedLogName, latestIncompleteLogEvent.description),
end: {
dateTime: endTime.toISOString(),
timeZone,
},
extendedProperties: {
private: {
completed: 'true',
},
},
}
};
return new Promise((resolve, reject) => {
googleCalendar.events.patch(patchParams, (err, response) => {
if (err) {
console.log(chalk.red('✗ An error occured during creating the completed work log.'));
return reject(err);
}
const linkOrDot = (this.showLinks === true) ? `: ${response.data.htmlLink}.` : '.';
console.log(chalk.green(`✔ Work logged${linkOrDot}`));
resolve();
});
});
}
}
async logEndDueToInactivity (latestIncompleteLogEvent, activityTime) {
console.log(chalk.blue(`Inactivity detected, ending previous log and creating a new one.`));
const googleCalendar = await this.calendarConnection,
concludedLogName = this.getLogName(this.strings.activityConcluded),
closedDueToInactivity = this.getLogName(this.strings.closedDueToInactivity);
const patchParams = {
calendarId: this.calendarId,
eventId: latestIncompleteLogEvent.id,
resource: {
summary: concludedLogName,
description: this.addToDescription(activityTime, `${concludedLogName} ${closedDueToInactivity}`, latestIncompleteLogEvent.description),
extendedProperties: {
private: {
completed: 'true',
},
},
},
};
return new Promise((resolve, reject) => {
googleCalendar.events.patch(patchParams, (err, response) => {
if (err) {
console.log(chalk.red('✗ An error occured during creating the completed work log.'));
return reject(err);
}
const linkOrDot = (this.showLinks === true) ? `: ${response.data.htmlLink}.` : '.';
console.log(chalk.green(`✔ Work logged${linkOrDot}`));
resolve();
});
});
}
async getLatestIncompleteLogEvent (currentTime, timeZone) {
const googleCalendar = await this.calendarConnection;
const listParams = {
calendarId: this.calendarId,
// Google Calendar API doesn't support listing in descending order,
// we have to specify timeMin and timeMax instead and reverse order later.
// Assuming you didn't start working more than 1 week ago, this should work:
timeMin: new Date(+currentTime - 1000 * 60 * 60 * 24 * 7).toISOString(),
timeMax: currentTime.toISOString(),
timeZone,
singleEvents: true,
orderBy: 'startTime',
};
// Get the latest incomplete work log
return new Promise((resolve, reject) => {
googleCalendar.events.list(listParams, (err, response) => {
if (err) {
console.log(chalk.red('✗ An error occured during listing events.'));
return reject(err);
}
const events = response.data.items;
if (events.length) {
// Find the latest incomplete log
const latestIncompleteLogEvent = events.reverse().find((event) => {
try {
const { completed } = event.extendedProperties.private;
return completed === 'false';
} catch (err) {}
});
if (latestIncompleteLogEvent) {
resolve(latestIncompleteLogEvent);
}
else {
console.log(chalk.red(`✗ Couldn't create log.`));
reject(new Error(`No latest incomplete event found in the past 7 days.`));
}
}
else {
console.log(chalk.red(`✗ Couldn't create log.`));
reject(new Error(`Found no events in the past 7 days.`));
}
});
});
}
addToDescription (date, activity, description = '') {
// If not empty description, add newline
if (description !== '') description += `\n`;
const hh = `0${date.getHours()}`.slice(-2),
mm = `0${date.getMinutes()}`.slice(-2);
// Add the new activity
description += `${hh}:${mm} – ${activity}`;
return description;
}
getLogName (logName) {
return (typeof logName === 'function') ? logName(this.calendarSummary) : logName;
}
hasInactivity (latestIncompleteLogEvent, currentTime) {
// Check for inactivity
const lastActivity = +new Date(latestIncompleteLogEvent.updated || latestIncompleteLogEvent.created);
return (+currentTime - lastActivity) > this.msUntilInactivity;
}
}