Skip to content

Commit

Permalink
fix program schema bug
Browse files Browse the repository at this point in the history
  • Loading branch information
adhi0331 committed Apr 15, 2024
1 parent f204c4f commit 5a02c23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
9 changes: 5 additions & 4 deletions backend/src/controllers/program.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import { RequestHandler } from "express";
import { validationResult } from "express-validator";
import { Schema } from "mongoose";
//import { error } from "firebase-functions/logger";

import ProgramModel from "../models/program";
Expand All @@ -12,11 +13,11 @@ export type Program = {
abbreviation: string;
type: string;
daysOfWeek: string[];
startDate: string;
endDate: string;
startDate: Date;
endDate: Date;
color: string; //colorValueHex;
studentUIDs: string[];
renewalDate: string;
studentUIDs: Schema.Types.ObjectId[];
renewalDate: Date;
hourlyPay: string;
sessions: [string[]];
};
Expand Down
6 changes: 3 additions & 3 deletions backend/src/models/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const programSchema = new Schema({
startDate: { type: Date, required: true },
endDate: { type: Date, required: true },
color: { type: String, required: true },
studentUIDs: { type: [String], required: true },
students: { type: [Schema.Types.ObjectId], ref: "Students", required: false },
renewalDate: { type: Date, required: true },
hourlyPay: { type: Number, required: true },
sessions: { type: [[String]], required: true },
});

type ProgramForm = InferSchemaType<typeof programSchema>;
type Program = InferSchemaType<typeof programSchema>;

export default model<ProgramForm>("ProgramForm", programSchema);
export default model<Program>("Program", programSchema);
40 changes: 19 additions & 21 deletions backend/src/validators/program.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { body } from "express-validator";
import mongoose from "mongoose";
// import mongoose from "mongoose";

import { Program } from "../controllers/program";

Expand Down Expand Up @@ -107,24 +107,24 @@ const makeColorValidator = () =>
}
return true;
});
const makeStudentUIDsValidator = () =>
// mongoID
body("studentUIDs")
.exists()
.withMessage("student UIDs list needed")
.bail()
.isArray()
.bail()
.withMessage("students must be an array")
.custom((students: string[]) => {
students.forEach((studentId) => {
if (!mongoose.Types.ObjectId.isValid(studentId))
throw new Error("students must be valid student ids");
});
return true;
})
.bail()
.withMessage("students must be valid student ids");
// const makeStudentUIDsValidator = () =>
// // mongoID
// body("studentUIDs")
// .exists()
// .withMessage("student UIDs list needed")
// .bail()
// .isArray()
// .bail()
// .withMessage("students must be an array")
// .custom((students: string[]) => {
// students.forEach((studentId) => {
// if (!mongoose.Types.ObjectId.isValid(studentId))
// throw new Error("students must be valid student ids");
// });
// return true;
// })
// .bail()
// .withMessage("students must be valid student ids");
const makeRenewalDateValidator = () =>
body("renewalDate")
.exists()
Expand Down Expand Up @@ -193,7 +193,6 @@ export const createProgram = [
makeStartDateValidator(),
makeEndDateValidator(),
makeColorValidator(),
makeStudentUIDsValidator(),
makeRenewalDateValidator(),
makeHourlyPayValidator(),
makeSessionsValidator(),
Expand All @@ -207,7 +206,6 @@ export const updateProgram = [
makeStartDateValidator(),
makeEndDateValidator(),
makeColorValidator(),
makeStudentUIDsValidator(),
makeRenewalDateValidator(),
makeHourlyPayValidator(),
makeSessionsValidator(),
Expand Down
1 change: 0 additions & 1 deletion frontend/src/hooks/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export const useRedirectToHomeIfSignedIn = () => {
export const useRedirectToLoginIfNotSignedIn = () => {
useRedirection({
checkShouldRedirect: ({ firebaseUser, piaUser }) => {
console.log(firebaseUser);
return firebaseUser === null || piaUser === null;
},
redirectURL: LOGIN_URL,
Expand Down

0 comments on commit 5a02c23

Please sign in to comment.