database-connector 2.4.5 → 2.4.6

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/CHANGELOG.md CHANGED
@@ -32,7 +32,12 @@
32
32
  - **BREAKING CHANGE**: Changed `storeId` to `sellerId` - now references User (seller) instead of Store
33
33
  - Updated all occurrences in main schema, subscriptionsHistory, and upcomingSubscriptions
34
34
  - Updated indexes to use `sellerId` instead of `storeId`
35
- - Updated history snapshot builder to track `sellerId`
35
+ - **BREAKING CHANGE**: Removed `histories` field and SubscriptionHistory collection tracking
36
+ - Simplified to use only embedded `subscriptionsHistory` array
37
+ - Automatic history snapshot creation now pushes to embedded subscriptionsHistory array on save/update
38
+ - History snapshots include all subscription fields (sellerId, planId, planTypeId, paymentTypeId, paymentIntentId, reductionOfferId, numberOfStores, billingAddress, invoiceRecipient, invoiceLink, status, dates, notes)
39
+ - Removed cascade delete logic (embedded arrays are deleted automatically with parent document)
40
+ - All history tracking done via embedded subscriptionsHistory and upcomingSubscriptions arrays
36
41
  - `planTypeId`: ObjectId reference to PlanType model
37
42
  - `numberOfStores`: Number (default: 1, minimum: 1) - tracks number of stores in subscription
38
43
  - `billingAddress`: Mixed object for billing address (Adresse de facturation)
@@ -1,7 +1,6 @@
1
1
  const mongoose = require('mongoose');
2
2
 
3
3
  const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
4
- subscriptionId: subscriptionDoc._id,
5
4
  paymentManagerId: subscriptionDoc.paymentManagerId,
6
5
  sellerId: subscriptionDoc.sellerId,
7
6
  planId: subscriptionDoc.planId,
@@ -24,28 +23,14 @@ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
24
23
  const recordSubscriptionHistory = async (subscriptionDoc) => {
25
24
  if (!subscriptionDoc) return;
26
25
 
27
- // Lazy-load to avoid require ordering issues
28
- // eslint-disable-next-line global-require
29
- const SubscriptionHistory = require('./SubscriptionHistory');
30
-
31
- const history = await SubscriptionHistory.create(
32
- buildSubscriptionHistorySnapshot(subscriptionDoc)
33
- );
26
+ const historySnapshot = buildSubscriptionHistorySnapshot(subscriptionDoc);
34
27
 
35
28
  await subscriptionDoc.updateOne(
36
- { $push: { histories: history._id } },
29
+ { $push: { subscriptionsHistory: historySnapshot } },
37
30
  { skipHistory: true }
38
31
  );
39
32
  };
40
33
 
41
- const deleteSubscriptionHistories = async (subscriptionId) => {
42
- if (!subscriptionId) return;
43
- // Lazy-load to avoid require ordering issues
44
- // eslint-disable-next-line global-require
45
- const SubscriptionHistory = require('./SubscriptionHistory');
46
- await SubscriptionHistory.deleteMany({ subscriptionId });
47
- };
48
-
49
34
  /**
50
35
  * @swagger
51
36
  * components:
@@ -127,11 +112,16 @@ const deleteSubscriptionHistories = async (subscriptionId) => {
127
112
  * type: string
128
113
  * nullable: true
129
114
  * description: Link to the invoice document
130
- * histories:
115
+ * subscriptionsHistory:
131
116
  * type: array
132
- * description: Immutable list of recorded subscription snapshots
117
+ * description: Embedded array of historical subscription snapshots
133
118
  * items:
134
- * $ref: '#/components/schemas/SubscriptionHistory'
119
+ * type: object
120
+ * upcomingSubscriptions:
121
+ * type: array
122
+ * description: Embedded array of upcoming subscription configurations
123
+ * items:
124
+ * type: object
135
125
  * example:
136
126
  * id: "507f1f77bcf86cd799439011"
137
127
  * sellerId: "507f1f77bcf86cd799439012"
@@ -227,12 +217,6 @@ const subscriptionSchema = new mongoose.Schema(
227
217
  type: String,
228
218
  default: null,
229
219
  },
230
- histories: [
231
- {
232
- type: mongoose.Schema.Types.ObjectId,
233
- ref: 'SubscriptionHistory',
234
- },
235
- ],
236
220
  subscriptionsHistory: [
237
221
  {
238
222
  paymentManagerId: {
@@ -405,7 +389,7 @@ const subscriptionSchema = new mongoose.Schema(
405
389
  { toJSON: { virtuals: true } }
406
390
  );
407
391
 
408
- // Auto-record history snapshots
392
+ // Auto-record history snapshots to embedded subscriptionsHistory array
409
393
  subscriptionSchema.post('save', async function (doc) {
410
394
  await recordSubscriptionHistory(doc);
411
395
  });
@@ -422,26 +406,6 @@ subscriptionSchema.post('updateOne', async function () {
422
406
  await recordSubscriptionHistory(updatedDoc);
423
407
  });
424
408
 
425
- // Cascade delete: remove all histories when a subscription is deleted
426
- subscriptionSchema.pre('deleteOne', { document: true, query: false }, async function () {
427
- await deleteSubscriptionHistories(this._id);
428
- });
429
-
430
- subscriptionSchema.post('findOneAndDelete', async function (doc) {
431
- if (!doc) return;
432
- await deleteSubscriptionHistories(doc._id);
433
- });
434
-
435
- subscriptionSchema.pre('deleteOne', { document: false, query: true }, async function () {
436
- const filter = this.getFilter();
437
- const docs = await this.model.find(filter).select('_id');
438
- if (!docs || docs.length === 0) return;
439
- const ids = docs.map((d) => d._id);
440
- // eslint-disable-next-line global-require
441
- const SubscriptionHistory = require('./SubscriptionHistory');
442
- await SubscriptionHistory.deleteMany({ subscriptionId: { $in: ids } });
443
- });
444
-
445
409
  // Indexes for performance optimization
446
410
  subscriptionSchema.index({ sellerId: 1 });
447
411
  subscriptionSchema.index({ status: 1 });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "database-connector",
3
- "version": "2.4.5",
3
+ "version": "2.4.6",
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": {