-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.subscription.model.mongoose.js
More file actions
97 lines (90 loc) · 2.02 KB
/
Copy pathbilling.subscription.model.mongoose.js
File metadata and controls
97 lines (90 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Module dependencies
*/
import mongoose from 'mongoose';
import config from '../../../config/index.js';
const Schema = mongoose.Schema;
/**
* Data Model Mongoose
*/
const SubscriptionMongoose = new Schema(
{
organization: {
type: Schema.ObjectId,
ref: 'Organization',
unique: true,
required: true,
},
stripeCustomerId: {
type: String,
unique: true,
sparse: true,
},
stripeSubscriptionId: {
type: String,
unique: true,
sparse: true,
},
plan: {
type: String,
enum: config.billing.plans,
default: 'free',
},
status: {
type: String,
enum: config.billing.statuses,
default: 'active',
},
currentPeriodEnd: {
type: Date,
},
cancelAtPeriodEnd: {
type: Boolean,
default: false,
},
// ── Meter fields (sparse — backward-compatible additions) ────────────────
/**
* The plan version active on this subscription (e.g. "v1", "v2").
* Only populated when meterMode is enabled.
*/
planVersion: {
type: String,
sparse: true,
},
/**
* Start of the current billing period. Used to detect period changes
* in webhook handlers and trigger meter period resets.
*/
currentPeriodStart: {
type: Date,
default: null,
},
/**
* Timestamp when the subscription first entered past_due status.
* Used to enforce the 7-day grace period before degraded mode.
*/
pastDueSince: {
type: Date,
default: null,
},
},
{
timestamps: true,
},
);
/**
* Returns the hex string representation of the document ObjectId.
* @returns {string} Hex string of the ObjectId.
*/
function addID() {
return this._id.toHexString();
}
/**
* Model configuration
*/
SubscriptionMongoose.virtual('id').get(addID);
// Ensure virtual fields are serialised.
SubscriptionMongoose.set('toJSON', {
virtuals: true,
});
mongoose.model('Subscription', SubscriptionMongoose);