database-connector 2.2.1 → 2.2.3

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/Store.js CHANGED
@@ -87,6 +87,22 @@ const { getBaseURL, getDefaultStoreImagePath } = require('../config');
87
87
  * subscriptionId:
88
88
  * type: string
89
89
  * description: Reference to subscription
90
+ * reports:
91
+ * type: array
92
+ * items:
93
+ * type: object
94
+ * properties:
95
+ * idUser:
96
+ * type: string
97
+ * description: Reference to user who reported
98
+ * message:
99
+ * type: string
100
+ * description: Report message
101
+ * date:
102
+ * type: string
103
+ * format: date-time
104
+ * description: Report date
105
+ * description: Store reports
90
106
  * createdAt:
91
107
  * type: string
92
108
  * format: date-time
@@ -339,6 +355,19 @@ const storeSchema = new mongoose.Schema(
339
355
  type: mongoose.Schema.Types.ObjectId,
340
356
  ref: 'Subscription',
341
357
  },
358
+ reports: [
359
+ {
360
+ idUser: {
361
+ type: mongoose.Schema.Types.ObjectId,
362
+ ref: 'User',
363
+ },
364
+ message: { type: String },
365
+ date: {
366
+ type: Date,
367
+ required: true,
368
+ },
369
+ },
370
+ ],
342
371
  },
343
372
 
344
373
  { timestamps: true, toJSON: { virtuals: true } }
@@ -1,5 +1,46 @@
1
1
  const mongoose = require('mongoose');
2
2
 
3
+ const buildSubscriptionHistorySnapshot = (subscriptionDoc) => ({
4
+ subscriptionId: subscriptionDoc._id,
5
+ paymentManagerId: subscriptionDoc.paymentManagerId,
6
+ storeId: subscriptionDoc.storeId,
7
+ planId: subscriptionDoc.planId,
8
+ subscriptionOfferId: subscriptionDoc.subscriptionOfferId,
9
+ paymentAmount: subscriptionDoc.paymentAmount,
10
+ paymentTypeId: subscriptionDoc.paymentTypeId,
11
+ paymentIntentId: subscriptionDoc.paymentIntentId,
12
+ reductionOfferId: subscriptionDoc.reductionOfferId,
13
+ status: subscriptionDoc.status,
14
+ startDate: subscriptionDoc.startDate,
15
+ endDate: subscriptionDoc.endDate,
16
+ notes: subscriptionDoc.notes,
17
+ });
18
+
19
+ const recordSubscriptionHistory = async (subscriptionDoc) => {
20
+ if (!subscriptionDoc) return;
21
+
22
+ // Lazy-load to avoid require ordering issues
23
+ // eslint-disable-next-line global-require
24
+ const SubscriptionHistory = require('./SubscriptionHistory');
25
+
26
+ const history = await SubscriptionHistory.create(
27
+ buildSubscriptionHistorySnapshot(subscriptionDoc)
28
+ );
29
+
30
+ await subscriptionDoc.updateOne(
31
+ { $push: { histories: history._id } },
32
+ { skipHistory: true }
33
+ );
34
+ };
35
+
36
+ const deleteSubscriptionHistories = async (subscriptionId) => {
37
+ if (!subscriptionId) return;
38
+ // Lazy-load to avoid require ordering issues
39
+ // eslint-disable-next-line global-require
40
+ const SubscriptionHistory = require('./SubscriptionHistory');
41
+ await SubscriptionHistory.deleteMany({ subscriptionId });
42
+ };
43
+
3
44
  /**
4
45
  * @swagger
5
46
  * components:
@@ -58,6 +99,11 @@ const mongoose = require('mongoose');
58
99
  * items:
59
100
  * type: string
60
101
  * description: Notes about the subscription
102
+ * histories:
103
+ * type: array
104
+ * description: Immutable list of recorded subscription snapshots
105
+ * items:
106
+ * $ref: '#/components/schemas/SubscriptionHistory'
61
107
  * example:
62
108
  * id: "507f1f77bcf86cd799439011"
63
109
  * storeId: "507f1f77bcf86cd799439012"
@@ -124,6 +170,12 @@ const subscriptionSchema = new mongoose.Schema(
124
170
  required: true,
125
171
  },
126
172
  notes: [{ type: String }],
173
+ histories: [
174
+ {
175
+ type: mongoose.Schema.Types.ObjectId,
176
+ ref: 'SubscriptionHistory',
177
+ },
178
+ ],
127
179
  subscriptionsHistory: [
128
180
  {
129
181
  paymentManagerId: {
@@ -200,6 +252,43 @@ const subscriptionSchema = new mongoose.Schema(
200
252
  { toJSON: { virtuals: true } }
201
253
  );
202
254
 
255
+ // Auto-record history snapshots
256
+ subscriptionSchema.post('save', async function (doc) {
257
+ await recordSubscriptionHistory(doc);
258
+ });
259
+
260
+ subscriptionSchema.post('findOneAndUpdate', async function () {
261
+ if (this.getOptions && this.getOptions().skipHistory) return;
262
+ const updatedDoc = await this.model.findOne(this.getQuery());
263
+ await recordSubscriptionHistory(updatedDoc);
264
+ });
265
+
266
+ subscriptionSchema.post('updateOne', async function () {
267
+ if (this.getOptions && this.getOptions().skipHistory) return;
268
+ const updatedDoc = await this.model.findOne(this.getQuery());
269
+ await recordSubscriptionHistory(updatedDoc);
270
+ });
271
+
272
+ // Cascade delete: remove all histories when a subscription is deleted
273
+ subscriptionSchema.pre('deleteOne', { document: true, query: false }, async function () {
274
+ await deleteSubscriptionHistories(this._id);
275
+ });
276
+
277
+ subscriptionSchema.post('findOneAndDelete', async function (doc) {
278
+ if (!doc) return;
279
+ await deleteSubscriptionHistories(doc._id);
280
+ });
281
+
282
+ subscriptionSchema.pre('deleteOne', { document: false, query: true }, async function () {
283
+ const filter = this.getFilter();
284
+ const docs = await this.model.find(filter).select('_id');
285
+ if (!docs || docs.length === 0) return;
286
+ const ids = docs.map((d) => d._id);
287
+ // eslint-disable-next-line global-require
288
+ const SubscriptionHistory = require('./SubscriptionHistory');
289
+ await SubscriptionHistory.deleteMany({ subscriptionId: { $in: ids } });
290
+ });
291
+
203
292
  // Indexes for performance optimization
204
293
  subscriptionSchema.index({ storeId: 1 });
205
294
  subscriptionSchema.index({ status: 1 });
@@ -0,0 +1,152 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ /**
4
+ * @swagger
5
+ * components:
6
+ * schemas:
7
+ * SubscriptionHistory:
8
+ * type: object
9
+ * required:
10
+ * - subscriptionId
11
+ * - storeId
12
+ * - paymentAmount
13
+ * - startDate
14
+ * - endDate
15
+ * properties:
16
+ * id:
17
+ * type: string
18
+ * description: The subscription history identifier
19
+ * subscriptionId:
20
+ * type: string
21
+ * description: Reference to the subscription
22
+ * paymentManagerId:
23
+ * type: string
24
+ * description: Reference to the payment manager
25
+ * storeId:
26
+ * type: string
27
+ * description: Reference to the store
28
+ * planId:
29
+ * type: string
30
+ * description: Reference to the plan
31
+ * subscriptionOfferId:
32
+ * type: string
33
+ * description: Reference to subscription offer
34
+ * paymentAmount:
35
+ * type: number
36
+ * description: Payment amount
37
+ * paymentTypeId:
38
+ * type: string
39
+ * description: Reference to payment type
40
+ * paymentIntentId:
41
+ * type: string
42
+ * nullable: true
43
+ * description: Reference to payment intent (can be null)
44
+ * reductionOfferId:
45
+ * type: string
46
+ * description: Reference to reduction offer
47
+ * status:
48
+ * type: string
49
+ * enum: [inactive, active, delayed, suspended, upcoming, deleted]
50
+ * description: Subscription status at this point in time
51
+ * startDate:
52
+ * type: string
53
+ * format: date-time
54
+ * description: Subscription start date
55
+ * endDate:
56
+ * type: string
57
+ * format: date-time
58
+ * description: Subscription end date
59
+ * notes:
60
+ * type: array
61
+ * items:
62
+ * type: string
63
+ * description: Notes about the subscription
64
+ * createdAt:
65
+ * type: string
66
+ * format: date-time
67
+ * description: When this history snapshot was recorded
68
+ * updatedAt:
69
+ * type: string
70
+ * format: date-time
71
+ * example:
72
+ * id: "507f1f77bcf86cd799439111"
73
+ * subscriptionId: "507f1f77bcf86cd799439011"
74
+ * storeId: "507f1f77bcf86cd799439012"
75
+ * planId: "507f1f77bcf86cd799439013"
76
+ * paymentAmount: 99.99
77
+ * status: "active"
78
+ * startDate: "2025-12-01T00:00:00.000Z"
79
+ * endDate: "2026-12-01T00:00:00.000Z"
80
+ * createdAt: "2025-12-05T10:30:00.000Z"
81
+ */
82
+
83
+ const subscriptionHistorySchema = new mongoose.Schema(
84
+ {
85
+ subscriptionId: {
86
+ type: mongoose.Schema.Types.ObjectId,
87
+ required: true,
88
+ ref: 'Subscription',
89
+ index: true,
90
+ },
91
+ paymentManagerId: {
92
+ type: mongoose.Schema.Types.ObjectId,
93
+ required: false,
94
+ ref: 'User',
95
+ },
96
+ storeId: {
97
+ type: mongoose.Schema.Types.ObjectId,
98
+ required: true,
99
+ ref: 'Store',
100
+ },
101
+ planId: {
102
+ type: mongoose.Schema.Types.ObjectId,
103
+ required: false,
104
+ ref: 'Plan',
105
+ },
106
+ subscriptionOfferId: {
107
+ type: mongoose.Schema.Types.ObjectId,
108
+ required: false,
109
+ ref: 'SubscriptionOffer',
110
+ },
111
+ paymentAmount: {
112
+ type: Number,
113
+ required: true,
114
+ },
115
+ paymentTypeId: {
116
+ type: mongoose.Schema.Types.ObjectId,
117
+ required: false,
118
+ ref: 'PaymentType',
119
+ },
120
+ paymentIntentId: {
121
+ type: mongoose.Schema.Types.ObjectId,
122
+ required: false,
123
+ ref: 'Payment',
124
+ default: null,
125
+ },
126
+ reductionOfferId: {
127
+ type: mongoose.Schema.Types.ObjectId,
128
+ required: false,
129
+ ref: 'ReductionOffer',
130
+ },
131
+ status: {
132
+ type: String,
133
+ enum: ['inactive', 'active', 'delayed', 'suspended', 'upcoming', 'deleted'],
134
+ required: false,
135
+ },
136
+ startDate: {
137
+ type: Date,
138
+ required: true,
139
+ },
140
+ endDate: {
141
+ type: Date,
142
+ required: true,
143
+ },
144
+ notes: [{ type: String }],
145
+ },
146
+ { timestamps: true, toJSON: { virtuals: true } }
147
+ );
148
+
149
+ subscriptionHistorySchema.index({ subscriptionId: 1, createdAt: -1 });
150
+ subscriptionHistorySchema.index({ storeId: 1, createdAt: -1 });
151
+
152
+ module.exports = mongoose.model('SubscriptionHistory', subscriptionHistorySchema);
package/models/index.js CHANGED
@@ -28,6 +28,7 @@ const Offer = require('./Offer');
28
28
  const Category = require('./Category');
29
29
  const ResetPassword = require('./ResetPassword');
30
30
  const StoreRate = require('./StoreRate');
31
+ const SubscriptionHistory = require('./SubscriptionHistory');
31
32
  const Subscription = require('./Subscription');
32
33
  const Plan = require('./Plan');
33
34
  const SubscriptionOffer = require('./SubscriptionOffer');
@@ -65,6 +66,7 @@ module.exports = {
65
66
  Category,
66
67
  ResetPassword,
67
68
  StoreRate,
69
+ SubscriptionHistory,
68
70
  Subscription,
69
71
  Plan,
70
72
  SubscriptionOffer,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "database-connector",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
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": {