Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: 🐛 endpoint returns array of school objects
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-wang0 committed Apr 24, 2024
1 parent b7a47d8 commit 5f7cd1b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions apps/api/src/routes/v1/rest/websoc/+endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PrismaClient } from "@libs/db";
import { createHandler } from "@libs/lambda";
import type { WebsocAPIResponse } from "@libs/uc-irvine-lib/websoc";
import type { WebsocCourse } from "@libs/uc-irvine-lib/websoc";
import type { WebsocAPIResponse, WebsocSchool } from "@libs/uc-irvine-lib/websoc";
import { notNull } from "@libs/utils";
import { combineAndNormalizeResponses, sortResponse } from "@libs/websoc-utils";
import type { z } from "zod";
Expand Down Expand Up @@ -132,25 +131,28 @@ function filterResults(query: z.infer<typeof QuerySchema>, websocResults: Websoc

const excludeRestrictions = query.excludeRestrictionCodes ?? [];

if (excludeRestrictions.length) {
return websocResults.schools.map((school) => {
const filteredDepartments = school.departments.map((department) => {
const filteredCourses = department.courses.reduce((acc: WebsocCourse[], course) => {
const filteredSections = course.sections.filter(
(section) =>
!section.restrictions
.split(/ and | or /)
.some((code: string) => excludeRestrictions.includes(code)),
);
if (filteredSections.length > 0) {
acc.push({ ...course, sections: filteredSections });
}
return acc;
}, []);
return { ...department, courses: filteredCourses };
});
return { ...school, departments: filteredDepartments };
});
if (excludeRestrictions.length > 0) {
websocResults.schools = websocResults.schools
.map((school) => {
school.departments = school.departments
.map((department) => {
department.courses = department.courses
.map((course) => {
course.sections = course.sections.filter(
(section) =>
!section.restrictions
.split(/ and | or /)
.some((code: string) => excludeRestrictions.includes(code)),
);
return course;
})
.filter((course) => course.sections.length > 0);
return { ...department, courses: department.courses };
})
.filter((department) => department.courses.length > 0);
return { ...school, departments: school.departments };
})
.filter((school) => school.departments.length > 0) satisfies WebsocSchool[];
}

return websocResults;
Expand Down

0 comments on commit 5f7cd1b

Please sign in to comment.