diff --git a/server/api/controllers/term-name-controller.ts b/server/api/controllers/term-name-controller.ts index f34398c..24945c2 100644 --- a/server/api/controllers/term-name-controller.ts +++ b/server/api/controllers/term-name-controller.ts @@ -6,6 +6,7 @@ import tokens from "../../../config"; export class TermNameController { static logger = new Logger("Term Name Controller"); + static termCache = new Map(); // cache of termCodes static async getTermNames(): Promise { const url = `${tokens.WATERLOO_OPEN_API_BASE_URL}/Terms`; @@ -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() {