database-connector 2.4.4 → 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
@@ -29,6 +29,15 @@
29
29
  - Updated Swagger documentation to reflect new field
30
30
 
31
31
  - **Subscription Model**: Added multiple new fields and improvements
32
+ - **BREAKING CHANGE**: Changed `storeId` to `sellerId` - now references User (seller) instead of Store
33
+ - Updated all occurrences in main schema, subscriptionsHistory, and upcomingSubscriptions
34
+ - Updated indexes to use `sellerId` instead of `storeId`
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
32
41
  - `planTypeId`: ObjectId reference to PlanType model
33
42
  - `numberOfStores`: Number (default: 1, minimum: 1) - tracks number of stores in subscription
34
43
  - `billingAddress`: Mixed object for billing address (Adresse de facturation)
@@ -1,9 +1,8 @@
1
1
  const mongoose = require('mongoose');
2
2
 
3
3
  const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
4
- subscriptionId: subscriptionDoc._id,
5
4
  paymentManagerId: subscriptionDoc.paymentManagerId,
6
- storeId: subscriptionDoc.storeId,
5
+ sellerId: subscriptionDoc.sellerId,
7
6
  planId: subscriptionDoc.planId,
8
7
  planTypeId: subscriptionDoc.planTypeId,
9
8
  subscriptionOfferId: subscriptionDoc.subscriptionOfferId,
@@ -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:
@@ -53,7 +38,7 @@ const deleteSubscriptionHistories = async (subscriptionId) => {
53
38
  * Subscription:
54
39
  * type: object
55
40
  * required:
56
- * - storeId
41
+ * - sellerId
57
42
  * - paymentAmount
58
43
  * - startDate
59
44
  * - endDate
@@ -64,9 +49,9 @@ const deleteSubscriptionHistories = async (subscriptionId) => {
64
49
  * paymentManagerId:
65
50
  * type: string
66
51
  * description: Reference to the payment manager
67
- * storeId:
52
+ * sellerId:
68
53
  * type: string
69
- * description: Reference to the store
54
+ * description: Reference to the seller (user)
70
55
  * planId:
71
56
  * type: string
72
57
  * description: Reference to the plan
@@ -127,14 +112,19 @@ 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
- * storeId: "507f1f77bcf86cd799439012"
127
+ * sellerId: "507f1f77bcf86cd799439012"
138
128
  * planId: "507f1f77bcf86cd799439013"
139
129
  * paymentAmount: 99.99
140
130
  * paymentIntentId: "507f1f77bcf86cd799439099"
@@ -149,10 +139,10 @@ const subscriptionSchema = new mongoose.Schema(
149
139
  required: false,
150
140
  ref: 'User',
151
141
  },
152
- storeId: {
142
+ sellerId: {
153
143
  type: mongoose.Schema.Types.ObjectId,
154
144
  required: true,
155
- ref: 'Store',
145
+ ref: 'User',
156
146
  },
157
147
  planId: {
158
148
  type: mongoose.Schema.Types.ObjectId,
@@ -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: {
@@ -240,10 +224,10 @@ const subscriptionSchema = new mongoose.Schema(
240
224
  required: false,
241
225
  ref: 'User',
242
226
  },
243
- storeId: {
227
+ sellerId: {
244
228
  type: mongoose.Schema.Types.ObjectId,
245
229
  required: false,
246
- ref: 'Store',
230
+ ref: 'User',
247
231
  },
248
232
  planId: {
249
233
  type: mongoose.Schema.Types.ObjectId,
@@ -324,10 +308,10 @@ const subscriptionSchema = new mongoose.Schema(
324
308
  required: false,
325
309
  ref: 'User',
326
310
  },
327
- storeId: {
311
+ sellerId: {
328
312
  type: mongoose.Schema.Types.ObjectId,
329
313
  required: false,
330
- ref: 'Store',
314
+ ref: 'User',
331
315
  },
332
316
  planId: {
333
317
  type: mongoose.Schema.Types.ObjectId,
@@ -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,35 +406,15 @@ 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
- subscriptionSchema.index({ storeId: 1 });
410
+ subscriptionSchema.index({ sellerId: 1 });
447
411
  subscriptionSchema.index({ status: 1 });
448
412
  subscriptionSchema.index({ endDate: 1 });
449
413
  subscriptionSchema.index({ startDate: 1 });
450
414
  subscriptionSchema.index({ planId: 1 });
451
415
  subscriptionSchema.index({ planTypeId: 1 });
452
416
  subscriptionSchema.index({ paymentManagerId: 1 });
453
- subscriptionSchema.index({ storeId: 1, status: 1 });
417
+ subscriptionSchema.index({ sellerId: 1, status: 1 });
454
418
  subscriptionSchema.index({ status: 1, endDate: 1 });
455
419
 
456
420
  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.4",
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": {