database-connector 2.4.10 → 2.4.12

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.
@@ -27,9 +27,10 @@ const recordSubscriptionHistory = async (subscriptionDoc) => {
27
27
 
28
28
  const historySnapshot = buildSubscriptionHistorySnapshot(subscriptionDoc);
29
29
 
30
- await subscriptionDoc.updateOne(
31
- { $push: { subscriptionsHistory: historySnapshot } },
32
- { skipHistory: true }
30
+ // Use Model.updateOne instead of doc.updateOne to avoid triggering middleware
31
+ await mongoose.model('Subscription').updateOne(
32
+ { _id: subscriptionDoc._id },
33
+ { $push: { subscriptionsHistory: historySnapshot } }
33
34
  );
34
35
  };
35
36
 
@@ -427,22 +428,60 @@ const subscriptionSchema = new mongoose.Schema(
427
428
  { toJSON: { virtuals: true } }
428
429
  );
429
430
 
431
+ // Pre-save middleware to check skipHistory flag
432
+ subscriptionSchema.pre('save', function (next) {
433
+ // Store the skipHistory flag on the document itself
434
+ if (this.$locals && this.$locals.skipHistory) {
435
+ this._skipHistory = true;
436
+ }
437
+ next();
438
+ });
439
+
430
440
  // Auto-record history snapshots to embedded subscriptionsHistory array
431
- subscriptionSchema.post('save', async function (doc) {
432
- if (this.getOptions && this.getOptions().skipHistory) return;
441
+ subscriptionSchema.post('save', async function (doc, next) {
442
+ // Check if we should skip history
443
+ if (doc._skipHistory) {
444
+ delete doc._skipHistory; // Clean up the flag
445
+ return next();
446
+ }
447
+
433
448
  await recordSubscriptionHistory(doc);
449
+ next();
434
450
  });
435
451
 
436
- subscriptionSchema.post('findOneAndUpdate', async function () {
437
- if (this.getOptions && this.getOptions().skipHistory) return;
438
- const updatedDoc = await this.model.findOne(this.getQuery());
439
- await recordSubscriptionHistory(updatedDoc);
452
+ // Pre-middleware for findOneAndUpdate to capture options
453
+ subscriptionSchema.pre('findOneAndUpdate', function (next) {
454
+ if (this.getOptions && this.getOptions().skipHistory) {
455
+ this._skipHistory = true;
456
+ }
457
+ next();
458
+ });
459
+
460
+ subscriptionSchema.post('findOneAndUpdate', async function (doc) {
461
+ if (this._skipHistory) return;
462
+
463
+ // Get the updated document
464
+ const updatedDoc = doc || await this.model.findOne(this.getQuery());
465
+ if (updatedDoc) {
466
+ await recordSubscriptionHistory(updatedDoc);
467
+ }
468
+ });
469
+
470
+ // Pre-middleware for updateOne to capture options
471
+ subscriptionSchema.pre('updateOne', function (next) {
472
+ if (this.getOptions && this.getOptions().skipHistory) {
473
+ this._skipHistory = true;
474
+ }
475
+ next();
440
476
  });
441
477
 
442
478
  subscriptionSchema.post('updateOne', async function () {
443
- if (this.getOptions && this.getOptions().skipHistory) return;
479
+ if (this._skipHistory) return;
480
+
444
481
  const updatedDoc = await this.model.findOne(this.getQuery());
445
- await recordSubscriptionHistory(updatedDoc);
482
+ if (updatedDoc) {
483
+ await recordSubscriptionHistory(updatedDoc);
484
+ }
446
485
  });
447
486
 
448
487
  // Indexes for performance optimization
@@ -457,4 +496,4 @@ subscriptionSchema.index({ sellerId: 1, status: 1 });
457
496
  subscriptionSchema.index({ status: 1, endDate: 1 });
458
497
  subscriptionSchema.index({ stores: 1 });
459
498
 
460
- module.exports = mongoose.model('Subscription', subscriptionSchema);
499
+ module.exports = mongoose.model('Subscription', subscriptionSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "database-connector",
3
- "version": "2.4.10",
3
+ "version": "2.4.12",
4
4
  "description": "MongoDB models package with Mongoose schemas for e-commerce applications. Includes User, Product, Store, Order and more with built-in validation and virtual properties.",
5
5
  "main": "models/index.js",
6
6
  "scripts": {