-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
253 lines (200 loc) · 7.93 KB
/
Copy pathindex.js
File metadata and controls
253 lines (200 loc) · 7.93 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// =============================================================================
// Parallel Project – Node App
// =============================================================================
const firebase = require('firebase-admin');
firebase.initializeApp({
credential: firebase.credential.cert(require('./build/service-account.json')),
databaseURL: `https://parallel-cf800.firebaseio.com`,
});
const fs = require('fs');
const path = require('path');
const functions = require('firebase-functions');
const express = require('express');
const user = require('./utilities/user');
const { getStreak } = require('./utilities/streak');
const BADGES = require('./build/badges.json');
const PAGES = require('./build/pages.json');
const LEVELS = ['year7', 'year8', 'year9', 'year10', 'year11'];
const LEVEL_NAMES = {year7: 'Year 7', year8: 'Year 8', year9: 'Year 9', year10: 'Year 10', year11: 'Year 11', graduated: 'Graduated'};
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const BADGE_NAMES = {ancient: 'Ancient Hero', geometry: 'Geometry', historic: 'Historic Hero', number: 'Number', modern: 'Modern Hero'};
const PAGES_MAP = {};
for (let l of LEVELS) {
for (let [i, p] of PAGES[l].entries()) {
const date = new Date(p.available);
p.available = +date;
p.deadline = +(new Date(p.deadline));
p.date = `${date.getDate()} ${MONTHS[date.getMonth()]} ${date.getFullYear()}`;
p.year = +l.slice(4);
p.index = PAGES[l].length - i;
PAGES_MAP[p.url] = p;
}
}
function scoreClass(score) {
if (score >= 90) return 'tesseract';
if (score >= 70) return 'cube';
if (score >= 50) return 'square';
if (score >= 20) return 'line';
return 'point';
}
function error(res, code) {
res.status(code);
return res.render('error', {code});
}
function letterOrder(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
// -----------------------------------------------------------------------------
// Set up Express App
const userDB = firebase.firestore().collection('users');
const app = express();
app.set('view engine', 'pug');
app.use(user.getActiveUser);
app.use((req, res, next) => {
res.locals.isProduction = process.env.NODE_ENV === 'production';
res.locals.user = req.user;
res.locals.badges = BADGES;
res.locals.pages = {};
res.locals.now = Date.now();
res.locals.levels = LEVELS;
res.locals.levelNames = LEVEL_NAMES;
res.locals.badgeNames = BADGE_NAMES;
res.locals.path = req.path.replace(/\/$/, '');
res.locals.scoreClass = scoreClass;
if (req.user && req.user.showWelcomeMsg) {
userDB.doc(req.user.uid) // async
.update({showWelcomeMsg: false})
.catch(() => console.error('Failed to update welcome msg', req.user.uid));
}
for (let l of LEVELS) {
res.locals.pages[l] =
PAGES[l].filter(p => (res.locals.now >= p.available));
}
res.locals.streak = getStreak(req, PAGES, new Date(res.locals.now));
next();
});
// -----------------------------------------------------------------------------
// Request Handlers
app.get('/', (req, res) => res.render('home'));
app.get('/contact', (req, res) => res.render('contact'));
for (let p of ['about', 'introduction', 'parents', 'teachers', 'terms-and-conditions', 'hints-tips', 'masterclass', 'troubleshooting']) {
const content = fs.readFileSync(path.join(__dirname, `build/${p}.html`));
app.get('/' + p, (req, res) => res.render('_layout', {content}));
}
app.get('/login', (req, res) => {
// When logging in we dont load their details before the "login redirect" (see addAuthTokenListener)
// So we send them to this path to redirect them correctly
if(res?.locals?.user?.code){
res.redirect('/dashboard')
}
if(res?.locals?.user){
res.redirect('/badges')
}
res.redirect('/');
})
app.get('/account', (req, res) => {
if (!req.user) return error(res, 401);
res.render('account');
});
app.get('/badges', (req, res) => {
if (!req.user) return error(res, 401);
res.render('badges', {showAllBadges: ('reveal' in req.query)});
});
app.get('/signup', (req, res) => {
if (req.user) res.redirect('/');
res.render('signup');
});
async function getDashboardData(req) {
const dashboard = {students: {}, pages: {}, error: null};
for (let l of LEVELS) {
dashboard.students[l] = [];
dashboard.pages[l] = PAGES[l].filter((p) => Date.now() >= p.available);
}
const students = await user.getAllStudents(req.user.code);
const studentKeys = Object.keys(students)
.filter(s => students[s].level !== 'graduated')
.sort((a, b) => letterOrder(students[a].last, students[b].last));
for (let s of studentKeys) {
if (!students[s].answers) students[s].answers = {};
dashboard.students[students[s].level || 'year7'].push(students[s]);
}
if (!studentKeys.length)
dashboard.error = 'So far, no students have signed up with your class code.';
return dashboard;
}
app.get('/dashboard', async function(req, res) {
if (!req.user) return error(res, 401);
if (!req.user.code) return error(res, 403);
const dashboard = await getDashboardData(req);
res.render('dashboard', {dashboard})
});
app.get('/dashboard.csv', async function(req, res) {
if (!req.user) return error(res, 401);
if (!req.user.code) return error(res, 403);
const dashboard = await getDashboardData(req);
const results = [];
for (let l of LEVELS) {
if (!dashboard.students[l].length) continue;
results.push(LEVEL_NAMES[l]);
const titles = ['Name', "Student Reference"];
for (const p of dashboard.pages[l]) titles.push('PG' + p.index);
results.push(titles.join(','));
for (const s of dashboard.students[l]) {
const row = [`"${s.first} ${s.last}"`, `"${s.userReference || ''}"`];
for (const p of dashboard.pages[l]) {
const answers = s.answers[p.url];
row.push(answers?.score !== undefined ? answers.score : ``);
}
results.push(row.join(','));
}
results.push('', '');
}
const response = results.join('\n');
res.setHeader('Content-disposition', 'attachment; filename=parallel-data.csv');
res.set('Content-Type', 'text/csv');
res.status(200).send(response);
});
app.post('/remove-student', async function(req,res) {
if (!req.user) return error(res, 401);
if (!req.user.code) return error(res, 403);
const student = await userDB.doc(req.body.id).get();
if (!student.exists) return error(res, 403);
const studentCodes = student.data().teacherCode || [];
if (!studentCodes.includes(req.user.code)) return error(res, 403);
await userDB.doc(req.body.id).update({teacherCode: studentCodes.filter(c => c !== req.user.code)});
res.sendStatus(200);
});
app.get('/validate/:code', async (req, res) => {
const code = req.params.code;
const data = await userDB.where('code', '==', code).limit(1).get();
const teacher = data.docs[0]?.data();
res.json(teacher ? {school: teacher.schoolName || 'Unknown School'} : {error: 'invalid-code'});
});
app.get('/:pid', (req, res, next) => {
const pid = req.params.pid;
if (!PAGES_MAP[pid]) return next();
const body = fs.readFileSync(path.join(__dirname, `build/${pid}.html`))
.toString().replace(/<h1.*<\/h1>/, '');
const answers = req.user ? (req.user.answers[pid] || {}) : {};
const userData = {answers, uid: req.user ? req.user.uid : '',
submitted: ('reveal' in req.query) || answers.submitted || false};
let newBadge = null;
if (req.user && !req.user.code) {
for (let b of BADGES[req.user.level]) {
if (b.score <= req.user.points && req.user.badges.indexOf(b.id) < 0) {
req.user.badges.push(b.id);
newBadge = b;
userDB.doc(req.user.uid) // async
.update({badges: req.user.badges.join(',')})
.catch(() => console.error('Failed to update badges', req.user.uid));
}
}
}
res.render('parallelogram', {pid, body, page: PAGES_MAP[pid], userData, newBadge});
});
app.use((req, res) => error(res, 404));
app.use((e, req, res, next) => {
console.error(e);
error(res, 500);
});
exports.app = functions.https.onRequest(app);