payservedb 9.1.2 → 9.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -94,6 +94,7 @@ const models = {
94
94
  Levy: require("./src/models/levy"),
95
95
  LevyType: require("./src/models/levytype"),
96
96
  LevyContract: require("./src/models/levycontract"),
97
+ DepositSchema: require("./src/models/levy_deposits"),
97
98
  Invoice: require("./src/models/invoice"),
98
99
  InvoiceEditLog: require("./src/models/invoice_edit_log"),
99
100
  CombinedInvoice: require("./src/models/combined_invoice"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "9.1.2",
3
+ "version": "9.1.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -0,0 +1,113 @@
1
+ const mongoose = require('mongoose');
2
+ const Schema = mongoose.Schema;
3
+
4
+ const DepositSchema = new Schema(
5
+ {
6
+ contractId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: 'LevyContract',
9
+ required: [true, 'Contract ID is required']
10
+ },
11
+ customerId: {
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: 'Customer',
14
+ required: [true, 'Customer ID is required']
15
+ },
16
+ unitId: {
17
+ type: mongoose.Schema.Types.ObjectId,
18
+ ref: 'Unit',
19
+ required: [true, 'Unit ID is required']
20
+ },
21
+ facilityId: {
22
+ type: mongoose.Schema.Types.ObjectId,
23
+ ref: 'Facility',
24
+ required: [true, 'Facility ID is required']
25
+ },
26
+ amount: {
27
+ type: Number,
28
+ required: [true, 'Amount is required'],
29
+ min: [0, 'Amount cannot be negative']
30
+ },
31
+ datePaid: {
32
+ type: Date,
33
+ required: [true, 'Date paid is required']
34
+ },
35
+ status: {
36
+ type: String,
37
+ enum: ['Held', 'Refunded', 'Partially Refunded', 'Forfeited'],
38
+ default: 'Held',
39
+ required: [true, 'Status is required']
40
+ },
41
+ refundedAmount: {
42
+ type: Number,
43
+ default: 0,
44
+ min: [0, 'Refunded amount cannot be negative']
45
+ },
46
+ refundDate: {
47
+ type: Date
48
+ }
49
+ },
50
+ {
51
+ timestamps: true,
52
+ toJSON: { virtuals: true },
53
+ toObject: { virtuals: true }
54
+ }
55
+ );
56
+
57
+ // Virtual populate for Contract details
58
+ DepositSchema.virtual('contract', {
59
+ ref: 'LevyContract',
60
+ localField: 'contractId',
61
+ foreignField: '_id',
62
+ justOne: true
63
+ });
64
+
65
+ // Virtual populate for Customer details
66
+ DepositSchema.virtual('customer', {
67
+ ref: 'Customer',
68
+ localField: 'customerId',
69
+ foreignField: '_id',
70
+ justOne: true
71
+ });
72
+
73
+ // Virtual populate for Unit details
74
+ DepositSchema.virtual('unit', {
75
+ ref: 'Unit',
76
+ localField: 'unitId',
77
+ foreignField: '_id',
78
+ justOne: true
79
+ });
80
+
81
+ // Virtual for remaining (unrefunded) balance
82
+ DepositSchema.virtual('balanceRemaining').get(function () {
83
+ return this.amount - (this.refundedAmount || 0);
84
+ });
85
+
86
+ // Pre-save middleware: set refundDate when status moves to Refunded/Partially Refunded
87
+ DepositSchema.pre('save', function (next) {
88
+ if (
89
+ (this.status === 'Refunded' || this.status === 'Partially Refunded') &&
90
+ !this.refundDate &&
91
+ this.isModified('status')
92
+ ) {
93
+ this.refundDate = new Date();
94
+ }
95
+
96
+ // Guard against refunded amount exceeding original amount
97
+ if ((this.refundedAmount || 0) > this.amount) {
98
+ return next(new Error('Refunded amount cannot exceed deposit amount'));
99
+ }
100
+
101
+ // Auto-flip to fully Refunded when refundedAmount covers the amount
102
+ if (this.refundedAmount >= this.amount) {
103
+ this.status = 'Refunded';
104
+ }
105
+
106
+ next();
107
+ });
108
+
109
+
110
+ module.exports = {
111
+ schema: DepositSchema,
112
+ name: 'Deposit'
113
+ };