Skip to content
This repository was archived by the owner on Nov 30, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion server/api/controllers/term-name-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import tokens from "../../../config";

export class TermNameController {
static logger = new Logger("Term Name Controller");
static termCache = new Map<string, string>(); // cache of termCodes

static async getTermNames(): Promise<Term[]> {
const url = `${tokens.WATERLOO_OPEN_API_BASE_URL}/Terms`;
Expand Down Expand Up @@ -37,10 +38,41 @@ export class TermNameController {
}
}

static convertTermCodeToTermName(termCode: string) {
if (this.termCache.has(termCode)) {
return this.termCache.get(termCode); // Return from cache
}
// termCodes are either in the format of abcd or bcd.
// assuming that a term code that starts with "2" i.e: 2bcd is a year in 2100
// bc refers to the year XXbc
let term = parseInt(termCode);
let termYear = 1900;
while (term > 1000) {
termYear += 100;
term -= 1000;
}
let monthNum = term % 10; // in theory, termCodes should always end in either 1, 5, or 9
let season = "FALL";
if (monthNum == 1) {
season = "WINTER";
} else if (monthNum == 5) {
season = "SPRING";
}
term /= 10; // get rid of the ones digit
term = Math.trunc(term);
termYear += term;
const termName = season + " " + termYear;

// Cache the result
this.termCache.set(termCode, termName);

return termName;
}

static getTermNameFromTermCode(termCode: string) {
const terms = this.getTermsFile();
const target = terms.find((item) => item.termCode === termCode);
return target?.name ?? termCode;
return target?.name ?? this.convertTermCodeToTermName(termCode);
}

static async overwriteTermsFile() {
Expand Down