Skip to content

Commit

Permalink
fix: remove catalog year and find distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
kamui-fin committed Oct 23, 2023
1 parent 9ba0e8f commit 48b1046
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/server/trpc/router/courseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class CourseCache {
private mutex = new Mutex();

public async getCourses(year: number) {
const formattedYear = year.toString().slice(-2);
// Acquire lock before success check so if another request is fetching, we don't fetch again.
const release = await this.mutex.acquire();
if (this.coursesByYear.has(year)) {
Expand All @@ -24,9 +23,7 @@ class CourseCache {
console.info(`Fetching courses for year ${year}...`);
return await platformPrisma.courses
.findMany({
where: {
catalog_year: formattedYear,
},
distinct: ['title', 'course_number', 'subject_prefix'],
})
.then((courses) => {
this.coursesByYear.set(year, courses);
Expand Down
15 changes: 12 additions & 3 deletions src/server/trpc/router/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export const validatorRouter = router({
* TODO: Fix this later somehow
*/

// internal course id -> course code
const courseInternalToCode = new Map<string, Prisma.JsonValue>();
// course code -> requisite information
const courseCodeToReqs = new Map<
string,
{
Expand All @@ -57,6 +59,7 @@ export const validatorRouter = router({
courseInternalToCode.set(course.internal_course_number, code);
}

// course -> semester index, used to determine if A is taken before B by comparing indices
const courseToSemester = new Map<string, number>();

planData?.semesters.forEach((semester, index) => {
Expand All @@ -66,6 +69,7 @@ export const validatorRouter = router({
});

planData?.transferCredits.forEach((course) => {
// set semester index to -1, indicating credit comes before all semesters
courseToSemester.set(course.trim(), -1);
});

Expand All @@ -75,24 +79,28 @@ export const validatorRouter = router({
requisiteType: RequisiteType,
) => {
if (!requirements || requirements.options.length === 0) return [];
// count the number of requisites not met, if 0 then requisites are satisfied
let numRequisitesNotMet = requirements.required;
const requisitesNotMet: [string[], number][] = [];
// storing course codes of the unmet requirements
const currentUnmetCourses: string[] = [];
// this function's output: [currentUnmetCourses, numRequisitesNotMet]
const requisitesNotMet: [string[], number][] = [];
for (const option of requirements.options) {
if (option.type === 'course') {
const courseCode = courseInternalToCode.get(option.class_reference);
const courseSemesterIndex = courseToSemester.get(courseCode as string);
if (
courseSemesterIndex !== undefined &&
((requisiteType == RequisiteType.PRE && courseSemesterIndex < semester) ||
courseSemesterIndex <= semester)
((requisiteType == RequisiteType.PRE && courseSemesterIndex < semester) || // if pre-req, semester index has to be less than
courseSemesterIndex <= semester) // if coreq, it can be taken in the same semester
) {
// course is satisfied
numRequisitesNotMet--;
} else {
currentUnmetCourses.push(courseCode as string);
}
} else if (option.type === 'collection') {
// recursively gather requisite information
const nestedRequisitesNotMet = checkForRequisites(option, semester, requisiteType);
if (nestedRequisitesNotMet.length > 0) {
requisitesNotMet.push(...nestedRequisitesNotMet);
Expand Down Expand Up @@ -121,6 +129,7 @@ export const validatorRouter = router({
[RequisiteType.CO_PRE]: new Map(),
};

// populates pre-req validation into output object
const validateRequisites = async (planData: PlanData, requisiteType: RequisiteType) => {
planData?.semesters.forEach((semester, index) => {
for (const course of semester.courses) {
Expand Down

0 comments on commit 48b1046

Please sign in to comment.