-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
70 lines (55 loc) · 2.18 KB
/
app.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
const bodyParser = require('body-parser');
const express = require('express');
const logger = require('./logger');
const MassageBooking = require('./services/massage_booking');
const { WebClient } = require('@slack/client');
const massageBooking = new MassageBooking(
new WebClient(process.env.SLACK_API_TOKEN),
{ host: process.env.REDIS_HOST, prefix: process.env.NODE_ENV },
parseInt(process.env.BOOKING_DURATION, 10),
);
const app = express();
const urlencodedParser = bodyParser.urlencoded({ extended: false });
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/web', (req, res) => res.sendfile(`${__dirname}/web/index.html`));
app.get('/web/script.js', (req, res) => res.sendfile(`${__dirname}/web/script.js`));
app.get('/web/style.css', (req, res) => res.sendfile(`${__dirname}/web/style.css`));
app.get('/web/massage.gif', (req, res) => res.sendfile(`${__dirname}/web/massage.gif`));
app.post('/slack/slash-commands/massage_booking', urlencodedParser, (req, res) => {
res.status(200).end();
const payload = req.body;
if (payload.token !== process.env.VERIFICATION_TOKEN) {
res.status(403).end('Access forbidden');
return;
}
massageBooking.bookMassage(payload, (error, response, body) => {
if (error) {
logger.error('Error', { method: 'bookMassage', error });
} else {
logger.info('Success', { method: 'bookMassage', body });
}
});
});
app.post('/slack/actions', urlencodedParser, (req, res) => {
res.status(200).end();
const payload = JSON.parse(req.body.payload);
if (payload.token !== process.env.VERIFICATION_TOKEN) {
res.status(403).end('Access forbidden');
return;
}
massageBooking.actionHandler(payload, (error, response, body) => {
if (error) {
logger.error('Error', { method: 'actionHandler', error });
} else {
logger.info('Success', { method: 'actionHandler', body });
}
});
});
app.get('/bookings', (req, res) => {
res.status(200);
massageBooking.bookingRepository.all().then((bookings) => {
const now = new Date();
res.json({ bookings: bookings.filter(booking => booking.dateRange.end > now).sort((a, b) => a.dateRange.start - b.dateRange.start) });
});
});
module.exports = app;