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.
- package/models/Subscription.js +49 -45
- package/package.json +1 -1
package/models/Subscription.js
CHANGED
|
@@ -22,15 +22,16 @@ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
|
|
|
22
22
|
createdAt: new Date(),
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
const recordSubscriptionHistory = async (
|
|
26
|
-
if (!
|
|
25
|
+
const recordSubscriptionHistory = async (oldSubscriptionDoc, updatedSubscriptionDoc) => {
|
|
26
|
+
if (!oldSubscriptionDoc || !updatedSubscriptionDoc) return;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// Build snapshot from the OLD state (before update)
|
|
29
|
+
const historySnapshot = buildSubscriptionHistorySnapshot(oldSubscriptionDoc);
|
|
29
30
|
|
|
30
|
-
//
|
|
31
|
-
await
|
|
32
|
-
{
|
|
33
|
-
{
|
|
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
|
-
//
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
453
|
-
|
|
454
|
-
if (
|
|
455
|
-
|
|
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 (
|
|
461
|
-
|
|
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
|
-
//
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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
|
-
|
|
471
|
-
|
|
472
|
-
if (
|
|
473
|
-
|
|
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
|
-
|
|
479
|
+
const options = this.getOptions ? this.getOptions() : this.options;
|
|
480
|
+
if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
|
|
480
481
|
|
|
481
|
-
|
|
482
|
-
if (
|
|
483
|
-
await
|
|
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.
|
|
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": {
|