Files
gameno-api/components/classes/classModel.js
T
kavehhn f57d8bbdfa feat: add class discounts, frontend visibility, and public listing
Add hasDiscount, freeSpots, and showOnFrontend on classes with a public get-all endpoint and richer notification user populate.
2026-08-16 08:11:42 +03:30

97 lines
1.6 KiB
JavaScript

// /components/classes/classModel.js
'use strict';
const mongoose = require('mongoose');
const classSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
course: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Course',
required: true
},
professor: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Professor'
},
students: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
capacity: {
type: Number,
default: 30
},
tuitionFee: {
type: Number,
default: 0
},
hasDiscount: {
type: Boolean,
default: false
},
discount: {
type: Number,
default: 0,
min: 0
},
freeSpots: {
type: Number,
min: 0,
default: null
},
showOnFrontend: {
type: Boolean,
default: true,
index: true
},
startDate: {
type: Date
},
endDate: {
type: Date
},
days: {
type: [{
type: Number,
min: 0,
max: 6
}],
default: []
},
startTime: {
type: String,
trim: true,
default: ''
},
endTime: {
type: String,
trim: true,
default: ''
},
isActive: {
type: Boolean,
default: true
},
adminNotes: {
type: [String],
default: []
}
}, {
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
classSchema.virtual('finalTuitionFee').get(function () {
const tuition = this.tuitionFee || 0;
if (!this.hasDiscount) return tuition;
return Math.max(0, tuition - (this.discount || 0));
});
module.exports = mongoose.model('Class', classSchema);