-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.js
255 lines (234 loc) · 8.92 KB
/
handler.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
'use strict'
var request = require('request')
var config = require('config')
module.exports.startTimer = (event, context, callback) => {
// var body = JSON.parse(event.body)
dailyListAPICall(function (error, response) {
if (error !== null) {
// Some Error!
httpCallback('error', 500, callback)
} else {
// Find stopped timer
var dayEntries = response.day_entries
var todayTimer = null
for (var index = 0; index < dayEntries.length; ++index) {
var dayEntry = dayEntries[index]
if (dayEntry.hasOwnProperty('timer_started_at') === false && dayEntry.project_id === config.get('harvest.credentials.project_id') && dayEntry.task_id === config.get('harvest.credentials.task_id')) {
todayTimer = dayEntry
}
}
if (todayTimer !== null && todayTimer.id > 0) {
// Found a running timer, stop it!
toggleTimerAPICall(todayTimer.id, function (error, response) {
if (error !== null) {
httpCallback('error', 500, callback)
} else {
httpCallback(response, 200, callback)
}
})
} else {
startTimerAPICall(0, config.get('harvest.credentials.task_id'), function (error, response) {
if (error !== null) {
// Some Error!
httpCallback('error', 500, callback)
} else {
// Ok: Started!
httpCallback({'message': 'no timers found, starting a new one', 'response': response }, 200, callback)
}
})
}
}
})
}
function startTimerAPICall (hours, taskId, callback) {
var options = { method: 'POST',
url: 'https://' + config.get('harvest.credentials.subdomain') + '.harvestapp.com/daily/add',
headers: { 'postman-token': 'f2f7f539-ab31-ed96-d613-38d7dc8f4d6f',
'cache-control': 'no-cache',
authorization: generateBasicAuth(config.get('harvest.credentials.username'), config.get('harvest.credentials.password')),
'content-type': 'application/json',
accept: 'application/json'
},
body: { notes: 'Started Using IFTTH',
project_id: config.get('harvest.credentials.project_id'),
task_id: taskId,
hours: (hours > 0 ? hours : null)
},
json: true
}
// Send your request to harvest
request(options, function (error, response, body) {
console.log('error:', error) // Print the error if one occurred
console.log('statusCode:', response && response.statusCode) // Print the response status code if a response was received
console.log('body:', body) // Print the HTML for the Google homepage.
callback(error, body)
})
}
function dailyListAPICall (callback) {
var options = {
method: 'GET',
url: 'https://' + config.get('harvest.credentials.subdomain') + '.harvestapp.com/daily/',
headers: {
'cache-control': 'no-cache',
authorization: generateBasicAuth(config.get('harvest.credentials.username'), config.get('harvest.credentials.password')),
'content-type': 'application/json',
accept: 'application/json'
},
json: true
}
// Send your request to harvest
request(options, function (error, response, body) {
console.log('error:', error) // Print the error if one occurred
console.log('statusCode:', response && response.statusCode) // Print the response status code if a response was received
console.log('body:', body) // Print the HTML for the Google homepage.
callback(error, body)
})
}
function toggleTimerAPICall (id, callback) {
var options = {
method: 'GET',
url: 'https://' + config.get('harvest.credentials.subdomain') + '.harvestapp.com/daily/timer/' + id,
headers: {
'cache-control': 'no-cache',
authorization: generateBasicAuth(config.get('harvest.credentials.username'), config.get('harvest.credentials.password')),
'content-type': 'application/json',
accept: 'application/json'
},
json: true
}
// Send your request to harvest
request(options, function (error, response, body) {
console.log('error:', error) // Print the error if one occurred
console.log('statusCode:', response && response.statusCode) // Print the response status code if a response was received
console.log('body:', body) // Print the HTML for the Google homepage.
callback(error, body)
})
}
module.exports.stopRunningTimer = (event, context, callback) => {
// var body = JSON.parse(event.body)
dailyListAPICall(function (error, response) {
if (error !== null) {
// Some Error!
httpCallback('error', 500, callback)
} else {
// Find running timer (on project and task)
var dayEntries = response.day_entries
var runningTimer = null
for (var index = 0; index < dayEntries.length; ++index) {
var dayEntry = dayEntries[index]
if (dayEntry.hasOwnProperty('timer_started_at') === true && dayEntry.project_id === config.get('harvest.credentials.project_id') && dayEntry.task_id === config.get('harvest.credentials.task_id')) {
runningTimer = dayEntry
}
}
if (runningTimer !== null && runningTimer.id > 0) {
// Found a running timer, stop it!
toggleTimerAPICall(runningTimer.id, function (error, response) {
if (error !== null) {
httpCallback('error', 500, callback)
} else {
if (config.get('harvest.credentials.cut_billable') === 1) {
// Cut billable.
cutBillable(response, function (error, reponse) {
if (error !== null) {
httpCallback('error', 500, callback)
} else {
httpCallback(response, 200, callback)
}
})
} else {
httpCallback(response, 200, callback) // No cutting needed.
}
}
})
} else {
httpCallback({'message': 'no running timers found', 'response': response }, 404, callback)
}
}
})
}
module.exports.cutToday = (event, context, callback) => {
// Cuts it in pieces.
dailyListAPICall(function (error, response) {
if (error !== null) {
// Some Error!
httpCallback('error', 500, callback)
} else {
// Find running timer (on project and task)
var dayEntries = response.day_entries
var billableEntry = null
for (var index = 0; index < dayEntries.length; ++index) {
var dayEntry = dayEntries[index]
if (dayEntry.hasOwnProperty('timer_started_at') === false && dayEntry.project_id === config.get('harvest.credentials.project_id') && dayEntry.task_id === config.get('harvest.credentials.task_id')) {
billableEntry = dayEntry
}
}
if (billableEntry !== null && billableEntry.id > 0) {
// Found a stopped timer, try to cut it
cutBillable(billableEntry, function (error, reponse) {
if (error !== null) {
httpCallback('error', 500, callback)
} else {
httpCallback(response, 200, callback)
}
})
} else {
httpCallback({'message': 'nothing stopped found', 'response': response }, 404, callback)
}
}
})
}
function cutBillable (entry, callback) {
var maxBillable = config.get('harvest.credentials.max_billable_hours')
var unBillableTaskId = config.get('harvest.credentials.unbillable_task_id')
if (maxBillable < entry.hours) {
var remains = entry.hours - maxBillable
entry.hours = maxBillable
updateEntry(entry, function (error, body) {
if (error !== null) {
callback(error, null)
} else {
// Entry has been updated, add the remaining to unbillable.
startTimerAPICall(remains, unBillableTaskId, function (error, body) {
if (error !== null) {
callback(error, null)
} else {
callback(null, body)
}
})
}
})
}
}
function generateBasicAuth (username, password) {
return 'Basic ' + new Buffer(username + ':' + password).toString('base64')
}
function httpCallback (message, code, callback) {
const response = {
statusCode: code,
body: JSON.stringify({
message: message
})
}
callback(null, response)
}
function updateEntry (entry, callback) {
var options = {
method: 'POST',
url: 'https://' + config.get('harvest.credentials.subdomain') + '.harvestapp.com/daily/update/' + entry.id,
headers: {
'cache-control': 'no-cache',
authorization: generateBasicAuth(config.get('harvest.credentials.username'), config.get('harvest.credentials.password')),
'content-type': 'application/json',
accept: 'application/json'
},
body: entry,
json: true
}
// Send your request to harvest
request(options, function (error, response, body) {
console.log('error:', error) // Print the error if one occurred
console.log('statusCode:', response && response.statusCode) // Print the response status code if a response was received
console.log('body:', body) // Print the HTML for the Google homepage.
callback(error, body)
})
}