database-connector 2.4.11 → 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,25 +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
441
  subscriptionSchema.post('save', async function (doc, next) {
432
- // For save operations, check the $locals property where custom options are stored
433
- if (doc.$locals && doc.$locals.skipHistory) return next();
434
- if (this.$locals && this.$locals.skipHistory) return 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
+
435
448
  await recordSubscriptionHistory(doc);
436
449
  next();
437
450
  });
438
451
 
439
- subscriptionSchema.post('findOneAndUpdate', async function () {
440
- if (this.getOptions && this.getOptions().skipHistory) return;
441
- const updatedDoc = await this.model.findOne(this.getQuery());
442
- 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();
443
476
  });
444
477
 
445
478
  subscriptionSchema.post('updateOne', async function () {
446
- if (this.getOptions && this.getOptions().skipHistory) return;
479
+ if (this._skipHistory) return;
480
+
447
481
  const updatedDoc = await this.model.findOne(this.getQuery());
448
- await recordSubscriptionHistory(updatedDoc);
482
+ if (updatedDoc) {
483
+ await recordSubscriptionHistory(updatedDoc);
484
+ }
449
485
  });
450
486
 
451
487
  // Indexes for performance optimization
@@ -460,4 +496,4 @@ subscriptionSchema.index({ sellerId: 1, status: 1 });
460
496
  subscriptionSchema.index({ status: 1, endDate: 1 });
461
497
  subscriptionSchema.index({ stores: 1 });
462
498
 
463
- 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.11",
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": {