diff --git a/src/server/trpc/router/validator.ts b/src/server/trpc/router/validator.ts index 70232cb76..b6a457bdd 100644 --- a/src/server/trpc/router/validator.ts +++ b/src/server/trpc/router/validator.ts @@ -77,6 +77,9 @@ export const validatorRouter = router({ const coReqHash = new Map, number]>>(); const preReqHash = new Map, number]>>(); const coOrPreReqHash = new Map, number]>>(); + // Regex to parse course from description of improperly parsed course + const re = /\b[A-Z]{2,4} \d{4}\b/; + /* Recursive function to check for prereqs. * TODO: Move to a client side function. Possibly a hook. */ @@ -105,8 +108,13 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -124,8 +132,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } @@ -150,8 +156,13 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -171,8 +182,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } if (count >= requirements.required) { @@ -196,8 +205,13 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -217,8 +231,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } if (count >= requirements.required) {