-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (164 loc) · 3.97 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
require('dotenv').config()
var https = require('https');
var slack = require('slack-incoming-webhook');
var send = slack({
url: process.env.SLACK_WEBHOOK_URL,
});
var moment = require('moment');
var now = moment();
var timeOff;
var people;
var message = '';
function isAWeekDay(callback) {
var weekday = moment().isoWeekday();
if(weekday < 6) {
callback();
}
else {
console.log('It\'s the weekend!');
}
}
function getTimeOff(callback) {
var options = {
host: 'api.float.com',
port: 443,
path: '/v3/timeoffs?sort=-start_date',
headers: {
'Authorization' : 'Bearer ' + process.env.FLOAT_ACCESS_TOKEN
}
};
return https.get(options, function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
timeOff = JSON.parse(body);
callback();
});
}).on('error', (e) => {
console.error(e);
});;
}
function filterTimeOff(callback) {
timeOff.forEach( function(element, index) {
var inRange = false;
var startDate = element.start_date;
var endDate = element.end_date;
// if today is before end date and after start date
if((moment(now).isSameOrBefore(endDate, 'day'))
&&
(moment(now).isSameOrAfter(startDate, 'day'))) {
inRange = true;
console.log('Start Date: ' + startDate);
console.log('End Date: ' + endDate);
}
element.in_range = inRange;
});
callback();
}
function getPeople(callback) {
var options = {
host: 'api.float.com',
port: 443,
path: '/v3/people',
headers: {
'Authorization' : 'Bearer ' + process.env.FLOAT_ACCESS_TOKEN
}
};
https.get(options, function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
people = JSON.parse(body);
callback();
});
}).on('error', (e) => {
console.error(e);
});
}
function addPeopleToTimeOff(callback) {
var counter = 0;
timeOff.forEach( function(element, index) {
var id = element.people_ids[0];
element.person = people.find(person => {
return person.people_id === id;
})
timeOff[index] = element;
counter++;
if(counter === timeOff.length) {
callback();
}
});
}
function compileSlackMessage(callback) {
var count = 0;
timeOff.forEach( function(element, index) {
if(element.in_range) {
count++;
}
});
if (count > 1) {
message += 'There are ' + count + ' people off work today\n';
}
else if (count > 0) {
message += 'There is ' + count + ' person off work today\n';
}
message += generatePersonList();
callback();
}
function generatePersonList() {
var message = '';
timeOff.forEach( function(element, index) {
if(element.in_range) {
personName = element.person.name;
message += '*' + personName +'*';
if(element.timeoff_notes !== '') {
message += ' ' + element.timeoff_notes;
}
else {
message += ' ' + element.timeoff_type_name;
}
if(!element.full_day) {
message += ' (' + element.hours + ' hours)';
}
message += '\n';
}
});
return message;
}
function postToSlack(callback) {
if (message == '') {
console.log('Nothing to post');
callback();
return;
}
else {
send(message);
callback();
}
}
function init() {
isAWeekDay(function() {
getTimeOff(function() {
filterTimeOff(function() {
getPeople(function() {
addPeopleToTimeOff(function() {
compileSlackMessage(function() {
postToSlack(function() {
console.log('Done!');
})
})
});
})
});
});
})
};
init();