-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
132 lines (116 loc) · 3.34 KB
/
server.js
File metadata and controls
132 lines (116 loc) · 3.34 KB
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
//node packages
const express = require("express");
const axios = require("cachios");
const bodyParser = require("body-parser");
require("dotenv").config();
//local packages
const { makeWhosonDate } = require("./utilities/helperFunctions.js");
//node package config
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//globals
const BASE_URL = process.env.BASE_URL;
const RPIA_WEB_TOKEN = process.env.RPIA_WEB_TOKEN;
const SLACK_SIGNING_TOKEN = process.env.SLACK_SIGNING_TOKEN;
const PORT = process.env.NODE_PORT || 3000;
app.post("/whoson", async ({ body: { token, text } }, res) => {
if (token != SLACK_SIGNING_TOKEN) {
res.send("Sorry, you're not authenticated!");
}
let done = false;
if (text === "" || text.toLowerCase() === "today") {
const { data } = await axios.get(
`${BASE_URL}?token=${RPIA_WEB_TOKEN}`
);
res.send(data);
} else if (text.toLowerCase() === "week") {
const { data } = await axios.get(
`${BASE_URL}?token=${RPIA_WEB_TOKEN}&week=1`
);
res.send(data);
} else {
const d = new Date();
let o = new Date();
let date = "";
switch (text.toLowerCase()) {
case "yest":
case "yesterday":
o.setDate(d.getDate() - 1);
date = makeWhosonDate(o) + "&yesterday=1";
break;
case "tom":
case "tomorrow":
o.setDate(d.getDate() + 1);
date = makeWhosonDate(o) + "&tomorrow=1";
break;
case "sun":
case "sunday":
o.setDate(d.getDate() + (7 - d.getDay()));
date = makeWhosonDate(o);
break;
case "mon":
case "monday":
if (d.getDay() < 1) {
o.setDate(d.getDate() + (1 - d.getDay()));
} else {
o.setDate(d.getDate() + (7 - (d.getDay() - 1)));
}
date = makeWhosonDate(o);
break;
case "tues":
case "tuesday":
if (d.getDay() < 2) {
o.setDate(d.getDate() + (2 - d.getDay()));
} else {
o.setDate(d.getDate() + (7 - (d.getDay() - 2)));
}
date = makeWhosonDate(o);
break;
case "wed":
case "wednesday":
if (d.getDay() < 3) {
o.setDate(d.getDate() + (3 - d.getDay()));
} else {
o.setDate(d.getDate() + (7 - (d.getDay() - 3)));
}
date = makeWhosonDate(o);
break;
case "thurs":
case "thursday":
if (d.getDay() < 4) {
o.setDate(d.getDate() + (4 - d.getDay()));
} else {
o.setDate(d.getDate() + (7 - (d.getDay() - 4)));
}
date = makeWhosonDate(o);
break;
case "fri":
case "friday":
if (d.getDay() < 5) {
o.setDate(d.getDate() + (5 - d.getDay()));
} else {
o.setDate(d.getDate() + (7 - (d.getDay() - 5)));
}
date = makeWhosonDate(o);
break;
case "sat":
case "saturday":
o.setDate(d.getDate() + (6 - d.getDay()));
date = makeWhosonDate(o);
break;
default:
res.send("Please enter a valid day parameter and try again.");
done = true;
}
if (!done) {
const { data } = await axios.get(
`${BASE_URL}?token=${RPIA_WEB_TOKEN}&date=${date}`
);
res.send(data);
}
}
});
app.listen(PORT, () => {
console.log(`whoson running on port ${PORT}`);
});