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

Commit

Permalink
feat: ✨ add restriction code filtering for Websoc lambda client
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-wang0 committed Mar 12, 2024
1 parent 692c486 commit fd7dd19
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
29 changes: 28 additions & 1 deletion apps/api/src/routes/v1/rest/websoc/+endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createHandler } from "@libs/lambda";
import type { WebsocAPIResponse } from "@libs/uc-irvine-lib/websoc";
import { notNull } from "@libs/utils";
import { combineAndNormalizeResponses, sortResponse } from "@libs/websoc-utils";
import type { z } from "zod";
import { ZodError } from "zod";

import { APILambdaClient } from "./APILambdaClient";
Expand Down Expand Up @@ -111,7 +112,9 @@ export const GET = createHandler(async (event, context, res) => {
queries: normalizeQuery(parsedQuery),
});

return res.createOKResult(websocResults, headers, requestId);
const filteredWebsocResults = filterResults(parsedQuery, websocResults);

return res.createOKResult(filteredWebsocResults, headers, requestId);
} catch (error) {
if (error instanceof ZodError) {
const messages = error.issues.map((issue) => issue.message);
Expand All @@ -120,3 +123,27 @@ export const GET = createHandler(async (event, context, res) => {
return res.createErrorResult(400, error, requestId);
}
}, onWarm);

function filterResults(query: z.infer<typeof QuerySchema>, websocResults: WebsocAPIResponse) {
const excludeRestrictions = query.excludeRestrictionCodes ?? [];

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

return websocResults;
}
1 change: 0 additions & 1 deletion apps/api/src/routes/v1/rest/websoc/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ export function constructPrismaQuery(parsedQuery: Query): Prisma.WebsocSectionWh
}

if (parsedQuery.excludeRestrictionCodes) {
console.log(parsedQuery.excludeRestrictionCodes as $Enums.RestrictionCode[]);
AND.push({
NOT: {
restrictionCodes: {
Expand Down
13 changes: 12 additions & 1 deletion apps/api/src/routes/v1/rest/websoc/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { $Enums } from "@libs/db";
import {
anyArray,
cancelledCoursesOptions,
Expand Down Expand Up @@ -73,7 +74,17 @@ export const QuerySchema = z
)
.refine((x) => x.cacheOnly || x.building || !x.room, {
message: 'If "building" is provided, "room" must also be provided',
});
})
.refine(
(x) =>
!x.excludeRestrictionCodes ||
x.excludeRestrictionCodes.every((code) =>
Object.values($Enums.RestrictionCode).includes(code as $Enums.RestrictionCode),
),
{
message: `Restriction codes must be in [${Object.values($Enums.RestrictionCode).join(", ")}]`,
},
);

/**
* Type of the parsed query: useful for passing the query as input to other functions.
Expand Down

0 comments on commit fd7dd19

Please sign in to comment.