Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/controllers/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const createSection: RequestHandler = async (req, res) => {
// Only keep the fields clients can update
export type UpdateSectionBody = Pick<
SectionDoc,
"code" | "program" | "teachers" | "enrolledStudents" | "startTime" | "endTime" | "days"
"code" | "teachers" | "enrolledStudents" | "startTime" | "endTime" | "startDate" | "endDate" | "archived" | "color" | "days"
>;

// ---------------------- UPDATE ----------------------
Expand Down
46 changes: 29 additions & 17 deletions backend/src/models/sections.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import mongoose from "mongoose";

import type { Document, Types } from "mongoose";
import type { Document, Types } from "mongoose"; // Types still used for ObjectId arrays in teachers/enrolledStudents

// Type definition for Section documents
export type SectionDoc = Document & {
code: string;
program: Types.ObjectId;
teachers: Types.ObjectId[];
enrolledStudents: Types.ObjectId[];
startTime: string;
endTime: string;
startDate: string;
endDate: string;
archived: boolean;
color: string;
days: string[];
};

Expand All @@ -18,36 +21,45 @@ const sectionSchema = new mongoose.Schema(
{
code: {
type: String,
required: true, // Code must be provided
},
program: {
type: mongoose.Schema.Types.ObjectId,
ref: "Program", // Reference to the Program model
required: true, // Must be associated with a program
required: true,
},
teachers: {
type: [mongoose.Schema.Types.ObjectId],
ref: "User", // Reference to the User model
required: true, // Must contain at least one teacher
default: [], // Default to an empty array if no teachers are added
ref: "User",
default: [],
},
enrolledStudents: {
type: [mongoose.Schema.Types.ObjectId],
ref: "Student", // Reference to the Student model
required: false, // Not required at creation
default: [], // Default to an empty array if no students are enrolled
ref: "Student",
default: [],
},
startTime: {
type: String,
required: true, // Start time must be provided
required: true,
},
endTime: {
type: String,
required: true, // End time must be provided
required: true,
},
startDate: {
type: String,
required: true,
},
endDate: {
type: String,
required: true,
},
archived: {
type: Boolean,
default: false,
},
color: {
type: String,
required: true,
},
days: {
type: [String],
required: true, // Must provide an array of days
required: true,
},
},
{
Expand Down
23 changes: 14 additions & 9 deletions backend/src/validators/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ const TIME_24H_REGEX = /^(?:[01]\d|2[0-3]):[0-5]\d$/;

const validateCode = body("code").notEmpty().withMessage("Code is required");

const validateProgram = body("program")
.notEmpty()
.withMessage("Program is required")
.isMongoId()
.withMessage("Program must be a valid MongoDB ObjectID");
const validateStartDate = body("startDate").notEmpty().withMessage("Start date is required");
const validateEndDate = body("endDate").notEmpty().withMessage("End date is required");
const validateColor = body("color").notEmpty().withMessage("Color is required");
const validateArchived = body("archived").optional().isBoolean().withMessage("Archived must be a boolean");

export const validateTeachers: ValidationChain[] = [
body("teachers").isArray().withMessage("Teachers must be an array"),
Expand Down Expand Up @@ -81,19 +80,25 @@ const validateEndTime = body("endTime")
export const createSectionValidator = [
validateCode,
validateDays,
validateStartTime,
validateEndTime,
validateStartDate,
validateEndDate,
validateColor,
validateArchived,
...validateEnrolledStudents,
validateProgram,
validateStartTime,
...validateTeachers,
];

export const updateSectionValidator = [
validateCode.optional(),
validateDays.optional(),
validateStartTime.optional(),
validateEndTime.optional(),
validateStartDate.optional(),
validateEndDate.optional(),
validateColor.optional(),
validateArchived.optional(),
...validateEnrolledStudents.map((v) => v.optional()),
validateProgram.optional(),
validateStartTime.optional(),
...validateTeachers.map((v) => v.optional()),
];
Loading