database-connector 2.4.5 → 2.4.7

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,14 +32,20 @@
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
43
+ - `stores`: Array of ObjectIds referencing Store - tracks which specific stores are included in this subscription
38
44
  - `billingAddress`: Mixed object for billing address (Adresse de facturation)
39
45
  - `invoiceRecipient`: Object with type (Person/Company enum) and name fields (Facture au nom de)
40
46
  - `invoiceLink`: String (nullable, default: null) - link to the invoice document
41
47
  - Updated `subscriptionsHistory` and `upcomingSubscriptions` arrays to include complete subscription snapshots
42
- - Both now include all fields: storeId, planId, planTypeId, paymentTypeId, paymentIntentId, reductionOfferId, numberOfStores, billingAddress, invoiceRecipient, invoiceLink
48
+ - Both now include all fields: storeId, planId, planTypeId, paymentTypeId, paymentIntentId, reductionOfferId, numberOfStores, stores, billingAddress, invoiceRecipient, invoiceLink
43
49
  - Makes history tracking consistent and comprehensive
44
50
  - Added `planTypeId` index for performance optimization
45
51
  - Updated history snapshot builder to include all new fields including invoiceLink
@@ -219,7 +225,8 @@ After comprehensive analysis of model usage across all microservices (AuthServic
219
225
  - `paymentManagerId`: For payment manager tracking
220
226
  - `storeId, status` (compound): For store subscription status
221
227
  - `status, endDate` (compound): For active subscription expiration monitoring
222
- - **Justification:** SubscriptionService queries subscriptions by store, status, and expiration dates. Compound indexes optimize active subscription monitoring and store subscription management.
228
+ - `stores`: For queries filtering by specific stores in the subscription
229
+ - **Justification:** SubscriptionService queries subscriptions by store, status, and expiration dates. Compound indexes optimize active subscription monitoring and store subscription management. The stores index enables efficient queries to find subscriptions containing specific stores.
223
230
 
224
231
  **13. UserAction Model** (`UserAction.js`)
225
232
  - **Indexes Added:**
@@ -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,
@@ -12,6 +11,7 @@ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
12
11
  paymentIntentId: subscriptionDoc.paymentIntentId,
13
12
  reductionOfferId: subscriptionDoc.reductionOfferId,
14
13
  numberOfStores: subscriptionDoc.numberOfStores,
14
+ stores: subscriptionDoc.stores,
15
15
  billingAddress: subscriptionDoc.billingAddress,
16
16
  invoiceRecipient: subscriptionDoc.invoiceRecipient,
17
17
  invoiceLink: subscriptionDoc.invoiceLink,
@@ -24,28 +24,14 @@ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
24
24
  const recordSubscriptionHistory = async (subscriptionDoc) => {
25
25
  if (!subscriptionDoc) return;
26
26
 
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
- );
27
+ const historySnapshot = buildSubscriptionHistorySnapshot(subscriptionDoc);
34
28
 
35
29
  await subscriptionDoc.updateOne(
36
- { $push: { histories: history._id } },
30
+ { $push: { subscriptionsHistory: historySnapshot } },
37
31
  { skipHistory: true }
38
32
  );
39
33
  };
40
34
 
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
35
  /**
50
36
  * @swagger
51
37
  * components:
@@ -80,6 +66,11 @@ const deleteSubscriptionHistories = async (subscriptionId) => {
80
66
  * type: number
81
67
  * default: 1
82
68
  * description: Number of stores in this subscription
69
+ * stores:
70
+ * type: array
71
+ * items:
72
+ * type: string
73
+ * description: Array of store IDs included in this subscription
83
74
  * billingAddress:
84
75
  * type: object
85
76
  * description: Billing address for the subscription
@@ -127,11 +118,16 @@ const deleteSubscriptionHistories = async (subscriptionId) => {
127
118
  * type: string
128
119
  * nullable: true
129
120
  * description: Link to the invoice document
130
- * histories:
121
+ * subscriptionsHistory:
122
+ * type: array
123
+ * description: Embedded array of historical subscription snapshots
124
+ * items:
125
+ * type: object
126
+ * upcomingSubscriptions:
131
127
  * type: array
132
- * description: Immutable list of recorded subscription snapshots
128
+ * description: Embedded array of upcoming subscription configurations
133
129
  * items:
134
- * $ref: '#/components/schemas/SubscriptionHistory'
130
+ * type: object
135
131
  * example:
136
132
  * id: "507f1f77bcf86cd799439011"
137
133
  * sellerId: "507f1f77bcf86cd799439012"
@@ -174,6 +170,15 @@ const subscriptionSchema = new mongoose.Schema(
174
170
  default: 1,
175
171
  min: 1,
176
172
  },
173
+ stores: {
174
+ type: [
175
+ {
176
+ type: mongoose.Schema.Types.ObjectId,
177
+ ref: 'Store',
178
+ },
179
+ ],
180
+ default: [],
181
+ },
177
182
  billingAddress: {
178
183
  type: mongoose.Schema.Types.Mixed,
179
184
  required: false,
@@ -227,12 +232,6 @@ const subscriptionSchema = new mongoose.Schema(
227
232
  type: String,
228
233
  default: null,
229
234
  },
230
- histories: [
231
- {
232
- type: mongoose.Schema.Types.ObjectId,
233
- ref: 'SubscriptionHistory',
234
- },
235
- ],
236
235
  subscriptionsHistory: [
237
236
  {
238
237
  paymentManagerId: {
@@ -283,6 +282,15 @@ const subscriptionSchema = new mongoose.Schema(
283
282
  type: Number,
284
283
  default: 1,
285
284
  },
285
+ stores: {
286
+ type: [
287
+ {
288
+ type: mongoose.Schema.Types.ObjectId,
289
+ ref: 'Store',
290
+ },
291
+ ],
292
+ default: [],
293
+ },
286
294
  billingAddress: {
287
295
  type: mongoose.Schema.Types.Mixed,
288
296
  required: false,
@@ -367,6 +375,15 @@ const subscriptionSchema = new mongoose.Schema(
367
375
  type: Number,
368
376
  default: 1,
369
377
  },
378
+ stores: {
379
+ type: [
380
+ {
381
+ type: mongoose.Schema.Types.ObjectId,
382
+ ref: 'Store',
383
+ },
384
+ ],
385
+ default: [],
386
+ },
370
387
  billingAddress: {
371
388
  type: mongoose.Schema.Types.Mixed,
372
389
  required: false,
@@ -405,7 +422,7 @@ const subscriptionSchema = new mongoose.Schema(
405
422
  { toJSON: { virtuals: true } }
406
423
  );
407
424
 
408
- // Auto-record history snapshots
425
+ // Auto-record history snapshots to embedded subscriptionsHistory array
409
426
  subscriptionSchema.post('save', async function (doc) {
410
427
  await recordSubscriptionHistory(doc);
411
428
  });
@@ -422,26 +439,6 @@ subscriptionSchema.post('updateOne', async function () {
422
439
  await recordSubscriptionHistory(updatedDoc);
423
440
  });
424
441
 
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
442
  // Indexes for performance optimization
446
443
  subscriptionSchema.index({ sellerId: 1 });
447
444
  subscriptionSchema.index({ status: 1 });
@@ -452,5 +449,6 @@ subscriptionSchema.index({ planTypeId: 1 });
452
449
  subscriptionSchema.index({ paymentManagerId: 1 });
453
450
  subscriptionSchema.index({ sellerId: 1, status: 1 });
454
451
  subscriptionSchema.index({ status: 1, endDate: 1 });
452
+ subscriptionSchema.index({ stores: 1 });
455
453
 
456
454
  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.5",
3
+ "version": "2.4.7",
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": {