-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (58 loc) · 2.27 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
const {WebClient} = require('@slack/web-api');
const {Raffle} = require('./lib/raffle');
const RafflePrizeRepository = require('./lib/raffle-prize-repository');
const express = require('express');
const path = require('path');
const fs = require('fs');
const SlackChannelRepository = require("./lib/slack-channel-repository");
const SlackUserRepository = require("./lib/slack-user-repository");
const RaffleCandidateService = require("./lib/raffle-candidate-service");
const readEnvOrFail = (key) => {
const result = process.env[key];
if (!result) {
throw new Error(`Missing envvar ${key}`);
}
return result;
}
const checkEnvVarPath = (path, envVar) => {
if (!fs.statSync(path).isFile()) {
throw new Error(`${path} does not point to a regular file. Please review your ${envVar} envvar configuration`);
}
}
const token = readEnvOrFail('SLACK_TOKEN');
const port = process.env.PORT || 3000;
const prizeFilePath = process.env.PRIZE_FILE || `${__dirname}/samples/prizes.json`;
checkEnvVarPath(prizeFilePath, 'PRIZE_FILE');
const app = express();
app.use(express.json());
const slackClient = new WebClient(token);
const prizeRepository = new RafflePrizeRepository(prizeFilePath);
const raffleCandidateService = new RaffleCandidateService(new SlackChannelRepository(slackClient), new SlackUserRepository(slackClient));
(async () => {
const prizes = await prizeRepository.getPrizes();
const initialCandidates = await raffleCandidateService.findInitialCandidates();
const raffle = new Raffle(initialCandidates, prizes);
app.get('/', (req, res) => {
res.sendFile(path.join(`${__dirname}/static/raffle.html`));
});
app.get('/status', (req, res) => {
res.status(200).send(raffle.status());
});
app.post('/cancel-winner', (req, res) => {
raffle.cancel(req.body.userId);
res.status(204).send();
});
app.post('/reset', (req, res) => {
raffle.reset();
res.status(204).send();
});
app.post('/', (req, res) => {
const raffleResult = raffle.run();
if (!raffleResult) {
res.status(204).send();
return;
}
res.status(200).send(raffleResult);
});
app.listen(port, () => console.log(`Listening at http://localhost:${port}`));
})();