-
Notifications
You must be signed in to change notification settings - Fork 2
Route Refactoring #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Route Refactoring #342
Conversation
|
Throughout the development of this revision, I have changed the structure of the routes to accommodate a routes file and a functions file for each subcategory of routes. Simply put, the routes have the actual HTTP requests, and call the associated functions for those routes. Those functions then return a promise to provide data. |
| db_router.use("/", filesRoutes(db)); | ||
| db_router.use("/", semesterRoutes(db)); | ||
| db_router.use("/", devOnlyRoutes(db)); | ||
| db_router.use("/", dashboardRoutes(db)); | ||
| const CONSTANTS = require("../consts"); | ||
| const { ROLES } = require("../consts"); | ||
| const { off } = require("process"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import "const {off} = require ("process")" appears unused, consider removing it to avoid confusion.
| // Attachment Handling | ||
| if (files && files.files) { | ||
| // If there is only one attachment, then it does not come as a list | ||
| if (files.files.length === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before checking files.files.length, consider guarding against files or files.files being undefined to avoid runtime errors when no files are sent.
| if (err) { | ||
| reject(err); | ||
| return; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for getFiles, this is good but one suggestion, maybe collect and return a list of files that failed statSync so callers can optionally log or surface that.
Rtyujklop
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent refactoring that significantly improves code organization and maintainability. Breaking down the monolithic db_routes.js file into domain-specific modules makes the codebase much easier to navigate and extend. Just go over these issues and it should be good.
| "/createAction", | ||
| [UserAuth.isAdmin, UserAuth.canWrite, body("page_html").unescape()], | ||
| db_router.get( | ||
| "/getActiveTimelines", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could move to dashboard_routes.js or a new timelines_routes.js
| db_router.get("/getHtml", (req, res, next) => { | ||
| let getHtmlQuery = `SELECT * FROM page_html`; | ||
| let queryParams = []; | ||
| //If there is a query parameter, then select html from specified table. | ||
| if (typeof req.query.name !== "undefined" && req.query.name) { | ||
| getHtmlQuery = `SELECT html FROM page_html WHERE name = ?`; | ||
| queryParams = [req.query.name]; | ||
| } | ||
| db.query(getHtmlQuery, queryParams) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could move to a new content_routes.js or pages_routes.js For getHTML
| .withMessage("Cannot be empty") | ||
| .isLength({ max: 50 }), | ||
| ], | ||
| "/editPage", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could move to a new content_routes.js or pages_routes.js for /editPage route similar to /getHtml
| let params = []; | ||
|
|
||
| switch (user.type) { | ||
| case ROLES.STUDENT: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a SQL injection risk, While user probably comes from authenticated session data, it's safer to use parameterized queries consistently. you could refactor this function with: Build the filter with placeholders,
return both the filter string and the parameters array, and pass parameters to db.query()
| } | ||
|
|
||
| const darkModeRaw = result[0].dark_mode; | ||
| const dark_mode = Boolean(darkModeRaw); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but explicitly checking values like you do in other view preference functions, is more consistent and handles edge cases better.
| const getPeerEvalLogsQuery = `SELECT action_log.*, users.fname, users.lname, users.type | ||
| FROM action_log | ||
| LEFT JOIN users ON action_log.system_id = users.system_id | ||
| WHERE action_template IN (${actionIds.join(",")}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is vulnerable to SQL injection if action_id values are user-controlled. Maybe use parameterized queries instead.
#309
DB Routes is an unnecessarily long file at the moment. These changes should move a lot of the bulk in it over to separate subfiles.