-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·1180 lines (1071 loc) · 35.1 KB
/
main.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/node
"use strict";
import * as mdb from 'mongodb';
import { MongoClient } from 'mongodb';
import express from 'express';
import session from 'express-session';
import { WebSocketServer } from 'ws';
import { createServer, Server } from 'http';
import bcrypt from 'bcrypt';
import fs, { read, readFileSync } from 'fs';
import { readFile } from 'node:fs/promises';
import { JSDOM } from 'jsdom';
import multer from 'multer';
import ConnectMongoDBSession from 'connect-mongodb-session';
const MongoDBStore = ConnectMongoDBSession(session);
// Use `yargs` to parse command-line arguments.
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import {log, accessLog, stdoutLog, errorLog, addDevLog} from './logging.mjs';
import {readSecrets} from './shakeguardSecrets.mjs';
import applyEasterEggStyle from './easterEgg.mjs';
import {join} from "node:path";
const argv = yargs(hideBin(process.argv))
.option('port', {
alias: 'P',
description: "The port that the app should listen on.",
type: 'number'
})
.option('instanceAddress', {
alias: 'i',
description: "The IP address or hostname of the MongoDB instance to connect the app to. Defaults to `localhost`.",
type: 'string'
})
.option('instancePort', {
alias: 'p',
description: "The port number that the MongoDB instance listens on. Defaults to `27017`.",
type: 'number'
})
.option('dbName', {
alias: 'db',
description: "The name of the database to use in the MongoDB instance. Defaults to `COMP2800`.",
type: 'string'
})
.option('auth', {
description: "If `--auth true`, the app will attempt to log into the MongoDB instance with"
+ "the username and password specified in the file `.secrets/mongodb_auth.json`.",
type: 'boolean'
}).option('devLog', {
description: "If set, app will log lots of data to stdout. If not, only errors will be logged.",
type: 'boolean'
})
.help()
.alias('help', 'h').parse();
const app = express();
const upload = multer();
// Defaults for address and port:
const dbURL = `mongodb://${argv.instanceAddress ?? 'localhost'}:${argv.instancePort ?? '27017'}`;
const dbName = argv.dbName ?? "COMP2800";
// Log in 'dev' format to stdout, if devLog option is set.
// If devLog is not set, log errors only to stdout.
const secrets = await new Promise((resolve) => {
if (argv.devLog) {
app.use(stdoutLog);
addDevLog(log);
} else {
app.use(errorLog)
}
app.use(accessLog);
resolve();
}).then(
async () => await readSecrets()
);
// The MongoDB Connection object.
/** @type {?MongoClient} */
let mongo = null;
/** The MongoDB Database object.
* Populated by the `{@link connectMongo()} function.
* @type {?mdb.Db} */
let db = null;
/** Connection options for MongoDB, also used for the session store.
* @type {mdb.MongoClientOptions} */
const mongoConnectionOptions = {
auth: argv.auth ? {
...secrets['mongodb_auth.json']
} : undefined,
useNewUrlParser: true,
useUnifiedTopology: true,
};
// Establishes connection and returns necessary objects to interact with database
// Caller is responsible for closing the connection
const connectMongo = async (url, dbName) => {
let mongo, db;
let dotAnim; // Interval handler for the "dots" animation.
try {
dotAnim = setInterval(() => process.stdout.write('…'), 1000);
mongo = await Promise.race([
MongoClient.connect(url, mongoConnectionOptions),
new Promise((res,rej) => {
setTimeout(() => rej("Timed out after 15 seconds."), 15000)
})
]);
db = mongo.db(dbName);
initDatabase(db);
log.info(`Connected to "${url}", using database "${dbName}"`);
} catch (error) {
log.info(); // Newline!
log.error(`Tried connecting to ${url}, using database ${dbName}`);
log.error("Ran into an error while connecting to MongoDB!");
switch (error.message) {
case "credentials must be an object with 'username' and 'password' properties":
if (argv.auth) {
log.error("Error: --auth option was set, but secrets module could "
+ "not load username and password for MongoDB instance!");
log.error("Try redownloading .secrets or running without the --auth option.")
}
break;
default:
break;
}
log.error('Error object details:');
log.info(JSON.stringify(error, null, 2));
log.error('Exiting early due to errors!')
// TODO: consider any cleanup code before exiting: any open file handles?
process.exit();
} finally {
clearInterval(dotAnim);
}
return [mongo, db];
}
// If db does not contain collections we need, add them.
// Else do nothing
/**
* Initializes the given [database]{@link Db} with values from JSON files in the `./data/` directory.
* If the database already contains all needed collections, this function is a no-op.
* @param {mdb.Db} db - The database to initialize.
*/
async function initDatabase(db){
// Touch the sessions collection.
const sessionsCollection = db.collection("BBY-6_sessions");
const usersCollection = db.collection("BBY-6_users");
// There are no collections in this database so we need to add ours.
const users = await usersCollection.find({}).toArray();
if (users.length === 0) {
const usersJson = JSON.parse(await readFile('./data/users.json', 'utf-8'));
usersJson.forEach(user => {
user.kits.forEach(kit => {
kit._id = new mdb.ObjectId();
kit.kit.forEach(item=> {
item._id = new mdb.ObjectId();
})
})
})
await db.collection("BBY-6_users").insertMany(usersJson);
// Create a unique index on the emailAddress field in the users collection.
await db.collection("BBY-6_users").createIndex({ emailAddress: 1 }, { unique: true });
}
const items = await db.collection("BBY-6_items").find({}).toArray();
if (items.length === 0) {
const itemsJson = JSON.parse(await readFile('./data/items.json', 'utf-8'))
await db.collection("BBY-6_items").insertMany(itemsJson);
}
const categories = await db.collection("BBY-6_categories").find({}).toArray();
if (categories.length === 0) {
const categoriesJson = JSON.parse(await readFile('./data/categories.json', 'utf-8'))
await db.collection("BBY-6_categories").insertMany(categoriesJson);
}
const articles = await db.collection("BBY-6_articles").find({}).toArray();
if (articles.length === 0) {
const articlesJson = JSON.parse(await readFile('./data/articles.json', 'utf-8'))
await db.collection("BBY-6_articles").insertMany(articlesJson);
}
const kits = await db.collection("BBY-6_kit-templates").find({}).toArray();
if(kits.length === 0 ){
const kitsJson = JSON.parse(await readFile('./data/kits.json', 'utf-8'))
kitsJson.forEach(kit => {
kit.kit.forEach(item => {
item._id = new mdb.ObjectId();
})
});
await db.collection("BBY-6_kit-templates").insertMany(kitsJson);
}
}
/**
* Uses {@link loadHTMLComponent} to load header and footer into their respective tags given a JSDOM object.
* @param {jsdom.JSDOM} baseDOM - the DOM object to template onto.
* @returns {jsdom.JSDOM} The original DOM object, with header and footer attached.
*/
const loadHeaderFooter = async (baseDOM) => {
// Add the header
baseDOM = await loadHTMLComponent(baseDOM, "header", "header", "./templates/header.html");
// HACK: Horrible hack to add the <link> element for the manifest to every page.
baseDOM = await loadHTMLComponent(baseDOM, "head", "head", "./templates/header.html");
// Add the footer
baseDOM = await loadHTMLComponent(baseDOM, "footer", "footer", "./templates/footer.html");
return baseDOM;
}
// Load a HTML component
// baseDOM is the return value of `new JSDOM(htmlFile)`
// Selectors should be formatted as they would with `querySelector()`
// componentLocation example: ./html/header.html
const loadHTMLComponent = async (baseDOM, placeholderSelector, componentSelector, componentLocation) => {
const document = baseDOM.window.document;
const placeholder = document.querySelector(placeholderSelector);
const html = await readFile(componentLocation, "utf8");
const componentDOM = new JSDOM(html);
placeholder.innerHTML = placeholder.innerHTML + componentDOM.window.document.querySelector(componentSelector).innerHTML;
return baseDOM;
}
log.info("Connecting to MongoDB instance…");
let sessionStore = null;
try {
[mongo, db] = await connectMongo(dbURL, dbName);
sessionStore = new MongoDBStore({
uri: dbURL,
databaseName: dbName,
collection: "BBY-6_sessions",
connectionOptions: mongoConnectionOptions
});
} catch (err) {
log.error(`Could not connect to MongoDB instance at ${dbURL}/${dbName}!`);
log.error(JSON.stringify(err, null, 2));
}
app.use(express.json({limit: '50mb'}));
const sessionParser = session({
secret: secrets['session.json'].sessionSecret,
name: "ShakeGuardSessionID",
store: sessionStore ?? undefined,
resave: false,
// create a unique identifier for that client
saveUninitialized: true
});
app.use(sessionParser);
// static path mappings
app.use("/js", express.static("public/js"));
app.use("/css", express.static("public/css"));
app.use("/images", express.static("public/images"));
app.use("/sounds", express.static("public/sounds"));
app.use("/html", express.static("public/html"));
app.use("/fonts", express.static("public/fonts"));
app.use("/shakeguard.webmanifest", (req, res) => {
res.sendFile(join(process.cwd(), "/public/shakeguard.webmanifest"));
});
app.use("/favicon.ico", (req, res) => {
res.sendFile(join(process.cwd(), "/public/images/comp2800_logo_favicon.ico"));
});
app.use("/sw.js", (req, res) => {
res.setHeader("Service-Worker-Allowed", "/");
res.sendFile(join(process.cwd(), "/public/js/sw.js"));
});
app.get('/', async function (req, res) {
if (req.session.loggedIn) {
if (req.session.isAdmin) {
res.redirect('/dashboard');
} else {
res.redirect('/profile');
}
log.info("User logged in!");
return;
}
const doc = await readFile("./html/index.html", "utf8");
const baseDOM = new JSDOM(doc);
let index = await loadHeaderFooter(baseDOM);
index = changeLoginButton(index, req);
res.send(index.serialize());
});
app.get('/profile-details', async(req, res) => {
// Check that the user is logged in.
const filterQuery = {emailAddress: req.session.email};
const userResults = await db.collection('BBY-6_users').find(filterQuery).toArray();
if(userResults.length === 0) {
// Could not find user
res.status(500).send("userNotFound");
return;
} else {
// res.setHeader("Content-Type", "application/json");
res.status(200).send({
name: req.session.name,
email: req.session.email
})
}
})
/**
* Redirects non-logged-in users to the `'/login'` route.
* @param {Response} res - Response object, may get redirected
* @returns `true` if the user was redirected, `false` otherwise.
*/
function redirectToLogin(req, res) {
if (!req.session || !req.session.loggedIn) {
res.redirect("/login");
return true;
}
return false;
}
/**
* Hide the logout/login buttons as appropriate, based on whether the user
* has an active session.
* NOTE: param types might be wrong fix em later
* @param {JSDOM} baseDOM
* @param {Request} req
*/
function changeLoginButton(baseDOM, req) {
const document = baseDOM.window.document;
if(!req.session || !req.session.loggedIn) {
document.getElementById("Button-Logout").style.display = "none";
document.getElementById("Kit-Button").style.display = "none";
} else {
document.getElementById("Button-Login-Nav").style.display = "none";
}
return baseDOM;
}
/**
* Checks if the user is logged in, if they aren't, sends a 401 error.
* @param {Response} res - Response object to send errors to; may get sent.
* @returns `true` if the user was sent an error, `false` otherwise.
*/
function checkLoginError(req, res) {
if (!req.session || !req.session.loggedIn) {
res.status(401).send("invalidSession");
return true;
}
return false;
}
app.get('/profile', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
if (req.session.isAdmin) {
res.redirect('/dashboard');
return;
}
let doc = await readFile("./html/user-profile.html", "utf8");
const baseDOM = new JSDOM(doc);
let profile = await loadHeaderFooter(baseDOM);
profile = changeLoginButton(profile, req);
if(req.session.easterEgg) {
const link = profile.window.document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/geocities.css'
profile.window.document.getElementsByTagName('HEAD')[0].appendChild(link);
}
profile = await loadHTMLComponent(profile, "#Base-Container", "div", "./templates/profile.html");
profile = await loadHTMLComponent(profile, "#kit-templates", "div", "./templates/kit.html");
profile.window.document.getElementById("FullName").defaultValue = req.session.name;
profile.window.document.getElementById("Email").defaultValue = req.session.email;
if (req.session.easterEgg) {
profile = applyEasterEggStyle(profile);
}
res.send(profile.serialize());
});
app.patch('/profile', async(req, res) => {
// if the request is not coming from a logged in user, reject.
if (checkLoginError(req, res)) {
return;
}
// get user from db
const filterQuery = {emailAddress: req.session.email};
const userResults = await db.collection('BBY-6_users').find(filterQuery).toArray();
if(userResults.length === 0) {
// Could not find user
res.status(500).send("userNotFound");
return;
}
// based on what is present in the update query,
const updateQuery = {};
if (req.body.email) {
// email to be changed
updateQuery['emailAddress'] = req.body.email;
}
if (req.body.name) {
// name to be changed
updateQuery['name'] = req.body.name;
}
if (req.body.pwd) {
// password to be changed
const pwd = await bcrypt.hash(req.body.pwd, 10);
updateQuery['pwd'] = pwd;
}
if(updateQuery === {}){
// invalid body, reject request
res.status(400).send("missingBodyArgument(s)");
return;
}
try {
const results = await db.collection('BBY-6_users').updateOne(filterQuery, { $set: updateQuery});
if(results.matchedCount === 0) {
// Couldn't find the user
res.status(404).send("userNotFound");
} else {
req.session.name = req.body.name ?? req.session.name;
req.session.email = req.body.email ?? req.session.email;
res.status(200).send("userUpdated");
}
} catch(e) {
log.info(e);
// Email addresses have a unique index so mongo will give error code 11000 if the email is already in use
if(e.code === 11000) {
res.status(403).send("emailInUse");
} else {
res.status(500).send("serverIssue");
}
}
})
app.get('/avatar', async(req, res) => {
//if the request is not coming from a logged in user, reject.
if (checkLoginError(req, res)) {
return;
}
const results = await db.collection('BBY-6_users').findOne({emailAddress:req.session.email});
let data, mimeType;
if (results?.avatar === null) {
// TODO: factor this out into a separate function/global constant/something.
data = await readFile("./public/images/Default-Profile-Picture.txt", "utf-8");
mimeType = 'image/jpeg';
}
res.status(200).send({
mimeType: results.avatar?.contentType ?? mimeType,
data: results.avatar?.data?.$binary?.base64 ?? data
})
})
app.post('/avatar', upload.single("avatar"), async(req, res) => {
//if the request is not coming from a logged in user, reject.
if (checkLoginError(req, res)) {
return;
}
const results = await db.collection('BBY-6_users').updateOne({emailAddress:req.session.email}, {
$set: {
avatar: {
data: {
$binary: {
base64: req.file.buffer
}
},
contentType: req.file.mimetype
}
}
})
res.status(200).send("avatarUploaded");
})
app.get('/login', async function (req, res) {
// Redirect to profile page if logged in
if (req.session.loggedIn) {
res.redirect("/profile");
return;
}
let doc = await readFile("./html/login.html", "utf8");
const baseDOM = new JSDOM(doc);
let login = await loadHeaderFooter(baseDOM);
login = changeLoginButton(login, req);
res.send(login.serialize());
});
app.get('/dashboard', async function (req, res) {
if (req.session.isAdmin) {
let dashboardDoc = fs.readFileSync("./html/dashboard.html", "utf-8");
const baseDOM = new JSDOM(dashboardDoc);
let dashboard = await loadHeaderFooter(baseDOM);
dashboard = changeLoginButton(dashboard, req);
let profileDetails = await loadHTMLComponent(dashboard, "#Base-Container", "div", "./templates/profile.html");
profileDetails = await loadHTMLComponent(dashboard, "#kit-templates", "div", "./templates/kit.html");
profileDetails.window.document.getElementById("FullName").defaultValue = req.session.name;
profileDetails.window.document.getElementById("Email").defaultValue = req.session.email;
res.send(dashboard.serialize());
return;
} else {
// Unauthorized TODO: test
res.redirect('/profile');
}
});
// This gets the admin profiles
// May change endpoint name to be more descriptive, or not
app.get('/profiles', async function (req, res) {
if (!req.session.isAdmin) {
res.status(401).send('notAnAdmin');
// TODO: log unauthorized access?
return;
}
// TODO: error handling
// TODO: really, we should do pagination for this kind of request.
const usersCursor = db.collection('BBY-6_users').find({'admin': true})
// Omit password hashes!
// There is probably a better way of doing this; any suggestions?
.map(user => {
delete user.pwd;
return user;
});
res.json(
{
page: 0,
pageSize: null,
result: await usersCursor.toArray()
}
);
})
/**
* @typedef { {
* _id: ?mdb.ObjectId,
* name: string,
* emailAddress: string,
* pwd: string,
* admin: boolean,
* avatar: ?object,
* dateJoined: ?Date,
* achievements: ?string[],
* kits: ?object[]
* } } UserDoc
* */
app.post('/create-user', upload.single('avatar'), async function (req, res) {
// req.file is the value of 'avatar' specified in the formData
if (!req.session.isAdmin) {
res.status(401).send('notAnAdmin');
return;
}
try {
// Default values
const uploadedAvatar = req.file ? {
data: {
$binary: {
base64: req.file.buffer
}
},
contentType: req.file.mimetype
} : undefined;
const defaultProfilePicture = await readFile("./public/images/Default-Profile-Picture.txt", "utf-8");
const defaultAvatar = {
data: {
$binary: {
base64: defaultProfilePicture
}
},
contentType: 'image/jpeg'
}
const defaultAchievements = ["gettingStarted", "planKit", "finishKit"];
const defaultKits = [];
/** @type {UserDoc} */
const newUserDoc = {
_id: req.body?._id ?? undefined,
name: req.body?.name ?? undefined,
emailAddress: req.body?.emailAddress ?? undefined,
pwd: req.body?.pwd ?? undefined,
admin: (req.body?.admin == 'true') ? true : false, // Doing this because formdata can't send booleans
avatar: uploadedAvatar ?? defaultAvatar,
dateJoined: req.body?.dateJoined ?? undefined,
achievements: req.body?.achievements ?? defaultAchievements,
kits: req.body?.kits ?? defaultKits
};
const requiredFields = ["name", "emailAddress", "pwd"];
const acceptable = requiredFields.reduce(
(validSoFar, current) => validSoFar && (newUserDoc[current] !== undefined), true
);
if (!acceptable) {
const missingFields = requiredFields.filter(field => newUserDoc[field] === undefined);
throw new Error(`"Create user" request missing required fields: ${missingFields.join(', ')}`);
}
// All required fields present, time to convert the data into a good format.
const hashPassword = bcrypt.hash(newUserDoc.pwd, 10);
let convertedDate = new Date(newUserDoc.dateJoined);
if (convertedDate.valueOf() === NaN) {
convertedDate = new Date();
}
try {
await db.collection('BBY-6_users').insertOne({
name: newUserDoc.name,
emailAddress: newUserDoc.emailAddress,
pwd: await hashPassword,
avatar: newUserDoc.avatar,
dateJoined: convertedDate,
achievements: newUserDoc.achievements,
admin: newUserDoc.admin,
kits: newUserDoc.kits
});
res.status(200).send("createdUserSuccess");
} catch(err) {
// Error inserting into database!
// Later on, log. For now, just rethrow.
throw (err);
}
} catch(err) {
// Can catch the error and display it here, but Arron says log.info is not allowd
if (err.code === 11000) {
res.status(500).send("duplicateKey");
return;
}
res.status(500).send("serverIssue");
}
});
// Update the admin's name
// TODO: fix response text
app.post('/edit-admin', async function (req, res) {
if (!req.session.isAdmin) {
res.status(401).send('notAnAdmin');
// TODO: log unauthorized access?
return;
}
// The next few chunks of code was copied/adapted from jay/alex's work on the profile page
// Get user from db
const filterQuery = {'_id': new mdb.ObjectId(req.body._id)};
const userResults = await db.collection('BBY-6_users').find(filterQuery).toArray();
if(userResults.length === 0) {
// Could not find user
res.status(500).send("userNotFound");
return;
}
// based on what is present in the update query,
const updateQuery = {};
if (req.body.name) {
// name to be changed
updateQuery['name'] = req.body.name;
}
if(updateQuery === {}){
// invalid body, reject request
res.status(400).send("missingBodyArgument(s)");
return;
}
try {
const results = await db.collection('BBY-6_users').updateOne(filterQuery, { $set: updateQuery});
if(results.matchedCount === 0) {
// Couldn't find the user
res.status(404).send("userNotFound");
} else {
// Edit session name if the req is the same person
if (req.session._id === req.body._id) {
req.session.name = req.body.name;
}
res.status(200).send("userUpdated");
}
} catch(e) {
res.status(500).send("serverIssue");
}
});
// Delete admin users
// TODO: fix response text, review and test this.......
app.post('/delete-admin', async function(req, res) {
if (!req.session.isAdmin) {
res.status(401).send('notAnAdmin');
// TODO: log unauthorized access?
return;
}
// Get user from db
const filterQuery = {'_id': new mdb.ObjectId(req.body._id)};
const userResults = await db.collection('BBY-6_users').find(filterQuery).toArray();
if(userResults.length === 0) {
// Could not find user
res.status(500).send("userNotFound");
return;
}
try {
if (req.session._id === req.body._id) {
// TODO: review/revise the response text
res.status(405).send("deleteAdminFailed");
} else {
db.collection('BBY-6_users').deleteOne(filterQuery);
res.status(200).send("deleteAdminSuccessful");
}
} catch (e) {
res.status(500).send("serverIssue");
}
});
app.get('/kits', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
const filterQuery = {'_id': new mdb.ObjectId(req.session._id)};
const user = await db.collection('BBY-6_users').findOne(filterQuery);
if(!user) {
// Could not find user
res.status(500).send("userNotFound");
return;
}
res.status(200).send(user.kits);
})
app.post('/kits', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
try {
const filterQuery = {'name': req.body.name};
const kitTemplate = await db.collection('BBY-6_kit-templates').findOne(filterQuery);
if(!kitTemplate) {
res.status(500).send("kitTemplateNotFound");
return;
}
try {
const userFilterQuery = {'_id': new mdb.ObjectId(req.session._id)};
kitTemplate.kit = kitTemplate.kit.map(element => {
element.required = true;
element.completed = false;
return element;
})
const user = await db.collection('BBY-6_users').updateOne(userFilterQuery, {$push: {kits: kitTemplate}});
if(!user) {
// Could not find user
res.status(500).send("userNotFound");
return;
}
res.status(200).send(kitTemplate);
} catch(e) {
res.status(500).send("serverIssue");
}
} catch(e) {
res.status(500).send("serverIssue");
}
})
app.patch('/kits', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
if(!req.body || !req.body._id || !req.body.itemId || req.body.completed === undefined) {
res.status(400).send("missingBodyArguments");
return;
}
const filterQuery = {
'_id': new mdb.ObjectId(req.session._id),
'kits._id': new mdb.ObjectId(req.body._id),
}
try {
const result = await db.collection('BBY-6_users').updateOne(filterQuery, {$set: {"kits.$[i].kit.$[j].completed": req.body.completed}},
{
arrayFilters: [
{"i._id": new mdb.ObjectId(req.body._id)},
{"j._id": new mdb.ObjectId(req.body.itemId)}
]
}
);
if(!result) {
// Could not find user
res.status(500).send("couldNotFindUser");
return;
} else if (result.matchedCount === 0) {
// Could not find kit
res.status(404).send("couldNotFindKit");
return;
}
res.status(200).send("kitUpdatedSuccessfully");
} catch(e) {
res.status(500).send("serverIssue");
}
})
app.delete('/kits', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
if(!req.body || !req.body._id) {
res.status(400).send("missingBodyArguments");
return;
}
const filterQuery = {
'_id': new mdb.ObjectId(req.session._id),
'kits._id': new mdb.ObjectId(req.body._id)
}
try {
req.body._id = new mdb.ObjectId(req.body._id);
const result = await db.collection('BBY-6_users').updateOne(filterQuery, {$pull: {"kits": {"_id": new mdb.ObjectId(req.body._id)}}});
if(!result) {
// Could not find user
res.status(500).send("couldNotFindUser");
return;
} else if (result.matchedCount === 0) {
// Could not find kit
res.status(404).send("couldNotFindKit");
return;
}
res.status(200).send("kitDeletedSuccessfully");
} catch(e) {
log.error(e)
res.status(500).send("serverIssue");
}
})
app.post('/add-item', upload.single('image'), async(req, res) => {
if (redirectToLogin(req, res)) {
return;
}
if(!req.body || !req.body._id || !req.body.itemProps || !req.file) {
res.status(400).send("missingBodyArguments");
return;
}
const filterQuery = {
'_id': new mdb.ObjectId(req.session._id),
'kits._id': new mdb.ObjectId(req.body._id)
}
const itemProps = JSON.parse(req.body.itemProps);
const newItem = {
name: itemProps.name,
quantity: itemProps.quantity,
description: itemProps.description,
image: {
data: {
$binary: {
base64: req.file.buffer
}
},
contentType: req.file.mimetype
},
completed: itemProps.completed,
required: itemProps.required,
_id: new mdb.ObjectId()
}
try {
const result = await db.collection('BBY-6_users').updateOne(filterQuery, {$push: {"kits.$.kit": newItem}});
if(!result) {
// Could not find user
res.status(500).send("couldNotFindUser");
return;
} else if (result.matchedCount === 0) {
// Could not find kit
res.status(404).send("couldNotFindKit");
return;
}
res.status(200).send("itemAddedSuccessfully");
} catch(e) {
res.status(500).send("serverIssue");
}
})
app.patch('/edit-item', async (req,res) => {
if (redirectToLogin(req, res)) {
return;
}
if(!req.body || !req.body._id || !req.body.itemProps) {
res.status(400).send("missingBodyArguments");
return;
}
const filterQuery = {
'_id': new mdb.ObjectId(req.session._id),
'kits._id': new mdb.ObjectId(req.body._id),
}
try {
req.body.itemProps._id = new mdb.ObjectId(req.body.itemProps._id);
const result = await db.collection('BBY-6_users').updateOne(filterQuery, {$set: {"kits.$[i].kit.$[j]": req.body.itemProps}},
{
arrayFilters: [
{"i._id": new mdb.ObjectId(req.body._id)},
{"j._id": new mdb.ObjectId(req.body.itemProps._id)}
]
}
);
if(!result) {
// Could not find user
res.status(500).send("couldNotFindUser");
return;
} else if (result.matchedCount === 0) {
// Could not find kit
res.status(404).send("couldNotFindKit");
return;
}
res.status(200).send("kitUpdatedSuccessfully");
} catch(e) {
log.info(e)
res.status(500).send("serverIssue");
}
})
app.delete('/delete-item', async (req, res) => {
if (redirectToLogin(req, res)) {
return;
}
if(!req.body || !req.body._id || !req.body.itemId) {
res.status(400).send("missingBodyArguments");
return;
}
const filterQuery = {
'_id': new mdb.ObjectId(req.session._id),
'kits._id': new mdb.ObjectId(req.body._id)
}
try {
req.body._id = new mdb.ObjectId(req.body._id);
const result = await db.collection('BBY-6_users').updateOne(filterQuery,
{
$pull: {
"kits.$[i].kit": {
"_id": new mdb.ObjectId(req.body.itemId),
"required": false
}
}
},
{
arrayFilters: [
{"i._id": new mdb.ObjectId(req.body._id)}
]
}
);
if(!result) {
// Could not find user
res.status(500).send("couldNotFindUser");
return;
} else if (result.matchedCount === 0) {
// Could not find kit
res.status(404).send("couldNotFindKit");
return;
} else if (result.modifiedCount === 0) {
res.status(401).send("cannotDeleteRequiredItem");
return;
}
res.status(200).send("itemDeletedSuccessfully");
} catch(e) {
res.status(500).send("serverIssue");
}
})
app.get('/kit-templates', async (req, res) => {
const results = await db.collection('BBY-6_kit-templates').find({}).toArray();
res.status(200).send(results);
})
app.get('/login', function (req, res) {
let doc = fs.readFileSync("./html/login.html", "utf-8");
res.send(doc);
});
app.post("/login", async (req, res) => {
const email = req.body.email;
const pwd = req.body.password;
// Set response header regardless of success/failure
res.setHeader("Content-Type", "application/json");
try {
const results = await db.collection('BBY-6_users').find({emailAddress: email}).toArray();
if(results.length === 0) {
// Could not find user
res.status(401).send("userNotFound");
return;