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(study-rooms): ✨ implement study room endpoints
- Loading branch information
Showing
18 changed files
with
446 additions
and
54 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
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
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,46 @@ | ||
type TimeSlot { | ||
"Date of the time slot (YYYY-MM-DD)." | ||
date: String! | ||
"Start time of the time slot (HH:MM)." | ||
start: String! | ||
"End time of the time slot (HH:MM)." | ||
end: String! | ||
"If the time slot is booked." | ||
booked: Boolean! | ||
} | ||
|
||
type StudyRoom { | ||
"ID of study room used by spaces.lib." | ||
id: ID! | ||
"Name of the study room and its room number." | ||
name: String! | ||
"Number of chairs in the study room." | ||
capacity: Int! | ||
"Name of study location." | ||
location: String! | ||
"Description of the study room." | ||
description: String | ||
"Directions to the study room." | ||
directions: String | ||
"Time slots for the study room." | ||
timeSlots: [TimeSlot]! | ||
"If the study room has TV or other tech enhancements." | ||
techEnhanced: Boolean | ||
} | ||
|
||
type StudyLocation { | ||
"ID of the study location using shortened name of the location." | ||
id: ID! | ||
"Location ID of the study location used by space.lib." | ||
lid: String! | ||
"Name of the study location." | ||
name: String! | ||
"Rooms in the study location." | ||
rooms: [StudyRoom!]! | ||
} | ||
|
||
extend type Query { | ||
"Fetch all study rooms." | ||
allStudyRooms(start: String!, end: String!): [StudyLocation!]! | ||
studyRooms(location: String!, start: String!, end: String!): StudyLocation! | ||
} |
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,30 @@ | ||
import { createHandler } from "@libs/lambda"; | ||
import { studyLocations } from "libs/uc-irvine-lib/src/spaces"; | ||
import { ZodError } from "zod"; | ||
|
||
import { aggreagteStudyRooms } from "./lib"; | ||
import { Query, QuerySchema } from "./schema"; | ||
|
||
export const GET = createHandler(async (event, context, res) => { | ||
const headers = event.headers; | ||
const query = event.queryStringParameters; | ||
const requestId = context.awsRequestId; | ||
let parsedQuery: Query; | ||
try { | ||
parsedQuery = QuerySchema.parse(query); | ||
if (!studyLocations[parsedQuery.location]) { | ||
return res.createErrorResult(404, `Location ${parsedQuery.location} not found`, requestId); | ||
} | ||
return res.createOKResult( | ||
await aggreagteStudyRooms(parsedQuery.location, parsedQuery.start, parsedQuery.end), | ||
headers, | ||
requestId, | ||
); | ||
} catch (e) { | ||
if (e instanceof ZodError) { | ||
const messages = e.issues.map((issue) => issue.message); | ||
return res.createErrorResult(400, messages.join("; "), requestId); | ||
} | ||
return res.createErrorResult(400, e, requestId); | ||
} | ||
}); |
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,60 @@ | ||
import { studyLocations } from "libs/uc-irvine-lib/src/spaces"; | ||
import { getStudySpaces } from "libs/uc-irvine-lib/src/spaces"; | ||
import { TimeSlot, StudyLocation } from "packages/types"; | ||
Check failure on line 3 in apps/api/src/routes/v1/rest/studyRooms/lib.ts GitHub Actions / Check for TypeScript errors
|
||
import { studyRooms } from "virtual:studyRooms"; | ||
|
||
/** | ||
* Data structure of time slots returned by libs.spaces. | ||
*/ | ||
type Slot = { | ||
start: string; | ||
end: string; | ||
itemId: number; | ||
checkSum: string; | ||
className: string; | ||
}; | ||
|
||
/** | ||
* Map time slots to a more readable format. | ||
*/ | ||
export function parseTimeSlots(slots: Slot[]): { [id: string]: TimeSlot[] } { | ||
const timeSlots: { [id: string]: TimeSlot[] } = {}; | ||
slots.forEach((slot) => { | ||
const roomId = slot.itemId.toString(); | ||
const [date, start] = slot.start.split(" "); | ||
const [_, end] = slot.end.split(" "); | ||
const timeSlot: TimeSlot = { | ||
date, | ||
start, | ||
end, | ||
booked: !!slot.className && slot.className === "s-lc-eq-checkout", | ||
}; | ||
if (!timeSlots[roomId]) { | ||
timeSlots[roomId] = [timeSlot]; | ||
} else { | ||
timeSlots[roomId].push(timeSlot); | ||
} | ||
}); | ||
return timeSlots; | ||
} | ||
|
||
/** | ||
* Aggregate study rooms and their time slots into a StudyLocation object. | ||
*/ | ||
export async function aggreagteStudyRooms( | ||
locationId: string, | ||
start: string, | ||
end: string, | ||
): Promise<StudyLocation> { | ||
const spaces = await getStudySpaces(studyLocations[locationId].lid, start, end); | ||
const timeSlotsMap = parseTimeSlots(spaces.slots); | ||
return { | ||
id: locationId, | ||
...studyLocations[locationId], | ||
rooms: Object.entries(timeSlotsMap) | ||
.filter(([id, _]) => studyRooms[id]) | ||
.map(([id, timeSlots]) => { | ||
return { ...studyRooms[id], timeSlots }; | ||
}), | ||
}; | ||
} |
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,13 @@ | ||
import { z } from "zod"; | ||
|
||
export const QuerySchema = z.object({ | ||
location: z.string({ required_error: 'Parameter "location" not provided' }), | ||
start: z | ||
.string({ required_error: 'Parameter "start" not provided' }) | ||
.regex(/^\d{4}-\d{2}-\d{2}$/, { message: "Start date must be in YYYY-MM-DD format" }), | ||
end: z | ||
.string({ required_error: 'Parameter "end" not provided' }) | ||
.regex(/^\d{4}-\d{2}-\d{2}$/, { message: "End date must be in YYYY-MM-DD format" }), | ||
}); | ||
|
||
export type Query = z.infer<typeof QuerySchema>; |
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,11 @@ | ||
import type { ApiPropsOverride } from "@bronya.js/api-construct"; | ||
|
||
import { esbuildOptions, constructs } from "../../../../../../bronya.config"; | ||
|
||
export const overrides: ApiPropsOverride = { | ||
esbuild: esbuildOptions, | ||
constructs: { | ||
functionPlugin: constructs.functionPlugin, | ||
restApiProps: constructs.restApiProps, | ||
}, | ||
}; |
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,41 @@ | ||
import { createHandler } from "@libs/lambda"; | ||
import { studyLocations } from "libs/uc-irvine-lib/src/spaces"; | ||
import { ZodError } from "zod"; | ||
|
||
import { aggreagteStudyRooms } from "../lib"; | ||
|
||
import { Query, QuerySchema } from "./schema"; | ||
|
||
export const GET = createHandler(async (event, context, res) => { | ||
const headers = event.headers; | ||
const query = event.queryStringParameters; | ||
const requestId = context.awsRequestId; | ||
const { id } = event.pathParameters ?? {}; | ||
let parsedQuery: Query; | ||
try { | ||
switch (id) { | ||
case null: | ||
case undefined: | ||
return res.createErrorResult(400, "Location not provided", requestId); | ||
case "all": | ||
parsedQuery = QuerySchema.parse(query); | ||
return res.createOKResult( | ||
await Promise.all( | ||
Object.keys(studyLocations).map(async (locationId) => { | ||
return aggreagteStudyRooms(locationId, parsedQuery.start, parsedQuery.end); | ||
}), | ||
), | ||
headers, | ||
requestId, | ||
); | ||
default: | ||
return res.createErrorResult(400, "Invalid endpoint", requestId); | ||
} | ||
} catch (e) { | ||
if (e instanceof ZodError) { | ||
const messages = e.issues.map((issue) => issue.message); | ||
return res.createErrorResult(400, messages.join("; "), requestId); | ||
} | ||
return res.createErrorResult(400, e, requestId); | ||
} | ||
}); |
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,12 @@ | ||
import { z } from "zod"; | ||
|
||
export const QuerySchema = z.object({ | ||
start: z | ||
.string({ required_error: 'Parameter "start" not provided' }) | ||
.regex(/^\d{4}-\d{2}-\d{2}$/, { message: "Start date must be in YYYY-MM-DD format" }), | ||
end: z | ||
.string({ required_error: 'Parameter "end" not provided' }) | ||
.regex(/^\d{4}-\d{2}-\d{2}$/, { message: "End date must be in YYYY-MM-DD format" }), | ||
}); | ||
|
||
export type Query = z.infer<typeof QuerySchema>; |
Oops, something went wrong.