-
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.
commit 0b133b8 Author: Michael Sullivan <[email protected]> Date: Tue May 14 09:09:16 2024 -0700 Feature/mraysu/program form v2 (#100) * Update Backend Program Schema * V2 UI * Disabled Editing Program Type * Frontend-backend integration * Lint fixes --------- Co-authored-by: mraysu <[email protected]> Co-authored-by: Adhithya Ananthan <[email protected]> commit e17b509 Author: parth4apple <[email protected]> Date: Tue May 14 09:01:15 2024 -0700 Student and Enrollment Schema modifications (#101) * feat: initial schema * feat: edit routes * feat: test and fix routes
- Loading branch information
1 parent
1498994
commit e078081
Showing
19 changed files
with
424 additions
and
403 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import mongoose, { InferSchemaType } from "mongoose"; | ||
|
||
const enrollmentSchema = new mongoose.Schema({ | ||
studentId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Student", | ||
required: true, | ||
unique: false, | ||
}, | ||
programId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Program", | ||
required: true, | ||
unique: false, | ||
}, | ||
status: { type: String, required: true }, | ||
dateUpdated: { type: Date, required: true, default: Date.now() }, | ||
hoursLeft: { type: Number, required: true }, | ||
schedule: { type: [String], required: true }, | ||
sessionTime: { type: [String], required: true }, | ||
startDate: { type: Date, required: true }, | ||
renewalDate: { type: Date, required: true }, | ||
authNumber: { type: String, required: true }, | ||
}); | ||
|
||
type Enrollment = InferSchemaType<typeof enrollmentSchema>; | ||
|
||
export default mongoose.model<Enrollment>("Enrollment", enrollmentSchema); |
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,13 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import mongoose, { Schema } from "mongoose"; | ||
|
||
import EnrollmentModel from "../models/enrollment"; | ||
|
||
// get the enrollment type from the enrollment model | ||
export type Enrollment = Extract< | ||
typeof EnrollmentModel, | ||
mongoose.Model<any, any, any> | ||
> extends mongoose.Model<infer U> | ||
? U & { _id: Schema.Types.ObjectId } | ||
: never; |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
import EnrollmentModel from "../models/enrollment"; | ||
import { Enrollment } from "../types/enrollment"; | ||
|
||
export const createEnrollment = async (req: Enrollment) => { | ||
try { | ||
await EnrollmentModel.create(req); | ||
} catch (e) { | ||
console.log(e); | ||
throw e; | ||
} | ||
}; | ||
|
||
export const editEnrollment = async (req: Enrollment) => { | ||
try { | ||
console.log(req); | ||
await EnrollmentModel.findByIdAndUpdate(req._id, req); | ||
} catch (e) { | ||
console.log(e); | ||
throw e; | ||
} | ||
}; |
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,58 +1,53 @@ | ||
import mongoose from "mongoose"; | ||
|
||
import ProgramModel from "../models/program"; | ||
import { programLink } from "../types/programLink"; | ||
|
||
type ObjectId = mongoose.Types.ObjectId; | ||
|
||
export const programValidatorUtil = async (programs: programLink[]) => { | ||
import { Enrollment } from "../types/enrollment"; | ||
|
||
export const programValidatorUtil = async (enrollments: Enrollment[]) => { | ||
// verify all fields are present | ||
const requiredFields = [ | ||
"programId", | ||
"status", | ||
"hoursLeft", | ||
"schedule", | ||
"sessionTime", | ||
"startDate", | ||
"renewalDate", | ||
"authNumber", | ||
]; | ||
enrollments.forEach((enrollment: Enrollment) => { | ||
requiredFields.forEach((field) => { | ||
if (!enrollment[field as keyof Enrollment]) | ||
throw new Error(`Field ${field} is required on enrollment`); | ||
}); | ||
}); | ||
|
||
// verify statuses are correct and student is not in more than 2 programs | ||
const allowedStatuses = ["Joined", "Waitlisted", "Archived", "Not a fit"]; | ||
const programIds = new Set(); | ||
let active = 0; | ||
let varying = 0; | ||
await Promise.all( | ||
programs.map(async (program) => { | ||
programIds.add(program.programId); | ||
if (!mongoose.Types.ObjectId.isValid(program.programId)) | ||
enrollments.map(async (enrollment) => { | ||
programIds.add(enrollment.programId); | ||
if (!mongoose.Types.ObjectId.isValid(enrollment.programId)) | ||
throw new Error("Program ID format is invalid"); | ||
|
||
if (!allowedStatuses.includes(program.status)) | ||
if (!allowedStatuses.includes(enrollment.status)) | ||
throw new Error("Status must be one of: " + allowedStatuses.join(", ")); | ||
|
||
const programType = (await ProgramModel.findById(program.programId))?.type; | ||
if (program.status === "Joined") { | ||
const programType = (await ProgramModel.findById(enrollment.programId))?.type; | ||
if (enrollment.status === "Joined") { | ||
active++; | ||
if (programType === "varying") varying++; | ||
} | ||
}), | ||
); | ||
if (programIds.size !== programs.length) throw new Error("Programs must be unique"); | ||
|
||
// handle error reporting | ||
if (programIds.size !== enrollments.length) throw new Error("Programs must be unique"); | ||
if (active > 2) throw new Error("Student can only be active in 2 programs"); | ||
if (varying > 1) throw new Error("Student can only be in 1 varying program"); | ||
|
||
return true; | ||
}; | ||
|
||
export const addStudentToPrograms = async (studentId: ObjectId, programIds: ObjectId[]) => { | ||
await Promise.all( | ||
programIds.map(async (programId) => { | ||
await ProgramModel.findByIdAndUpdate( | ||
programId, | ||
{ $push: { students: studentId } }, | ||
{ new: true }, | ||
); | ||
}), | ||
); | ||
}; | ||
|
||
export const removeStudentFromPrograms = async (studentId: ObjectId, programIds: ObjectId[]) => { | ||
await Promise.all( | ||
programIds.map(async (programId) => { | ||
await ProgramModel.findByIdAndUpdate( | ||
programId, | ||
{ $pull: { students: studentId } }, | ||
{ new: true }, | ||
); | ||
}), | ||
); | ||
}; |
Oops, something went wrong.