This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ implement degreeworks scraper and requirements endpoint (#140)
Co-authored-by: Aponia <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,090 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type Specialization { | ||
id: String! | ||
majorId: String! | ||
name: String! | ||
requirements: JSON! | ||
} | ||
|
||
type Major { | ||
id: String! | ||
degreeId: String! | ||
code: String! | ||
name: String! | ||
requirements: JSON! | ||
specializations: [Specialization!]! | ||
} | ||
|
||
type Minor { | ||
id: String! | ||
name: String! | ||
requirements: JSON! | ||
} | ||
|
||
type Degree { | ||
id: String! | ||
name: String! | ||
division: DegreeDivision! | ||
majors: [Major!]! | ||
} | ||
|
||
extend type Query { | ||
major(id: String!): Major! | ||
majors(degreeId: String, nameContains: String): [Major!]! | ||
minor(id: String!): Minor! | ||
minors(nameContains: String): [Minor!]! | ||
specialization(id: String!): Specialization! | ||
specializations(nameContains: String): [Specialization!]! | ||
specializationsByMajorId(majorId: String!): [Specialization!]! | ||
allDegrees: [Degree!]! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { PrismaClient } from "@libs/db"; | ||
import { createHandler } from "@libs/lambda"; | ||
|
||
import { ProgramSchema, SpecializationSchema } from "./schema"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
async function onWarm() { | ||
await prisma.$connect(); | ||
} | ||
|
||
const degreeRepository = { | ||
majors: { | ||
findMany: async () => { | ||
return await prisma.major.findMany({ include: { specializations: true } }); | ||
}, | ||
findFirstById: async (id: string) => { | ||
return await prisma.major.findFirst({ where: { id }, include: { specializations: true } }); | ||
}, | ||
findManyNameContains: async (degreeId: string, contains?: string) => { | ||
return await prisma.major.findMany({ | ||
where: { | ||
degreeId, | ||
name: { contains, mode: "insensitive" }, | ||
}, | ||
include: { specializations: true }, | ||
}); | ||
}, | ||
}, | ||
minors: { | ||
findMany: async () => { | ||
return await prisma.minor.findMany({}); | ||
}, | ||
findFirstById: async (id: string) => { | ||
return await prisma.minor.findFirst({ where: { id } }); | ||
}, | ||
}, | ||
}; | ||
|
||
export const GET = createHandler(async (event, context, res) => { | ||
const headers = event.headers; | ||
const params = event.pathParameters ?? {}; | ||
const query = event.queryStringParameters ?? {}; | ||
const requestId = context.awsRequestId; | ||
|
||
switch (params?.id) { | ||
case "all": | ||
return res.createOKResult( | ||
await prisma.degree.findMany({ | ||
include: { majors: { include: { specializations: true } } }, | ||
}), | ||
headers, | ||
requestId, | ||
); | ||
|
||
case "majors": // falls through | ||
case "minors": { | ||
const parsedQuery = ProgramSchema.safeParse(query); | ||
|
||
if (!parsedQuery.success) { | ||
return res.createErrorResult( | ||
400, | ||
parsedQuery.error.issues.map((issue) => issue.message).join("; "), | ||
requestId, | ||
); | ||
} | ||
|
||
switch (parsedQuery.data.type) { | ||
case "id": { | ||
const result = await degreeRepository[params.id].findFirstById(parsedQuery.data.id); | ||
return result | ||
? res.createOKResult(result, headers, requestId) | ||
: res.createErrorResult( | ||
404, | ||
`${params.id === "majors" ? "Major" : "Minor"} with ID ${parsedQuery.data.id} not found`, | ||
requestId, | ||
); | ||
} | ||
|
||
case "degreeOrName": { | ||
const { degreeId, nameContains } = parsedQuery.data; | ||
|
||
if (params.id === "minors" && degreeId != null) { | ||
return res.createErrorResult(400, "Invalid input", requestId); | ||
} | ||
|
||
const result = await degreeRepository.majors.findManyNameContains(degreeId, nameContains); | ||
return res.createOKResult(result, headers, requestId); | ||
} | ||
|
||
case "empty": { | ||
const result = await degreeRepository[params.id].findMany(); | ||
return res.createOKResult(result, headers, requestId); | ||
} | ||
} | ||
break; | ||
} | ||
|
||
case "specializations": { | ||
const parsedQuery = SpecializationSchema.safeParse(query); | ||
|
||
if (!parsedQuery.success) { | ||
return res.createErrorResult( | ||
400, | ||
parsedQuery.error.issues.map((issue) => issue.message).join("; "), | ||
requestId, | ||
); | ||
} | ||
|
||
switch (parsedQuery.data.type) { | ||
case "id": { | ||
const row = await prisma.specialization.findFirst({ where: { id: parsedQuery.data.id } }); | ||
|
||
return row | ||
? res.createOKResult(row, headers, requestId) | ||
: res.createErrorResult( | ||
404, | ||
`Specialization with ID ${parsedQuery.data.id} not found`, | ||
requestId, | ||
); | ||
} | ||
|
||
case "major": { | ||
const result = await prisma.specialization.findMany({ | ||
where: { majorId: parsedQuery.data.majorId }, | ||
}); | ||
return res.createOKResult(result, headers, requestId); | ||
} | ||
|
||
case "name": { | ||
const result = await prisma.specialization.findMany({ | ||
where: { name: { contains: parsedQuery.data.nameContains, mode: "insensitive" } }, | ||
}); | ||
return res.createOKResult(result, headers, requestId); | ||
} | ||
|
||
case "empty": { | ||
const result = await prisma.specialization.findMany(); | ||
return res.createOKResult(result, headers, requestId); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return res.createErrorResult(400, "Invalid endpoint", requestId); | ||
}, onWarm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { z } from "zod"; | ||
|
||
export const ProgramSchema = z | ||
.union([ | ||
z.object({ id: z.string() }), | ||
z.object({ degreeId: z.string().optional(), nameContains: z.string().optional() }), | ||
z.object({}), | ||
]) | ||
.transform((data) => { | ||
if ("id" in data) { | ||
return { type: "id" as const, ...data }; | ||
} | ||
|
||
if ("degreeId" in data && data.degreeId != null) { | ||
return { type: "degreeOrName" as const, degreeId: data.degreeId, ...data }; | ||
} | ||
|
||
return { type: "empty" as const, ...data }; | ||
}); | ||
|
||
export const SpecializationSchema = z | ||
.union([ | ||
z.object({ id: z.string() }), | ||
z.object({ majorId: z.string() }), | ||
z.object({ nameContains: z.string() }), | ||
z.object({}), | ||
]) | ||
.transform((data) => { | ||
if ("id" in data) { | ||
return { type: "id" as const, ...data }; | ||
} | ||
|
||
if ("majorId" in data) { | ||
return { type: "major" as const, ...data }; | ||
} | ||
|
||
if ("nameContains" in data) { | ||
return { type: "name" as const, ...data }; | ||
} | ||
|
||
return { type: "empty" as const, ...data }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.