Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
72 lines
1.5 KiB
JavaScript
72 lines
1.5 KiB
JavaScript
// /components/payments/paymentModel.js
|
|
'use strict';
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const transactionSchema = new mongoose.Schema({
|
|
amount: { type: Number, required: true },
|
|
method: {
|
|
type: String,
|
|
enum: ['online', 'card', 'cash'],
|
|
default: 'card'
|
|
},
|
|
receiptNumber: { type: String, trim: true },
|
|
recordedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
|
date: { type: Date, default: Date.now }
|
|
}, { _id: true });
|
|
|
|
const paymentSchema = new mongoose.Schema({
|
|
user: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
ref: 'User',
|
|
required: true,
|
|
index: true
|
|
},
|
|
classes: [{
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
ref: 'Class'
|
|
}],
|
|
course: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
ref: 'Course'
|
|
},
|
|
amount: {
|
|
type: Number,
|
|
required: true,
|
|
min: 0
|
|
},
|
|
paidAmount: {
|
|
type: Number,
|
|
default: 0,
|
|
min: 0
|
|
},
|
|
dueDate: {
|
|
type: Date
|
|
},
|
|
status: {
|
|
type: String,
|
|
enum: ['pending', 'partial', 'paid', 'overdue'],
|
|
default: 'pending'
|
|
},
|
|
transactions: [transactionSchema],
|
|
notes: { type: String, trim: true }
|
|
}, {
|
|
timestamps: true
|
|
});
|
|
|
|
// Auto-update status based on paid amount
|
|
paymentSchema.pre('save', function (next) {
|
|
if (this.paidAmount >= this.amount) {
|
|
this.status = 'paid';
|
|
} else if (this.paidAmount > 0) {
|
|
this.status = 'partial';
|
|
} else if (this.dueDate && new Date() > this.dueDate) {
|
|
this.status = 'overdue';
|
|
} else {
|
|
this.status = 'pending';
|
|
}
|
|
next();
|
|
});
|
|
|
|
module.exports = mongoose.model('Payment', paymentSchema);
|