Skip to content

Commit

Permalink
Add discriminator for lesson page and activity page (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynzhang18 authored Dec 31, 2024
1 parent b6021ae commit 2196fad
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backend/models/coursemodule.mgmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const CourseModuleSchema: Schema = new Schema({
});

/* eslint-disable no-param-reassign */
CourseModuleSchema.set("toJSON", {
CourseModuleSchema.set("toObject", {
virtuals: true,
versionKey: false,
transform: (_doc: Document, ret: Record<string, unknown>) => {
Expand Down
63 changes: 51 additions & 12 deletions backend/models/coursepage.mgmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,55 @@ export interface CoursePage extends Document {
title: string;
displayIndex: number;
type: PageType;
}

export interface LessonPage extends CoursePage {
source: string;
}

export interface ActivityPage extends CoursePage {
layout: [ElementSkeleton];
}

export const CoursePageSchema: Schema = new Schema({
title: {
type: String,
required: true,
},
displayIndex: {
type: Number,
required: true,
const baseOptions = {
discriminatorKey: "type",
};

export const CoursePageSchema: Schema = new Schema(
{
title: {
type: String,
required: true,
},
displayIndex: {
type: Number,
required: true,
},
type: {
type: String,
required: true,
enum: ["Lesson", "Activity"],
},
},
type: {
baseOptions,
);

const LessonPageSchema: Schema = new Schema({
source: {
type: String,
required: true,
enum: ["Lesson", "Activity"],
},
});

const ActivityPageSchema: Schema = new Schema({
layout: {
type: [ElementSkeletonSchema],
required: true,
},
});

/* eslint-disable no-param-reassign */
CoursePageSchema.set("toJSON", {
CoursePageSchema.set("toObject", {
virtuals: true,
versionKey: false,
transform: (_doc: Document, ret: Record<string, unknown>) => {
Expand All @@ -72,4 +96,19 @@ CoursePageSchema.set("toJSON", {
},
});

export default mongoose.model<CoursePage>("CoursePage", CoursePageSchema);
const CoursePageModel = mongoose.model<CoursePage>(
"CoursePage",
CoursePageSchema,
);

const LessonPageModel = CoursePageModel.discriminator(
"Lesson",
LessonPageSchema,
);
const ActivityPageModel = CoursePageModel.discriminator(
"Activity",
ActivityPageSchema,
);

export { LessonPageModel, ActivityPageModel };
export default CoursePageModel;
2 changes: 1 addition & 1 deletion backend/models/courseunit.mgmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CourseUnitSchema: Schema = new Schema({
});

/* eslint-disable no-param-reassign */
CourseUnitSchema.set("toJSON", {
CourseUnitSchema.set("toObject", {
virtuals: true,
versionKey: false,
transform: (_doc: Document, ret: Record<string, unknown>) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/models/helprequest.mgmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const HelpRequestSchema: Schema = new Schema(
);

/* eslint-disable no-param-reassign */
HelpRequestSchema.set("toJSON", {
HelpRequestSchema.set("toObject", {
virtuals: true,
versionKey: false,
transform: (_doc: Document, ret: Record<string, unknown>) => {
Expand Down

0 comments on commit 2196fad

Please sign in to comment.