database-connector 2.4.13 → 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 +52 -15
- package/package.json +1 -1
package/models/Subscription.js
CHANGED
|
@@ -22,14 +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
|
+
// Push the old state to the updated document's history
|
|
32
|
+
await updatedSubscriptionDoc.updateOne(
|
|
31
33
|
{ $push: { subscriptionsHistory: historySnapshot } },
|
|
32
|
-
{
|
|
34
|
+
{ skipHistory: true }
|
|
33
35
|
);
|
|
34
36
|
};
|
|
35
37
|
|
|
@@ -428,27 +430,62 @@ const subscriptionSchema = new mongoose.Schema(
|
|
|
428
430
|
);
|
|
429
431
|
|
|
430
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
|
|
431
435
|
subscriptionSchema.post('save', async function (doc, next) {
|
|
432
|
-
//
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
436
|
+
// Skip history on save operations (creation or manual saves)
|
|
437
|
+
// History should only be recorded on updates via findOneAndUpdate/updateOne
|
|
438
|
+
return next();
|
|
439
|
+
});
|
|
440
|
+
|
|
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;
|
|
450
|
+
}
|
|
438
451
|
});
|
|
439
452
|
|
|
440
453
|
subscriptionSchema.post('findOneAndUpdate', async function () {
|
|
441
454
|
const options = this.getOptions ? this.getOptions() : this.options;
|
|
442
455
|
if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
|
|
443
|
-
|
|
444
|
-
|
|
456
|
+
|
|
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
|
+
}
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
|
|
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;
|
|
475
|
+
}
|
|
445
476
|
});
|
|
446
477
|
|
|
447
478
|
subscriptionSchema.post('updateOne', async function () {
|
|
448
479
|
const options = this.getOptions ? this.getOptions() : this.options;
|
|
449
480
|
if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
|
|
450
|
-
|
|
451
|
-
|
|
481
|
+
|
|
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
|
+
}
|
|
488
|
+
}
|
|
452
489
|
});
|
|
453
490
|
|
|
454
491
|
// Indexes for performance optimization
|
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": {
|