payservedb 4.7.0 → 4.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 +1 -1
- package/src/models/invoice.js +25 -0
package/package.json
CHANGED
package/src/models/invoice.js
CHANGED
|
@@ -160,6 +160,21 @@ const invoiceSchema = new mongoose.Schema(
|
|
|
160
160
|
paymentDate: { type: Date },
|
|
161
161
|
transactionId: { type: String },
|
|
162
162
|
},
|
|
163
|
+
// New field to track when an invoice has been viewed
|
|
164
|
+
viewStatus: {
|
|
165
|
+
isOpened: { type: Boolean, default: false },
|
|
166
|
+
openedAt: { type: Date, default: null },
|
|
167
|
+
openedBy: {
|
|
168
|
+
facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
|
|
169
|
+
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
|
|
170
|
+
userRole: { type: String }
|
|
171
|
+
},
|
|
172
|
+
viewHistory: [{
|
|
173
|
+
viewedAt: { type: Date, required: true },
|
|
174
|
+
facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
|
|
175
|
+
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
|
|
176
|
+
}]
|
|
177
|
+
}
|
|
163
178
|
},
|
|
164
179
|
{
|
|
165
180
|
timestamps: true,
|
|
@@ -176,6 +191,8 @@ invoiceSchema.index({ 'currency.code': 1 }); // Add index for currency code
|
|
|
176
191
|
invoiceSchema.index({ 'currency.id': 1 }); // Add index for currency ID
|
|
177
192
|
invoiceSchema.index({ 'currency.code': 1, 'client.clientId': 1, status: 1 }); // Compound index for currency-based queries
|
|
178
193
|
invoiceSchema.index({ 'client.clientId': 1, balanceBroughtForward: 1 }); // Add index for finding invoices with credit balances
|
|
194
|
+
invoiceSchema.index({ 'viewStatus.isOpened': 1 }); // Add index for view status
|
|
195
|
+
invoiceSchema.index({ 'viewStatus.openedBy.facilityId': 1 }); // Add index for facility view tracking
|
|
179
196
|
|
|
180
197
|
// Add virtual field for calculating balance
|
|
181
198
|
invoiceSchema.virtual('calculatedBalance').get(function () {
|
|
@@ -236,6 +253,14 @@ invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
|
|
|
236
253
|
]);
|
|
237
254
|
};
|
|
238
255
|
|
|
256
|
+
// New static method to find all unviewed invoices
|
|
257
|
+
invoiceSchema.statics.findUnviewedInvoices = function (facilityId) {
|
|
258
|
+
return this.find({
|
|
259
|
+
'facility.id': facilityId,
|
|
260
|
+
'viewStatus.isOpened': false
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
|
|
239
264
|
// Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
|
|
240
265
|
invoiceSchema.pre('save', function (next) {
|
|
241
266
|
// If balanceBroughtForward is negative (credit), sync with overpay for backwards compatibility
|