-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.js
458 lines (431 loc) · 13.6 KB
/
routes.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
/**
* This file contains the translation of route functions
* for ./MakeMyFuture's app POST and GET functions. Each
* function here takes a request and response, and performs
* a body of operations to achieve a needed effect.
*
*
* @file routes.js
* @authors Pirjot Atwal,
* @version 01/06/2022
*/
//NEEDED REQUIREMENTS (INCLUDE NEW MODULES AS NEEDED)
const mongo = require('./mongodb-library.js');
const accounts = require('./accounts.js')
const fs = require('fs');
/**
* Sign up an account, manipulating the user's cookies to store their special session ID.
*
* @param {*} req expects req.body to be equivalent to a JSON of the following structure
* {
* username: {STRING},
* password: {STRING},
* email: {STRING}
* }
* @param {*} res
* @returns a response on whether the user's request was successful. If it was,
* a session is automatically issued and the cookie is set. Of the form:
* {
* info: "ACCOUNT CREATED" / ...
* account_created: true / false
* }
*/
async function sign_up(req, res) {
//TODO: Perform some error checking possibly, parsing, cleaning up,etc.
let username = req.body.username;
let password = req.body.password;
let email = req.body.email;
//Sign up the account
let sign_up_response = await accounts.sign_up(username, password, email);
if (sign_up_response["account_created"]) {
//Issue a new session using the account's _id
let session_response = await accounts.issue_session(sign_up_response["user_id"]);
res.cookie("session", session_response["hash"], {
maxAge: 5 * 24 * 60 * 60 * 1000,
httpOnly: true
});
}
res.send({
"info": sign_up_response["info"],
"account_created": sign_up_response["account_created"]
});
}
/**
* Login in a user. Sets session in cookie (for 24 hours.)
* @param {*} req The request should have a body that has the following structure:
* {
* username: {STRING},
* password: {STRING}
* }
* @param {*} res The result is a JS object of the following structure:
* {
* loggedIn: true / false,
* info: {STRING},
* }
*/
async function login(req, res) {
//TODO: Perform some error checking possibly, parsing, cleaning up, etc.
let username = req.body.username;
let password = req.body.password;
let login_response = {
info: "FAILED",
"loggedIn": false
}
try {
login_response = await accounts.login(username, password);
if (login_response["loggedIn"]) {
//Issue a new session using the account's _id
let session_response = await accounts.issue_session(login_response["user_id"]);
res.cookie("session", session_response["hash"], {
maxAge: 5 * 24 * 60 * 60 * 1000,
httpOnly: true
});
}
} catch (error) {
console.log("An Error Occurred in the Login Function... " + error.message);
}
res.send({
"info": login_response["info"],
"loggedIn": login_response["loggedIn"]
});
}
/**
* Logout, deleting the session cookie.
* @param {*} req
* @param {*} res
* {
* info: [STRING]
* }
*/
async function logout(req, res) {
try {
let x = req.cookies["session"];
} catch (error) {
res.send({
"info": "You're not signed in."
});
return;
}
res.clearCookie("session");
res.send({
"info": "You have been logged out."
});
}
/**
* Post a user's schedule to the database.
* @param {*} req A request object with a body of the following structure:
* {
* schedule: SCHEDULE STRUCTURE
* }
* @param {*} res
* {
* success: true / false
* }
*/
async function post_schedule(req, res) {
let verify_response = await accounts.verify_session(req.cookies["session"]);
let update_response = {
success: false
};
if (verify_response["valid"]) {
req.body.schedule.user_id = verify_response.user_id;
req.body.schedule.time_created = (new Date()).getTime();
update_response = await accounts.upload_schedule(verify_response["user_id"], req.body.schedule);
}
res.send(update_response);
}
/**
* A function meant to be run on every page load, verifying if the user's session
* is valid.
* The user will send a Request object in which the cookies are stored.
* If the session cookie exists and the session (hash) is valid, then the session
* is valid. Otherwise, the session is invalid.
*
*
* @param {*} req
* @param {JSON} res An object of the form:
* {
* isSignedIn: true / false,
* username: [STRING],
* data: [STRING]
* }
*/
async function verify_session(req, res) {
let response = {
isSignedIn: false,
username: null,
data: null
};
if (req.cookies["session"] == undefined) {
res.send(response);
return;
}
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
response.isSignedIn = true;
response.username = await accounts.get_account_username(verify_response["user_id"]);
}
res.send(response);
}
/**
* Using the query object in the body, return some data from the catalog json file.
* If the query was not recognized, return the entire file.
*
* @param {JSON} req A JS object with a body equivalent to the following:
* {
* query: [STRING],
* "AREAS" = The AREAS array, holding JS objects that point each AREA ACR to its full name.
* "CLASSES" = The CLASSES array, expecting the acr to also be attached in the acr property.
* "CLASS" = A CLASS object, matching the provided acr.
* }
* @param {*} res An object of the form:
* {
* result: [OF VARYING TYPE DEPENDING ON QUERY]
* }
*/
async function query_data(req, res) {
// Get the data
let data = JSON.parse(fs.readFileSync("2021_2022_class_data.json"));
let query = req.body.query;
if (query == "AREAS") {
res.send(data["AREAS"]);
} else if (query == "CLASSES") {
// Select all classes that match acr.
let classes = [];
for (let course of data["CLASSES"]) {
if (course["AREA"] == req.body.acr) {
classes.push(course);
}
}
res.send(classes);
} else if (query == "CLASS") {
let course = null;
for (let object of data["CLASSES"]) {
if (object["AREA-ACR"] == req.body.acr) {
course = object;
}
}
res.send(course);
} else {
res.send(data);
}
}
/**
* Get the schedules that belong to the user. Returns the schedules as
* an array. If the user is not logged in, returns null.
*
* @param {JSON} req Assumes this is a get method.
* @param {JSON} res Returns a JS object of the following format:
* {
* success: true / false,
* data: [{SCHEDULE FORMAT}] / null
* }
*/
async function get_user_schedules(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
let get_response = {success: false, data: null};
if (verify_response["valid"]) {
let user_data = await accounts.get_user_schedules(verify_response["user_id"]);
get_response.success = true;
get_response.data = user_data;
}
res.send(get_response);
} catch (error) {
res.send({success: false, error: error.message});
}
}
/**
* Create a new template schedule in the accounts database
* given the provided information.
* @param {JSON} req A JS Object with a body of the following structure
* {
* majors: Array of Strings,
* universities: Array of Strings,
* name: String,
* }
* @param {JSON} res A JS object with an info property.
*/
async function create_schedule(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
await accounts.create_schedule(verify_response["user_id"], req.body.majors, req.body.universities, req.body.name);
res.send({"info": "SUCCESS"});
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN SCHEDULE CREATION. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN SCHEDULE CREATION."});
}
/**
* Delete a schedule with a given name.
*
* @param {JSON} req A JS Object with a body of the following type
* {
* name: [STRING]
* }
* @param {*} res
*/
async function delete_schedule(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
await accounts.delete_schedule(verify_response["user_id"], req.body.name);
res.send({"info": "SUCCESS"});
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN SCHEDULE DELETION. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN SCHEDULE DELETION."});
}
/**
* Fetch a schedule for a provided name.
*
* @param {JSON} req A JS object with a body of the following structure:
* {
* name: [STRING]
* }
* @param {JSON} res The Schedule Object in schedule notation
*/
async function fetch_schedule(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
res.send(await accounts.fetch_schedule(verify_response["user_id"], req.body.name));
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN SCHEDULE FETCHING. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN SCHEDULE FETCHING."});
}
/**
* Edit a schedule by either removing or adding a class.
* @param {JSON} req A JS object with a body of the following type
* {
* type: "ADD" / "REMOVE"
name: [STRING],
acr: [STRING],
season: [STRING],
year: [STRING]
* }
* @param {*} res
*/
async function edit_schedule(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
res.send(await accounts.edit_schedule(verify_response["user_id"], req.body.type, req.body.name, req.body.acr, req.body.season, req.body.year));
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN SCHEDULE EDITING. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN SCHEDULE EDITING. " + error.message});
}
/**
* Fetch the JSON file for majors and colleges. (Currently using global majors)
* @param {*} req
* @param {*} res
*/
async function fetch_major_colleges(req, res) {
let data = JSON.parse(fs.readFileSync("major_colleges.json"));
res.send(data);
}
/**
* Fetch all schedules in batch
* @param {*} req A JS object with a body of the following:
* {
* queries: [JSON],
* dateRange: [ARRAY OF NUMBERS]
* sortOption: [STRING],
* matching: [BOOLEAN],
* page: [NUMBER],
* majors: [ARRAY OF STRINGS],
* universities: [ARRAY OF STRINGS]
* }
* @param {*} res An array of schedules
*/
async function fetch_schedules_batch(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
res.send(await accounts.fetch_schedules_batch(req.body.queries, req.body.dateRange, req.body.sortOption, req.body.matching, req.body.majors, req.body.universities, req.body.page));
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN SCHEDULE BATCH FETCHING. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN SCHEDULE BATCH FETCHING. " + error.message});
}
/**
* Fetch a user profile (public information, their schedules, their description, etc.)
* @param {*} req A request with a body of the following type:
* {
* username: [STRING]
* }
* @param {*} res
*/
async function fetch_user_profile(req, res) {
let profile = await accounts.fetch_user_profile(req.body.username);
res.send(profile);
}
/**
* Update a user's account for a certain field in the following categories:
* USERNAME
* EMAIL
* DESCRIPTION
* DELETE
* @param {*} req
* @param {*} res
*/
async function update_account(req, res) {
try {
let verify_response = await accounts.verify_session(req.cookies["session"]);
if (verify_response["valid"]) {
res.send(await accounts.update_account(verify_response["user_id"], req.body.type, req.body));
return;
} else {
res.send({"info": "THE USER IS NOT SIGNED IN."});
return;
}
} catch (error) {
console.log("AN ERROR OCCURRED IN UPDATING THE ACCOUNT. " + error.message);
}
res.send({"info": "AN ERROR OCCURRED IN UPDATING THE ACCOUNT. " + error.message});
}
module.exports = {
sign_up,
login,
logout,
verify_session,
post_schedule,
query_data,
get_user_schedules,
create_schedule,
delete_schedule,
fetch_schedule,
edit_schedule,
fetch_major_colleges,
fetch_schedules_batch,
fetch_user_profile,
update_account
}