payservedb 5.6.9 → 5.7.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "5.6.9",
3
+ "version": "5.7.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -37,18 +37,16 @@ const levySchema = new mongoose.Schema(
37
37
  type: String,
38
38
  required: true,
39
39
  },
40
- // New billing type field
41
40
  billingType: {
42
41
  type: String,
43
42
  enum: ['Prepaid', 'Postpaid'],
44
43
  required: true,
45
44
  default: 'Postpaid'
46
45
  },
47
- // Fields from levy contract
48
46
  currency: {
49
47
  type: mongoose.Schema.Types.ObjectId,
50
48
  ref: "Currency",
51
- required: false,
49
+ required: true,
52
50
  },
53
51
  mobilePayment: {
54
52
  type: Boolean,
@@ -57,71 +55,56 @@ const levySchema = new mongoose.Schema(
57
55
  },
58
56
  paymentMethodId: {
59
57
  type: mongoose.Schema.Types.ObjectId,
58
+ ref: "FacilityPaymentDetails",
60
59
  required: false,
61
60
  },
62
- // New bank payment field
63
61
  bankPayment: {
64
62
  type: Boolean,
65
63
  default: false,
66
64
  required: false,
67
65
  },
68
- // Bank details for bank payment
69
- bankDetails: {
70
- bankName: {
71
- type: String,
72
- required: false,
73
- trim: true,
74
- },
75
- accountName: {
76
- type: String,
77
- required: false,
78
- trim: true,
79
- },
80
- accountNumber: {
81
- type: String,
82
- required: false,
83
- trim: true,
84
- },
85
- branch: {
86
- type: String,
87
- required: false,
88
- trim: true,
89
- },
90
- swiftCode: {
91
- type: String,
92
- required: false,
93
- trim: true,
94
- }
66
+ // Reference to BankDetails document
67
+ bankAccountId: {
68
+ type: mongoose.Schema.Types.ObjectId,
69
+ ref: "BankDetails",
70
+ required: false,
95
71
  },
96
- // GL Account Configuration (moved from levy contract)
72
+ // Reference to BillerAddress document
73
+ billerAddressId: {
74
+ type: mongoose.Schema.Types.ObjectId,
75
+ ref: "BillerAddress",
76
+ required: true,
77
+ },
78
+ // GL Account Configuration
97
79
  glAccounts: {
98
80
  invoice: {
99
81
  debit: {
100
82
  type: mongoose.Schema.Types.ObjectId,
101
83
  ref: "GLAccount",
102
- required: false,
84
+ required: true,
103
85
  },
104
86
  credit: {
105
87
  type: mongoose.Schema.Types.ObjectId,
106
88
  ref: "GLAccount",
107
- required: false,
89
+ required: true,
108
90
  }
109
91
  },
110
92
  payment: {
111
93
  debit: {
112
94
  type: mongoose.Schema.Types.ObjectId,
113
95
  ref: "GLAccount",
114
- required: false,
96
+ required: true,
115
97
  },
116
98
  credit: {
117
99
  type: mongoose.Schema.Types.ObjectId,
118
100
  ref: "GLAccount",
119
- required: false,
101
+ required: true,
120
102
  }
121
103
  }
122
104
  },
123
105
  disabled: {
124
106
  type: Boolean,
107
+ default: false,
125
108
  required: false,
126
109
  },
127
110
  facilityId: {
@@ -129,13 +112,13 @@ const levySchema = new mongoose.Schema(
129
112
  ref: "Facility",
130
113
  required: true,
131
114
  },
132
- // Added reminder reference
115
+ // Reminder reference
133
116
  reminderId: {
134
117
  type: mongoose.Schema.Types.ObjectId,
135
118
  ref: 'Reminder',
136
119
  required: false
137
120
  },
138
- // Added penalty reference
121
+ // Penalty reference
139
122
  penaltyId: {
140
123
  type: mongoose.Schema.Types.ObjectId,
141
124
  ref: 'Penalty',
@@ -147,6 +130,77 @@ const levySchema = new mongoose.Schema(
147
130
  }
148
131
  );
149
132
 
133
+ // Add virtuals for populated references
134
+ levySchema.virtual('bankAccount', {
135
+ ref: 'BankDetails',
136
+ localField: 'bankAccountId',
137
+ foreignField: '_id',
138
+ justOne: true
139
+ });
140
+
141
+ levySchema.virtual('billerAddress', {
142
+ ref: 'BillerAddress',
143
+ localField: 'billerAddressId',
144
+ foreignField: '_id',
145
+ justOne: true
146
+ });
147
+
148
+ levySchema.virtual('reminder', {
149
+ ref: 'Reminder',
150
+ localField: 'reminderId',
151
+ foreignField: '_id',
152
+ justOne: true
153
+ });
154
+
155
+ levySchema.virtual('penalty', {
156
+ ref: 'Penalty',
157
+ localField: 'penaltyId',
158
+ foreignField: '_id',
159
+ justOne: true
160
+ });
161
+
162
+ // Pre-save validation middleware
163
+ levySchema.pre('save', function (next) {
164
+ // Validate bank payment requirements
165
+ if (this.bankPayment && !this.bankAccountId) {
166
+ const error = new Error('Bank account is required when bank payment is enabled');
167
+ return next(error);
168
+ }
169
+
170
+ // Validate mobile payment requirements
171
+ if (this.mobilePayment && !this.paymentMethodId) {
172
+ const error = new Error('Payment method is required when mobile payment is enabled');
173
+ return next(error);
174
+ }
175
+
176
+ // Validate GL accounts
177
+ if (!this.glAccounts?.invoice?.debit || !this.glAccounts?.invoice?.credit ||
178
+ !this.glAccounts?.payment?.debit || !this.glAccounts?.payment?.credit) {
179
+ const error = new Error('All GL accounts (invoice and payment, debit and credit) are required');
180
+ return next(error);
181
+ }
182
+
183
+ next();
184
+ });
185
+
186
+ // Add indexes for better performance
187
+ levySchema.index({ facilityId: 1 });
188
+ levySchema.index({ levyType: 1 });
189
+ levySchema.index({ disabled: 1 });
190
+ levySchema.index({ bankAccountId: 1 });
191
+ levySchema.index({ billerAddressId: 1 });
192
+ levySchema.index({ reminderId: 1 });
193
+ levySchema.index({ penaltyId: 1 });
194
+ levySchema.index({ createdAt: -1 });
195
+
196
+ // Compound indexes
197
+ levySchema.index({ facilityId: 1, disabled: 1 });
198
+ levySchema.index({ facilityId: 1, levyType: 1 });
199
+
200
+ // Ensure virtual fields are serialized
201
+ levySchema.set('toJSON', { virtuals: true });
202
+ levySchema.set('toObject', { virtuals: true });
203
+
150
204
  // Compile the model from the schema
151
205
  const Levy = mongoose.model("Levy", levySchema);
152
206
 
@@ -75,6 +75,14 @@ const ticketSchema = new mongoose.Schema({
75
75
  type: String,
76
76
  required: false,
77
77
  },
78
+ isRead: {
79
+ type: Boolean,
80
+ default: false
81
+ },
82
+ readAt: {
83
+ type: Date,
84
+ default: null
85
+ },
78
86
  services: [
79
87
  {
80
88
  description: {