This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (162 loc) · 4.38 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
const fetch = require('node-fetch')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.PORT || 8080
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/checkin', function (req, res) {
const token = req.body.token
const user = req.body.user_id
const code = req.body.text
const username = req.body.user_name
console.log(req.body)
fetch('https://checkin-chicken.herokuapp.com/checkin', {
method: 'POST',
headers: {
'api_key': token,
'slack_id': user,
'Content-Type': 'application/json',
},
body: JSON.stringify({
'check_in_code': code,
'username': username,
})
}).then((e) => e.json())
.then((json) => {
res.send(json.message)
})
})
app.post('/score', function (req, res) {
const token = req.body.token
const user = req.body.user_id
fetch('https://checkin-chicken.herokuapp.com/score', {
method: 'GET',
headers: {
'api_key': token,
'slack_id': user,
'Content-Type': 'application/json',
}
}).then((e) => e.json())
.then((json) => {
if (json.message) {
res.send(json.message)
return
}
res.send('Your score is: ' + json.score)
})
})
app.post('/score', function (req, res) {
})
app.post('/event', function (req, res) {
const token = req.body.token
const user = req.body.user_id
const parts = req.body.text.split(' ')
const command = parts[0]
const code = parts[1]
switch (command) {
case 'new':
let name = parts.slice(1).join(' ')
let weight = 1
if (!isNaN(parts[1])) {
name = parts.slice(2).join(' ')
weight = parts[1]
}
if (!name) {
res.send('Usage: /event new <optionalWeight> <name>')
} else {
createEvent(name, weight, token, user, res)
}
break
case 'list':
listEvents(false, token, user, res)
break
case 'active':
listEvents(true, token, user, res)
break
case 'close':
if (!code) {
res.send('Usage: /event close <eventCode>')
}
setEventStatus('close', code, token, user, res)
break
case 'reopen':
if (!code) {
res.send('Usage: /event close <eventCode>')
}
setEventStatus('reopen', code, token, user, res)
break
case 'delete':
if (!code) {
res.send('Usage: /event close <eventCode>')
}
setEventStatus('delete', code, token, user, res)
break
case 'reactivate':
if (!code) {
res.send('Usage: /event close <eventCode>')
}
setEventStatus('reactivate', code, token, user, res)
break
}
})
function createEvent(name, weight, token, user, res) {
fetch('https://checkin-chicken.herokuapp.com/event/new', {
method: 'POST',
headers: {
'api_key': token,
'slack_id': user,
'Content-Type': 'application/json',
},
body: JSON.stringify({
'name': name,
'weight': Number(weight),
})
}).then((e) => e.json())
.then((json) => {
// Had error
if (json.message) {
res.send(json.message)
} else {
res.send('Event #' + json.event_id + ' was created. Check-in code is ' + json.check_in_code)
}
})
}
function listEvents(active, token, user, res) {
fetch('https://checkin-chicken.herokuapp.com/event/list' + (active ? '/active' : ''), {
headers: {
'api_key': token,
'slack_id': user,
'Content-Type': 'application/json',
},
}).then((e) => e.json())
.then((json) => {
// Had error
if (json.message) {
res.send(json.message)
} else {
res.send('*Here\'s all of the events: *\n' + json.events.reduce((fin, item) => {
const statusEmoji = (item.deleted ? ':trash: ' : (item.active ? ':heavy_check_mark: ' : ':x: ' ))
return fin += (statusEmoji + item.name + ' - ' + item.check_in_code + ' - ' + item.attendees.length + '\n')
}, ''))
}
})
}
function setEventStatus(status, code, token, user, res) {
fetch(('https://checkin-chicken.herokuapp.com/event/' + status), {
method: 'PUT',
headers: {
'api_key': token,
'slack_id': user,
'Content-Type': 'application/json',
},
body: JSON.stringify({
'check_in_code': code
})
}).then((e) => e.json())
.then((json) => {
res.send(json.message)
})
}
// start the server
app.listen(port)
console.log('Server started! At http://localhost:' + port)