database-connector 2.4.12 → 2.4.14

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.
@@ -22,15 +22,16 @@ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
22
22
  createdAt: new Date(),
23
23
  });
24
24
 
25
- const recordSubscriptionHistory = async (subscriptionDoc) => {
26
- if (!subscriptionDoc) return;
25
+ const recordSubscriptionHistory = async (oldSubscriptionDoc, updatedSubscriptionDoc) => {
26
+ if (!oldSubscriptionDoc || !updatedSubscriptionDoc) return;
27
27
 
28
- const historySnapshot = buildSubscriptionHistorySnapshot(subscriptionDoc);
28
+ // Build snapshot from the OLD state (before update)
29
+ const historySnapshot = buildSubscriptionHistorySnapshot(oldSubscriptionDoc);
29
30
 
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 } }
31
+ // Push the old state to the updated document's history
32
+ await updatedSubscriptionDoc.updateOne(
33
+ { $push: { subscriptionsHistory: historySnapshot } },
34
+ { skipHistory: true }
34
35
  );
35
36
  };
36
37
 
@@ -428,59 +429,62 @@ const subscriptionSchema = new mongoose.Schema(
428
429
  { toJSON: { virtuals: true } }
429
430
  );
430
431
 
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
-
440
432
  // Auto-record history snapshots to embedded subscriptionsHistory array
433
+ // On creation: Skip history (no previous state to record)
434
+ // On update: Record the PREVIOUS state before the update happens
441
435
  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
-
448
- await recordSubscriptionHistory(doc);
449
- next();
436
+ // Skip history on save operations (creation or manual saves)
437
+ // History should only be recorded on updates via findOneAndUpdate/updateOne
438
+ return next();
450
439
  });
451
440
 
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;
441
+ subscriptionSchema.pre('findOneAndUpdate', async function () {
442
+ const options = this.getOptions ? this.getOptions() : this.options;
443
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
444
+
445
+ // Fetch the OLD document BEFORE the update happens
446
+ const oldDoc = await this.model.findOne(this.getQuery());
447
+ if (oldDoc) {
448
+ // Store old document in options to access in post hook
449
+ this._oldDoc = oldDoc;
456
450
  }
457
- next();
458
451
  });
459
452
 
460
- subscriptionSchema.post('findOneAndUpdate', async function (doc) {
461
- if (this._skipHistory) return;
453
+ subscriptionSchema.post('findOneAndUpdate', async function () {
454
+ const options = this.getOptions ? this.getOptions() : this.options;
455
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
462
456
 
463
- // Get the updated document
464
- const updatedDoc = doc || await this.model.findOne(this.getQuery());
465
- if (updatedDoc) {
466
- await recordSubscriptionHistory(updatedDoc);
457
+ // Record history from the OLD document (captured in pre hook)
458
+ if (this._oldDoc) {
459
+ const updatedDoc = await this.model.findOne(this.getQuery());
460
+ if (updatedDoc) {
461
+ await recordSubscriptionHistory(this._oldDoc, updatedDoc);
462
+ }
467
463
  }
468
464
  });
469
465
 
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;
466
+ subscriptionSchema.pre('updateOne', async function () {
467
+ const options = this.getOptions ? this.getOptions() : this.options;
468
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
469
+
470
+ // Fetch the OLD document BEFORE the update happens
471
+ const oldDoc = await this.model.findOne(this.getQuery());
472
+ if (oldDoc) {
473
+ // Store old document in options to access in post hook
474
+ this._oldDoc = oldDoc;
474
475
  }
475
- next();
476
476
  });
477
477
 
478
478
  subscriptionSchema.post('updateOne', async function () {
479
- if (this._skipHistory) return;
479
+ const options = this.getOptions ? this.getOptions() : this.options;
480
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
480
481
 
481
- const updatedDoc = await this.model.findOne(this.getQuery());
482
- if (updatedDoc) {
483
- await recordSubscriptionHistory(updatedDoc);
482
+ // Record history from the OLD document (captured in pre hook)
483
+ if (this._oldDoc) {
484
+ const updatedDoc = await this.model.findOne(this.getQuery());
485
+ if (updatedDoc) {
486
+ await recordSubscriptionHistory(this._oldDoc, updatedDoc);
487
+ }
484
488
  }
485
489
  });
486
490
 
@@ -496,4 +500,4 @@ subscriptionSchema.index({ sellerId: 1, status: 1 });
496
500
  subscriptionSchema.index({ status: 1, endDate: 1 });
497
501
  subscriptionSchema.index({ stores: 1 });
498
502
 
499
- module.exports = mongoose.model('Subscription', subscriptionSchema);
503
+ 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.12",
3
+ "version": "2.4.14",
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": {