forked from StellarEdu/StellarEduPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentModel.js
More file actions
55 lines (47 loc) · 1.99 KB
/
Copy pathstudentModel.js
File metadata and controls
55 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const mongoose = require('mongoose');
const studentSchema = new mongoose.Schema({
studentId: { type: String, required: true, unique: true, index: true },
name: { type: String, required: true },
class: { type: String, required: true, index: true },
feeAmount: { type: Number, required: true },
feePaid: { type: Boolean, default: false, index: true },
totalPaid: { type: Number, default: 0 },
remainingBalance: { type: Number, default: null },
// Concurrency control fields
version: { type: Number, default: 0 },
lastPaymentAt: { type: Date, default: null },
lastPaymentHash: { type: String, default: null },
// Additional tracking
lastTransactionAt: { type: Date, default: null },
}, { timestamps: true });
const studentSchema = new mongoose.Schema(
{
// schoolId is required — injected by resolveSchool middleware on every write
schoolId: { type: String, required: true, index: true },
studentId: { type: String, required: true, index: true },
name: { type: String, required: true },
class: { type: String, required: true, index: true },
feeAmount: { type: Number, required: true },
feePaid: { type: Boolean, default: false, index: true },
totalPaid: { type: Number, default: 0 },
remainingBalance: { type: Number, default: null },
},
{ timestamps: true }
);
// studentId is unique per school, not globally
studentSchema.index({ studentId: 1, schoolId: 1 }, { unique: true });
studentSchema.index({ schoolId: 1, class: 1 });
studentSchema.index({ schoolId: 1, feePaid: 1 });
// Compound indexes for common query patterns
studentSchema.index({ studentId: 1, version: 1 });
studentSchema.index({ feePaid: 1, class: 1 });
studentSchema.index({ totalPaid: 1 });
// Pre-save hook to increment version
studentSchema.pre('save', function(next) {
if (this.isModified() && !this.isNew) {
this.version += 1;
}
next();
});
module.exports = mongoose.model('Student', studentSchema);