forked from cdanielsen/dwish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
62 lines (56 loc) · 1.42 KB
/
events.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
const rp = require('request-promise')
const moment = require('moment')
const {SLACK_HOOK} = require('./config')
function scheduleAlert ({time = 9000000, msg = "Something happened!"}) {
return setTimeout(() => {
return rp({
uri: SLACK_HOOK,
method: 'POST',
body: {
'text': msg
},
json: true
})
}, time)
}
function getData (apiKey) {
return rp({
method: 'GET',
uri: `https://api.mlab.com/api/1/databases/dwishdb/collections/DishEvents?apiKey=${apiKey}`,
json: true
});
}
function getDataById (apiKey, id) {
return rp({
method: 'GET',
uri: `https://api.mlab.com/api/1/databases/dwishdb/collections/DishEvents/${id}?apiKey=${apiKey}`,
json: true
});
}
function postData (apiKey, eventType) {
return rp({
method: 'POST',
uri: `https://api.mlab.com/api/1/databases/dwishdb/collections/DishEvents?apiKey=${apiKey}`,
body: {
timeCreated: moment().format(),
timeClaimed: '',
claimed: false,
eventType
},
json: true
});
}
function putData(apiKey, dishEvent) {
const id = dishEvent._id.$oid;
return rp({
method: 'PUT',
uri: `https://api.mlab.com/api/1/databases/dwishdb/collections/DishEvents/${id}?apiKey=${apiKey}`,
body: {
...dishEvent,
timeClaimed: moment().format(),
claimed: true
},
json: true
});
}
module.exports = { scheduleAlert, getData, postData, putData, getDataById }