123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import moment from "moment";
- import localDatabase from "../../common/localDatabase.js";
- const subscriptionTrial = "trial";
- const subscriptionPaymentFailed = "payment_failed";
- const subscriptionDeactivated = "deactivated";
- const subscriptionActive = "active";
- const schema = new localDatabase.Schema(
- {
- subdomain: String,
- displayName: String,
- companyName: String,
- companyVat: String,
- companyBillingAddress: String,
- companySdi: String,
- companyPec: String,
- companyPhone: String,
- companyEmail: String,
- companyCountry: String,
- privacyAccepted: {
- type: Boolean,
- default: false,
- },
- marketingAccepted: {
- type: Boolean,
- default: false,
- },
- stripeCustomerId: String,
- paymentFailed: {
- type: Boolean,
- default: false,
- },
- paymentFailedFirstAt: Date,
- paymentFailedSubscriptionEndsAt: Date,
- manualPayment: {
- type: Boolean,
- default: false,
- },
- trialPeriodEndsAt: Date,
- stripePlanId: String,
- subscriptionExpiresAt: Date,
- planType: String,
- },
- { timestamps: true, toJSON: { virtuals: true } }
- );
- schema.virtual("subscriptionStatus").get(function () {
-
- if (this.subscriptionExpiresAt && moment(this.subscriptionExpiresAt).isAfter(Date.now())) {
- return subscriptionActive;
- }
-
-
- if (this.paymentFailed) {
- if (moment(this.paymentFailedSubscriptionEndsAt).isAfter(Date.now())) {
- return subscriptionPaymentFailed;
- }
- return subscriptionDeactivated;
- }
-
-
- if (this.trialPeriodEndsAt) {
- if (moment(this.trialPeriodEndsAt).isAfter(Date.now())) {
- return subscriptionTrial;
- }
- return subscriptionDeactivated;
- }
-
-
- return subscriptionDeactivated;
- });
- schema.virtual("id").get(function () {
- return this._id;
- });
- const Account = localDatabase.model("Account", schema, "account");
- export default Account;
|