-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (167 loc) · 5.55 KB
/
server.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require("dotenv").config();
const bodyParser = require('body-parser');
const NodeCache = require("node-cache");
const { Client } = require("pg");
const express = require("express");
const app = express();
const port = process.env.PORT || 8080;
var path = require('path');
const school_cache = new NodeCache({ stdTTL: 60 * 5 });
let client = new Client({
user: "postgres",
password: null,
host: "localhost",
database: "sbhacksvi_dev",
port: 5432
});
if(process.env.NODE_ENV === "production") {
client = new Client({
connectionString: process.env.DB_URL
});
}
client.connect();
const getSchoolCount = () => {
let school_count_query =
`SELECT schools.id, schools.name, COUNT(*)
FROM schools
JOIN applications ON applications.school_id = schools.id
GROUP BY schools.name, schools.id
ORDER BY count DESC;`;
return new Promise((resolve, reject) => {
school_cache.get("count", (err, cached_rows) => {
if(cached_rows) return resolve(cached_rows);
console.log("querying..");
client.query(school_count_query, (err, res) => {
school_cache.set("count", res.rows, (err, success) => {
resolve(res.rows);
});
});
})
});
}
const getApplications = (school_id) => {
let applications_from_school_query =
`SELECT applications.*, users.*, schools.name as school_name, applications.id as application_id
FROM schools
JOIN applications ON applications.school_id = schools.id
JOIN users ON applications.user_id = users.id
${school_id ? `WHERE schools.id = ${parseInt(school_id) || 0}` : ""}
ORDER BY applications."createdAt", applications."updatedAt";`;
return new Promise((resolve, reject) => {
school_cache.get(`apps-${school_id}`, (err, cached_rows) => {
if(cached_rows) return resolve(cached_rows);
console.log("Querying applications..");
client.query(applications_from_school_query, (err, res) => {
if(err) console.log(err);
school_cache.set(`apps-${school_id}`, res.rows, (err, success) => {
resolve(res.rows);
});
});
})
});
};
const getAllApplicationsNoCache = () => {
let applications_from_school_query =
`SELECT applications.*, users.*, schools.name as school_name, applications.id as application_id
FROM schools
JOIN applications ON applications.school_id = schools.id
JOIN users ON applications.user_id = users.id
ORDER BY accepted DESC, first_name, last_name;`;
return new Promise((resolve, reject) => {
client.query(applications_from_school_query, (err, res) => {
if(err) console.log(err);
resolve(res.rows);
});
});
};
const getApplicationsCheckedInCountNoCache = () => {
let applications_checked_in_count_query =
`SELECT COUNT(*) FROM applications WHERE checked_in=true;`;
return new Promise((resolve, reject) => {
client.query(applications_checked_in_count_query, (err, res) => {
if(err) console.log(err);
resolve(res.rows);
});
});
};
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "src/views"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "src/static")));
app.use((req, res, next) => {
res.locals.s3_url = process.env["S3_URL"] || "";
res.locals.token = process.env["TOKEN"];
next();
});
app.get("/", (req, res) => {
getSchoolCount()
.then((schools) => {
res.locals.schools = schools;
res.render("index");
});
});
app.get(`/${process.env["TOKEN"]}/app-review`, (req, res) => {
console.log(req.query);
var start = req.query.start || 0;
var end = req.query.end || 2000;
getAllApplicationsNoCache()
.then((applications) => {
var filteredApps = applications.filter((application) => {
return (application.application_id>=start && application.application_id<=end);
});
var apps = {applicants: filteredApps};
res.render("review", {applications: apps});
});
});
app.get(`/${process.env["TOKEN"]}/applications/:school_id?`, (req, res) => {
getSchoolCount()
.then((schools) => {
res.locals.schools = schools;
res.locals.params = req.params;
getApplications(req.params["school_id"])
.then((applications) => {
res.locals.applications = applications;
res.render("applications");
});
});
});
app.put('/update-rating', (req, res) => {
if(isNaN(req.body.id)) return res.json("Nope");
var rating = null;
if (req.body.rating!=='')
{
rating = req.body.rating;
}
client.query(`UPDATE applications SET rating=${rating} WHERE id=${req.body.id};`, (err, response) => {
if(err) throw err;
res.json({id: req.body.id, rating: rating});
});
});
app.post(`/${process.env["TOKEN"]}/checkin`, (req, res) => {
if(isNaN(req.query.application_id)) return res.json("Nope");
client.query(`UPDATE applications SET checked_in=true WHERE id=${req.query.application_id};`, (err, response) => {
if(err) throw err;
res.redirect(`/${process.env["TOKEN"]}/checkin`);
});
});
app.get(`/${process.env["TOKEN"]}/checkin`, (req, res) => {
getApplicationsCheckedInCountNoCache()
.then((applications_checked_in_count) => {
res.locals.applications_checked_in_count = applications_checked_in_count[0]["count"];
getAllApplicationsNoCache()
.then((applications) => {
res.locals.applications = applications;
res.locals.token = process.env["TOKEN"];
res.render("checkin");
});
})
});
app.post(`/${process.env["TOKEN"]}/undo_checkin`, (req, res) => {
if(isNaN(req.query.application_id)) return res.json("Nope");
client.query(`UPDATE applications SET checked_in=NULL WHERE id=${req.query.application_id};`, (err, response) => {
if(err) throw err;
res.redirect(`/${process.env["TOKEN"]}/checkin`);
});
});
app.listen(port, () => console.log("Server listening in on port", port));