payservedb 8.0.0 → 8.0.2

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": "8.0.0",
3
+ "version": "8.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,3 +1,5 @@
1
+ const mongoose = require("mongoose");
2
+
1
3
  const FacilityBillingPriceSchema = new mongoose.Schema({
2
4
  facilityId: {
3
5
  type: mongoose.Schema.Types.ObjectId,
@@ -120,6 +120,28 @@ const invoiceSchema = new mongoose.Schema(
120
120
  type: Number,
121
121
  default: 0,
122
122
  },
123
+ // NEW FIELD 1: Year-Month for easy filtering
124
+ yearMonth: {
125
+ type: String,
126
+ required: true,
127
+ match: /^\d{4}-\d{2}$/, // Validates format like "2025-08"
128
+ index: true
129
+ },
130
+ // NEW FIELD 2: Simple notification tracking
131
+ notificationsSent: {
132
+ sms: {
133
+ type: Boolean,
134
+ default: false
135
+ },
136
+ email: {
137
+ type: Boolean,
138
+ default: false
139
+ },
140
+ attempts: {
141
+ type: Number,
142
+ default: 0
143
+ }
144
+ },
123
145
  voidMetadata: {
124
146
  voidedBy: {
125
147
  userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
@@ -237,6 +259,11 @@ invoiceSchema.index({ 'viewStatus.openedBy.facilityId': 1 }); // Add index for f
237
259
  // Add index for double entry accounts
238
260
  invoiceSchema.index({ invoiceDoubleEntryAccount: 1 });
239
261
  invoiceSchema.index({ paymentDoubleEntryAccount: 1 });
262
+ // NEW INDEXES for new fields
263
+ invoiceSchema.index({ yearMonth: 1 }); // For filtering by year-month
264
+ invoiceSchema.index({ yearMonth: 1, status: 1 }); // Combined index for monthly reports
265
+ invoiceSchema.index({ 'notificationsSent.sms': 1 }); // For finding SMS sent/not sent
266
+ invoiceSchema.index({ 'notificationsSent.email': 1 }); // For finding email sent/not sent
240
267
 
241
268
  // Add virtual field for calculating balance
242
269
  invoiceSchema.virtual('calculatedBalance').get(function () {
@@ -321,6 +348,24 @@ invoiceSchema.statics.findUnviewedInvoices = function (facilityId) {
321
348
  });
322
349
  };
323
350
 
351
+ // NEW STATIC METHODS for new fields
352
+ // Find invoices by year-month
353
+ invoiceSchema.statics.findByYearMonth = function (yearMonth) {
354
+ return this.find({ yearMonth: yearMonth });
355
+ };
356
+
357
+ // Find invoices where notifications haven't been sent
358
+ invoiceSchema.statics.findPendingNotifications = function (facilityId) {
359
+ return this.find({
360
+ 'facility.id': facilityId,
361
+ status: { $in: ['Unpaid', 'Overdue'] },
362
+ $or: [
363
+ { 'notificationsSent.sms': false },
364
+ { 'notificationsSent.email': false }
365
+ ]
366
+ });
367
+ };
368
+
324
369
  // Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
325
370
  invoiceSchema.pre('save', function (next) {
326
371
  // If balanceBroughtForward is negative (credit), sync with overpay for backwards compatibility
@@ -329,6 +374,14 @@ invoiceSchema.pre('save', function (next) {
329
374
  } else {
330
375
  this.overpay = 0; // No overpay if there's no negative balance
331
376
  }
377
+
378
+ // NEW: Auto-generate yearMonth from issueDate if not provided
379
+ if (!this.yearMonth && this.issueDate) {
380
+ const year = this.issueDate.getFullYear();
381
+ const month = String(this.issueDate.getMonth() + 1).padStart(2, '0');
382
+ this.yearMonth = `${year}-${month}`;
383
+ }
384
+
332
385
  next();
333
386
  });
334
387