-
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.
* basic implementation * completed UI * basic backend connection * view calendar * preliminary calendar complete * fixed issue with program enrollment * remove unnecssary logs
- Loading branch information
Showing
30 changed files
with
1,128 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
/** | ||
* Function handlers for calendar route requests | ||
*/ | ||
import { RequestHandler } from "express"; | ||
import createHttpError from "http-errors"; | ||
|
||
import EnrollmentModel from "../models/enrollment"; | ||
import SessionModel from "../models/session"; | ||
|
||
import { Calendar, CalendarSlot } from "./types/calendarTypes"; | ||
|
||
/** | ||
* Calendar Body: { | ||
* | ||
* studentId: string; | ||
* programId: string; | ||
* calendar: { | ||
* date: Date; | ||
* hours: number; | ||
* session: string; | ||
* }[] | ||
* | ||
* } | ||
*/ | ||
|
||
/** | ||
* Request handler for getting calendar for student in program | ||
* @param req | ||
* @param res | ||
* @param next | ||
*/ | ||
export const getCalendar: RequestHandler = async (req, res, next) => { | ||
try { | ||
const studentId = req.params.studentId; | ||
const programId = req.params.programId; | ||
|
||
const enrollment = EnrollmentModel.find({ studentId, programId }); | ||
if (!enrollment) { | ||
throw createHttpError(404, "Enrollment not found"); | ||
} | ||
|
||
// get all sessions with studentId and programId | ||
const sessions = await SessionModel.find({ programId }); | ||
|
||
const calendar: Calendar = { studentId, programId, calendar: [] }; | ||
for (const session of sessions) { | ||
for (const student of session.students) { | ||
if (student.studentId.toString() === studentId) { | ||
let hours = 0; | ||
if (session.marked) { | ||
hours = student.hoursAttended; | ||
} | ||
const date = session.date; | ||
const sessionId = session._id.toString(); | ||
calendar.calendar.push({ date, hours, session: sessionId }); | ||
} | ||
} | ||
} | ||
console.log(calendar); | ||
|
||
return res.status(200).send(calendar); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
/** | ||
* Handler for editing a day in a calendar | ||
* @param req | ||
* @param res | ||
* @param next | ||
*/ | ||
export const editCalendar: RequestHandler = async (req, res, next) => { | ||
try { | ||
const studentId = req.params.studentId; | ||
const programId = req.params.programId; | ||
|
||
const enrollment = await EnrollmentModel.findOne({ studentId, programId }); | ||
if (!enrollment) { | ||
throw createHttpError(404, "Enrollment not found"); | ||
} | ||
|
||
const { hours, session } = req.body as CalendarSlot; | ||
|
||
const sessionObject = await SessionModel.findById(session); | ||
|
||
if (!sessionObject) { | ||
throw createHttpError(404, "Session not found"); | ||
} | ||
|
||
if (sessionObject.programId.toString() !== programId) { | ||
throw createHttpError(404, "Incorrect program for session"); | ||
} | ||
|
||
const student = sessionObject.students.find((s) => s.studentId.toString() === studentId); | ||
|
||
if (!student) { | ||
throw createHttpError(404, "Student not in session"); | ||
} | ||
|
||
const prevHoursAttended = student.hoursAttended; | ||
let hoursLeft = enrollment.hoursLeft + prevHoursAttended; | ||
|
||
student.hoursAttended = hours; | ||
hoursLeft -= student.hoursAttended; | ||
enrollment.hoursLeft = hoursLeft > 0 ? hoursLeft : 0; | ||
|
||
await sessionObject.save(); | ||
await enrollment.save(); | ||
|
||
res.status(200).send("Updated"); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
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,11 @@ | ||
export type CalendarSlot = { | ||
date: Date; | ||
hours: number; | ||
session: string; | ||
}; | ||
|
||
export type Calendar = { | ||
studentId: string; | ||
programId: string; | ||
calendar: CalendarSlot[]; | ||
}; |
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,20 @@ | ||
/** | ||
* Calendar route requests | ||
*/ | ||
import express from "express"; | ||
|
||
import * as CalendarController from "../controllers/calendar"; | ||
import { verifyAuthToken } from "../validators/auth"; | ||
import * as CalendarValidator from "../validators/calendar"; | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/:studentId/:programId", [verifyAuthToken], CalendarController.getCalendar); | ||
router.patch( | ||
"/:studentId/:programId", | ||
[verifyAuthToken], | ||
CalendarValidator.editCalendar, | ||
CalendarController.editCalendar, | ||
); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Task route requests. | ||
* Student route requests. | ||
*/ | ||
|
||
import express from "express"; | ||
|
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,33 @@ | ||
import { body } from "express-validator"; | ||
|
||
// const makeDateValidator = () => | ||
// body("date") | ||
// .exists() | ||
// .withMessage("date needed") | ||
// .bail() | ||
// .isDate() | ||
// .bail(); | ||
|
||
const makeHoursValidator = () => | ||
body("hours") | ||
.exists() | ||
.withMessage("hours needed") | ||
.bail() | ||
.isNumeric() | ||
.withMessage("needs to be number") | ||
.bail(); | ||
|
||
const makeSessionValidator = () => | ||
body("session") | ||
.exists() | ||
.withMessage("sessionId needed") | ||
.bail() | ||
.isString() | ||
.withMessage("needs to be string") | ||
.bail(); | ||
|
||
export const editCalendar = [ | ||
// makeDateValidator(), | ||
makeHoursValidator(), | ||
makeSessionValidator(), | ||
]; |
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 @@ | ||
import { GET, PATCH, createAuthHeader, handleAPIError } from "../api/requests"; | ||
|
||
import type { APIResult } from "../api/requests"; | ||
|
||
export type CalendarResponse = { | ||
studentId: string; | ||
programId: string; | ||
calendar: { | ||
date: Date; | ||
hours: number; | ||
session: string; | ||
}[]; | ||
}; | ||
|
||
export async function getCalendar( | ||
studentId: string, | ||
programId: string, | ||
firebaseToken: string, | ||
): Promise<APIResult<CalendarResponse>> { | ||
try { | ||
const headers = createAuthHeader(firebaseToken); | ||
const response = await GET(`/calendar/${studentId}/${programId}`, headers); | ||
const json = (await response.json()) as CalendarResponse; | ||
return { success: true, data: json }; | ||
} catch (error) { | ||
return handleAPIError(error); | ||
} | ||
} | ||
|
||
export async function editCalendar( | ||
studentId: string, | ||
programId: string, | ||
firebaseToken: string, | ||
hours: number, | ||
session: string, | ||
): Promise<APIResult<string>> { | ||
try { | ||
const headers = createAuthHeader(firebaseToken); | ||
const body = { hours, session }; | ||
const res = await PATCH(`/calendar/${studentId}/${programId}`, body, headers); | ||
const json = (await res.json()) as string; | ||
return { success: true, data: json }; | ||
} catch (error) { | ||
return handleAPIError(error); | ||
} | ||
} |
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
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.