Spaces:
Runtime error
Runtime error
| const { mongoose, baseSchemaOptions } = require('./base'); | |
| const SafetyBadgeSchema = new mongoose.Schema({ | |
| employee_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Employee', required: true, unique: true }, | |
| induction_date: { type: Date, required: true }, | |
| expiry_date: { type: Date, required: true }, | |
| badge_no: { type: String, required: true, unique: true }, | |
| issued_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null } | |
| }, baseSchemaOptions()); | |
| SafetyBadgeSchema.pre('validate', function setExpiry(next) { | |
| if (this.induction_date) { | |
| const dt = new Date(this.induction_date); | |
| dt.setDate(dt.getDate() + 365); | |
| this.expiry_date = dt; | |
| } | |
| next(); | |
| }); | |
| SafetyBadgeSchema.index({ expiry_date: 1 }); | |
| module.exports = mongoose.model('SafetyBadge', SafetyBadgeSchema); | |