Skip to content

Conversation

@atauln
Copy link

@atauln atauln commented Nov 20, 2025

#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.

@atauln atauln marked this pull request as ready for review December 10, 2025 21:22
@atauln atauln self-assigned this Dec 10, 2025
@atauln atauln moved this from Needs Planning to In Progress in Portal Development Dec 10, 2025
@atauln
Copy link
Author

atauln commented Dec 10, 2025

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.

@atauln atauln added the enhancement New feature or request label Dec 10, 2025
@atauln atauln changed the title db routes split route refactoring Dec 12, 2025
@atauln atauln changed the title route refactoring Route Refactoring Dec 12, 2025
@atauln atauln added this to the 1.9 milestone Dec 12, 2025
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");
Copy link
Contributor

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) {
Copy link
Contributor

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;
}
Copy link
Contributor

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.

Copy link
Contributor

@Rtyujklop Rtyujklop left a 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",
Copy link
Contributor

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

Comment on lines +105 to +113
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)
Copy link
Contributor

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",
Copy link
Contributor

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:
Copy link
Contributor

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);
Copy link
Contributor

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(",")})
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants