-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·96 lines (69 loc) · 2.09 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
#!/usr/bin/env node
// Required packages
const got = require('got');
const ical = require('node-ical');
const DateDiff = require('date-diff');
const CachemanFile = require('cacheman-file');
const difference = require('lodash.difference');
const argv = require('mri')(process.argv.slice(2));
// Arguments check
if (! argv['personio-ics'] || ! argv['slack-webhook']) {
console.error('Endpoints needed');
process.exit(1);
}
// Today values
const todayKey = new Date().toISOString().split('T')[0];
const todayDate = Date.parse(todayKey);
// Cache init
const cache = new CachemanFile({tmpDir: './cache'});
const cacheKey = todayKey;
const cacheTTL = 60 * 60 * 24;
// Absences pool
let absences = [];
// Request ICS
ical.fromURL(argv['personio-ics'], {}, (error, data) => {
// Exit on error
if (error) {
console.log(error);
process.exit(1);
}
// Loop events
Object.values(data).map(event => {
const eventStart = Date.parse(event.start);
const eventEnd = Date.parse(event.end);
const diffTime = todayDate <= eventEnd && todayDate >= eventStart;
if (! diffTime) {
return;
}
const diffDays = Math.ceil((new DateDiff(eventEnd, todayDate)).days());
if (diffDays > 31) {
return;
}
const absentPerson = event.summary.replace(/^\[.+\]\s/, '');
absences.push(`>${absentPerson}`);
});
// Exit if empty
if (! absences.length) {
console.log('No crowd');
process.exit(1);
}
// Sync cache
cache.get(cacheKey, (error, value) => {
if (error) {
console.log('Cacheman error');
process.exit(1);
}
const diffAbsences = difference(absences, value);
if (! diffAbsences.length) {
return;
}
cache.set(cacheKey, absences, cacheTTL, () => {
got(argv['slack-webhook'], {
method: 'POST',
body: JSON.stringify({
'text': '\n' + diffAbsences.sort().join('\n')
})
});
});
})
});