diff --git a/apps/backend/src/modules/course/controller.ts b/apps/backend/src/modules/course/controller.ts index 697463933..8632a0ee0 100644 --- a/apps/backend/src/modules/course/controller.ts +++ b/apps/backend/src/modules/course/controller.ts @@ -58,10 +58,19 @@ export const getClassesByCourse = async ( }; export const getAssociatedCourses = async (courses: string[]) => { - const queries = courses.map((course) => ({ - "subjectArea.code": course.split(" ")[0], - "catalogNumber.formatted": course.split(" ")[1], - })); + console.log(courses); + + const queries = courses.map((course) => { + const split = course.split(" "); + + const subject = split.slice(0, -1).join(" "); + const number = split[split.length - 1]; + + return { + "subjectArea.code": subject, + "catalogNumber.formatted": number, + }; + }); const associatedCourses = await CourseModel.find({ $or: queries, @@ -69,7 +78,22 @@ export const getAssociatedCourses = async (courses: string[]) => { .sort({ fromDate: -1 }) .lean(); - return associatedCourses.map(formatCourse); + console.log(associatedCourses); + + return ( + associatedCourses + // TODO: Properly filter out duplicates in the query + .filter( + (course, index) => + associatedCourses.findIndex( + (associatedCourse) => + associatedCourse.subjectArea?.code === course.subjectArea?.code && + course.catalogNumber?.formatted === + associatedCourse.catalogNumber?.formatted + ) === index + ) + .map(formatCourse) + ); }; export const getCourses = async (info: GraphQLResolveInfo) => { diff --git a/apps/backend/src/modules/course/formatter.ts b/apps/backend/src/modules/course/formatter.ts index b446f2be5..5afc64af8 100644 --- a/apps/backend/src/modules/course/formatter.ts +++ b/apps/backend/src/modules/course/formatter.ts @@ -25,10 +25,14 @@ export function formatCourse(course: CourseType) { classes: null, crossListing: course.crossListing?.courses ?? [], requiredCourses: - course.preparation?.requiredCourses.map?.( - (course) => - `${course.subjectArea?.code} ${course.catalogNumber?.formatted}` - ) ?? [], + course.preparation?.requiredCourses.map?.((course) => { + const split = course.displayName?.split(" ") as string[]; + + const subject = split.slice(0, -1).join(" "); + const number = split[split.length - 1]; + + return `${subject} ${number}`; + }) ?? [], requirements: course.preparation?.requiredText, description: course.description as string, diff --git a/apps/frontend/src/app/Discover/index.tsx b/apps/frontend/src/app/Discover/index.tsx index cf7170df1..4cc2f724b 100644 --- a/apps/frontend/src/app/Discover/index.tsx +++ b/apps/frontend/src/app/Discover/index.tsx @@ -1,6 +1,6 @@ import { FormEvent, useState } from "react"; -import { useApolloClient, useQuery } from "@apollo/client"; +import { useApolloClient } from "@apollo/client"; import { ArrowRight, Calendar } from "iconoir-react"; import AverageGrade from "@/components/AverageGrade"; @@ -10,13 +10,7 @@ import CourseDrawer from "@/components/CourseDrawer"; import Footer from "@/components/Footer"; import NavigationBar from "@/components/NavigationBar"; import Units from "@/components/Units"; -import { - GET_COURSE, - GET_COURSES, - GetCoursesResponse, - ICourse, - Semester, -} from "@/lib/api"; +import { GET_COURSE, ICourse, Semester } from "@/lib/api"; import styles from "./Discover.module.scss"; import Placeholder from "./Placeholder"; @@ -33,10 +27,6 @@ export default function Discover() { const [courses, setCourses] = useState([]); const apolloClient = useApolloClient(); - const { data } = useQuery(GET_COURSES); - - console.log(data); - const getCourse = async (name: string) => { const [subject, courseNumber] = name.split(" ");