This repository has been archived by the owner on May 27, 2022. It is now read-only.
generated from abir-taheer/quicker-picker-upper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (89 loc) · 2.38 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
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
require('express-async-errors');
const express = require('express');
const app = express();
const jwtValidator = require('./middleware/jwtValidator');
const api = require('./api');
const nunjucks = require('nunjucks');
const { GOOGLE_CLIENT_ID, COOKIE_SECRET } = require('./constants');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const models = require('./database/models');
const crypto = require('crypto');
app.use(
logger('dev', {
skip: (req, res) =>
res.statusCode < 400 && process.env.NODE_ENV === 'production'
})
);
app.use(cookieParser(COOKIE_SECRET));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(jwtValidator);
app.use('/api', api);
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.use((req, res, next) => {
// If they're not signed in always send the sign in page
if (!req.jwt) {
res.render('sign-in.html', { GOOGLE_CLIENT_ID });
} else {
next();
}
});
app.get('/', async (req, res) => {
const application = await models.applications.findOne();
const subHash = crypto
.createHash('sha256')
.update(String(application.id) + req.jwt.user.sub)
.digest('hex');
let userCode = await models.userCodes.findOne({
where: {
subHash
}
});
if (!userCode) {
userCode = await models.userCodes.create({
applicationId: application.id,
subHash,
code: crypto.randomBytes(4).toString('hex').toUpperCase()
});
}
const results = await models.results.findAll({
where: {
code: userCode.code
}
});
results.forEach(res => {
if(res.email !== req.jwt.user.email){
res.email = req.jwt.user.email;
res.save();
}
});
const messages = await Promise.all(
results.map(
async res =>
(
(await models.acceptanceMessages.findOne({
where: {
application: res.application,
status: res.status
}
})) || {
application: res.application,
message:
'<p>After reading your application for Student Union membership positions, we are unfortunately not able to give you an interview. We had a pool of extremely talented and qualified applicants this year and as much as we would have liked to, we are not able to interview every applicant.\n' +
'</p>'
}
)
)
);
res.render('index.html', {
user: req.jwt.user,
userCode,
messages
});
});
module.exports = app;