account.model.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // ./api/accounts/account.model.js
  2. import moment from "moment";
  3. import localDatabase from "../../common/localDatabase.js";
  4. const subscriptionTrial = "trial";
  5. const subscriptionPaymentFailed = "payment_failed";
  6. const subscriptionDeactivated = "deactivated";
  7. const subscriptionActive = "active";
  8. const schema = new localDatabase.Schema(
  9. {
  10. subdomain: String,
  11. displayName: String,
  12. companyName: String,
  13. companyVat: String,
  14. companyBillingAddress: String,
  15. companySdi: String,
  16. companyPec: String,
  17. companyPhone: String,
  18. companyEmail: String,
  19. companyCountry: String,
  20. privacyAccepted: {
  21. type: Boolean,
  22. default: false,
  23. },
  24. marketingAccepted: {
  25. type: Boolean,
  26. default: false,
  27. },
  28. stripeCustomerId: String,
  29. paymentFailed: {
  30. type: Boolean,
  31. default: false,
  32. },
  33. paymentFailedFirstAt: Date,
  34. paymentFailedSubscriptionEndsAt: Date,
  35. manualPayment: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. trialPeriodEndsAt: Date,
  40. stripePlanId: String,
  41. subscriptionExpiresAt: Date,
  42. planType: String,
  43. },
  44. { timestamps: true, toJSON: { virtuals: true } }
  45. );
  46. /*schema.virtual("subscriptionStatus").get(function () {
  47. if (moment(this.trialPeriodEndsAt).isAfter(Date.now())) {
  48. return subscriptionTrial;
  49. } else if (
  50. this.trialPeriodEndsAt &&
  51. moment(this.trialPeriodEndsAt).isBefore(Date.now())
  52. ) {
  53. return subscriptionDeactivated;
  54. } else if (
  55. this.paymentFailed &&
  56. moment(this.paymentFailedSubscriptionEndsAt).isAfter(Date.now())
  57. ) {
  58. return subscriptionPaymentFailed;
  59. } else if (
  60. this.paymentFailed &&
  61. moment(this.paymentFailedSubscriptionEndsAt).isBefore(Date.now())
  62. ) {
  63. return subscriptionDeactivated;
  64. } else if (moment(this.subscriptionExpiresAt).isBefore(Date.now())) {
  65. return subscriptionDeactivated;
  66. } else {
  67. return subscriptionActive;
  68. }
  69. });*/
  70. schema.virtual("subscriptionStatus").get(function () {
  71. // First check if there's an active subscription
  72. if (this.subscriptionExpiresAt && moment(this.subscriptionExpiresAt).isAfter(Date.now())) {
  73. return subscriptionActive;
  74. }
  75. // Then check for failed payments
  76. if (this.paymentFailed) {
  77. if (moment(this.paymentFailedSubscriptionEndsAt).isAfter(Date.now())) {
  78. return subscriptionPaymentFailed;
  79. }
  80. return subscriptionDeactivated;
  81. }
  82. // Then check trial status
  83. if (this.trialPeriodEndsAt) {
  84. if (moment(this.trialPeriodEndsAt).isAfter(Date.now())) {
  85. return subscriptionTrial;
  86. }
  87. return subscriptionDeactivated;
  88. }
  89. // If no conditions match, account is deactivated
  90. return subscriptionDeactivated;
  91. });
  92. schema.virtual("id").get(function () {
  93. return this._id;
  94. });
  95. const Account = localDatabase.model("Account", schema, "account");
  96. export default Account;