database-connector 2.2.0 → 2.2.2

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/Payment.js CHANGED
@@ -55,11 +55,6 @@ const mongoose = require('mongoose');
55
55
  * type: number
56
56
  * default: 0
57
57
  * description: Total price
58
- * transactions:
59
- * type: array
60
- * items:
61
- * type: string
62
- * description: References to related transactions
63
58
  * token:
64
59
  * type: string
65
60
  * description: Payment token
@@ -79,7 +74,6 @@ const mongoose = require('mongoose');
79
74
  * paymentCurrency: "USD"
80
75
  * paymentMethodId: "507f1f77bcf86cd799439020"
81
76
  * totalPrice: 150.00
82
- * transactions: ["507f1f77bcf86cd799439111", "507f1f77bcf86cd799439112"]
83
77
  * createdAt: "2025-12-07T10:30:00.000Z"
84
78
  * updatedAt: "2025-12-07T10:30:00.000Z"
85
79
  */
@@ -135,13 +129,7 @@ const paymentSchema = new mongoose.Schema(
135
129
  required: true,
136
130
  default: 0,
137
131
  },
138
- transactions: [
139
- {
140
- type: mongoose.Schema.Types.ObjectId,
141
- ref: 'Transaction',
142
- required: false,
143
- }
144
- ],
132
+ // transactions field removed: Transactions are no longer tracked as a separate model
145
133
  timestamps: {
146
134
  type: Date,
147
135
  required: true,
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,38 @@
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
+
3
36
  /**
4
37
  * @swagger
5
38
  * components:
@@ -58,6 +91,11 @@ const mongoose = require('mongoose');
58
91
  * items:
59
92
  * type: string
60
93
  * description: Notes about the subscription
94
+ * histories:
95
+ * type: array
96
+ * description: Immutable list of recorded subscription snapshots
97
+ * items:
98
+ * $ref: '#/components/schemas/SubscriptionHistory'
61
99
  * example:
62
100
  * id: "507f1f77bcf86cd799439011"
63
101
  * storeId: "507f1f77bcf86cd799439012"
@@ -124,6 +162,12 @@ const subscriptionSchema = new mongoose.Schema(
124
162
  required: true,
125
163
  },
126
164
  notes: [{ type: String }],
165
+ histories: [
166
+ {
167
+ type: mongoose.Schema.Types.ObjectId,
168
+ ref: 'SubscriptionHistory',
169
+ },
170
+ ],
127
171
  subscriptionsHistory: [
128
172
  {
129
173
  paymentManagerId: {
@@ -200,6 +244,23 @@ const subscriptionSchema = new mongoose.Schema(
200
244
  { toJSON: { virtuals: true } }
201
245
  );
202
246
 
247
+ // Auto-record history snapshots
248
+ subscriptionSchema.post('save', async function (doc) {
249
+ await recordSubscriptionHistory(doc);
250
+ });
251
+
252
+ subscriptionSchema.post('findOneAndUpdate', async function () {
253
+ if (this.getOptions && this.getOptions().skipHistory) return;
254
+ const updatedDoc = await this.model.findOne(this.getQuery());
255
+ await recordSubscriptionHistory(updatedDoc);
256
+ });
257
+
258
+ subscriptionSchema.post('updateOne', async function () {
259
+ if (this.getOptions && this.getOptions().skipHistory) return;
260
+ const updatedDoc = await this.model.findOne(this.getQuery());
261
+ await recordSubscriptionHistory(updatedDoc);
262
+ });
263
+
203
264
  // Indexes for performance optimization
204
265
  subscriptionSchema.index({ storeId: 1 });
205
266
  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.0",
3
+ "version": "2.2.2",
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": {