payservedb 6.2.8 → 6.2.9

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": "6.2.8",
3
+ "version": "6.2.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -138,37 +138,9 @@ facilityWalletTransactionsMetadataSchema
138
138
  return ["approved", "rejected", "cancelled"].includes(this.status);
139
139
  });
140
140
 
141
- // Pre-save middleware to validate status transitions
141
+ // Simple pre-save middleware to set timestamps
142
142
  facilityWalletTransactionsMetadataSchema.pre("save", function (next) {
143
- if (this.isModified("status")) {
144
- const validTransitions = {
145
- pending: ["approved", "rejected", "cancelled"],
146
- partial: ["paid", "cancelled"],
147
- approved: ["paid", "cancelled"],
148
- rejected: ["pending"], // Allow resubmission
149
- paid: [], // Terminal state
150
- cancelled: ["pending"], // Allow reactivation
151
- };
152
-
153
- if (this.isNew) {
154
- // New documents can have any status
155
- return next();
156
- }
157
-
158
- const previousStatus = this.$locals.previousStatus;
159
- if (
160
- previousStatus &&
161
- !validTransitions[previousStatus]?.includes(this.status)
162
- ) {
163
- return next(
164
- new Error(
165
- `Invalid status transition from ${previousStatus} to ${this.status}`,
166
- ),
167
- );
168
- }
169
- }
170
-
171
- // Set approval/rejection timestamps
143
+ // Set approval/rejection timestamps when status changes
172
144
  if (this.isModified("status")) {
173
145
  if (this.status === "approved" && !this.approvedAt) {
174
146
  this.approvedAt = new Date();
@@ -177,19 +149,6 @@ facilityWalletTransactionsMetadataSchema.pre("save", function (next) {
177
149
  this.rejectedAt = new Date();
178
150
  }
179
151
  }
180
-
181
- next();
182
- });
183
-
184
- // Pre-save middleware to store previous status
185
- facilityWalletTransactionsMetadataSchema.pre("save", function (next) {
186
- if (!this.isNew && this.isModified("status")) {
187
- this.$locals.previousStatus = this.$locals.wasNew
188
- ? undefined
189
- : this.$__.wasModified("status")
190
- ? this.$__.$originalValue.status
191
- : this.status;
192
- }
193
152
  next();
194
153
  });
195
154
 
@@ -223,7 +182,7 @@ facilityWalletTransactionsMetadataSchema.statics.getSummaryStats = function (
223
182
  ]);
224
183
  };
225
184
 
226
- // Instance method to approve metadata
185
+ // Instance method to approve metadata (with validation)
227
186
  facilityWalletTransactionsMetadataSchema.methods.approve = function (
228
187
  approvedBy,
229
188
  notes,
@@ -240,7 +199,7 @@ facilityWalletTransactionsMetadataSchema.methods.approve = function (
240
199
  return this.save();
241
200
  };
242
201
 
243
- // Instance method to reject metadata
202
+ // Instance method to reject metadata (with validation)
244
203
  facilityWalletTransactionsMetadataSchema.methods.reject = function (
245
204
  rejectedBy,
246
205
  reason,
@@ -257,6 +216,21 @@ facilityWalletTransactionsMetadataSchema.methods.reject = function (
257
216
  return this.save();
258
217
  };
259
218
 
219
+ // Static method to validate status transitions (call manually if needed)
220
+ facilityWalletTransactionsMetadataSchema.statics.isValidStatusTransition =
221
+ function (fromStatus, toStatus) {
222
+ const validTransitions = {
223
+ pending: ["approved", "rejected", "cancelled"],
224
+ partial: ["paid", "cancelled"],
225
+ approved: ["paid", "cancelled"],
226
+ rejected: ["pending"], // Allow resubmission
227
+ paid: [], // Terminal state
228
+ cancelled: ["pending"], // Allow reactivation
229
+ };
230
+
231
+ return validTransitions[fromStatus]?.includes(toStatus) || false;
232
+ };
233
+
260
234
  const FacilityWalletTransactionsMetadata = mongoose.model(
261
235
  "FacilityWalletTransactionsMetadata",
262
236
  facilityWalletTransactionsMetadataSchema,