Subscription.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import mongoose from '../infrastructure/Mongoose.mjs'
  2. import { TeamInviteSchema } from './TeamInvite.mjs'
  3. import { callbackify } from '@overleaf/promise-utils'
  4. const { Schema } = mongoose
  5. const { ObjectId } = Schema
  6. const PaymentProvider = {
  7. service: {
  8. type: String,
  9. },
  10. subscriptionId: {
  11. type: String,
  12. },
  13. state: {
  14. type: String,
  15. },
  16. pausePeriodStart: {
  17. type: Date,
  18. },
  19. pausePeriodEnd: {
  20. type: Date,
  21. },
  22. trialStartedAt: {
  23. type: Date,
  24. },
  25. trialEndsAt: {
  26. type: Date,
  27. },
  28. }
  29. export const SubscriptionSchema = new Schema(
  30. {
  31. admin_id: {
  32. type: ObjectId,
  33. ref: 'User',
  34. index: { unique: true, dropDups: true },
  35. },
  36. manager_ids: {
  37. type: [ObjectId],
  38. ref: 'User',
  39. required: true,
  40. validate: function (managers) {
  41. // require at least one manager
  42. return !!managers.length
  43. },
  44. },
  45. member_ids: [{ type: ObjectId, ref: 'User' }],
  46. groupPolicy: { type: ObjectId, ref: 'GroupPolicy' },
  47. invited_emails: [String],
  48. teamInvites: [TeamInviteSchema],
  49. recurlySubscription_id: String,
  50. lastSuccesfulSubscription: {
  51. planCode: {
  52. type: String,
  53. },
  54. addOns: Schema.Types.Mixed,
  55. },
  56. timesRevertedDueToFailedPayment: { type: Number, default: 0 },
  57. teamName: { type: String },
  58. teamNotice: { type: String },
  59. planCode: { type: String },
  60. groupPlan: { type: Boolean, default: false },
  61. domainCaptureEnabled: { type: Boolean, default: false },
  62. managedUsersEnabled: { type: Boolean, default: false },
  63. membersLimit: { type: Number, default: 0 },
  64. membersLimitNotificationSent: { type: Boolean, default: false },
  65. customAccount: Boolean,
  66. features: {
  67. managedUsers: { type: Boolean, default: true },
  68. groupSSO: { type: Boolean, default: true },
  69. domainCapture: { type: Boolean, default: false },
  70. },
  71. userFeaturesDisabled: Boolean,
  72. addOns: Schema.Types.Mixed,
  73. overleaf: {
  74. id: {
  75. type: Number,
  76. index: {
  77. unique: true,
  78. partialFilterExpression: { 'overleaf.id': { $exists: true } },
  79. },
  80. },
  81. },
  82. recurlyStatus: {
  83. state: {
  84. type: String,
  85. },
  86. trialStartedAt: {
  87. type: Date,
  88. },
  89. trialEndsAt: {
  90. type: Date,
  91. },
  92. },
  93. paymentProvider: PaymentProvider,
  94. previousPaymentProvider: PaymentProvider,
  95. collectionMethod: {
  96. type: String,
  97. enum: ['automatic', 'manual'],
  98. default: 'automatic',
  99. },
  100. v1_id: {
  101. type: Number,
  102. required: false,
  103. min: 1,
  104. },
  105. salesforce_id: {
  106. type: String,
  107. required: false,
  108. validate: {
  109. validator: function (salesforceId) {
  110. return (
  111. salesforceId == null ||
  112. salesforceId === '' ||
  113. salesforceId.match(/^(?:[A-Za-z0-9]{15}|[A-Za-z0-9]{18})$/)
  114. )
  115. },
  116. },
  117. },
  118. ssoConfig: { type: ObjectId, ref: 'SSOConfig' },
  119. },
  120. { minimize: false }
  121. )
  122. // Subscriptions have no v1 data to fetch
  123. async function fetchV1DataPromise() {
  124. return this
  125. }
  126. SubscriptionSchema.method('fetchV1Data', callbackify(fetchV1DataPromise))
  127. SubscriptionSchema.method('fetchV1DataPromise', fetchV1DataPromise)
  128. export const Subscription = mongoose.model('Subscription', SubscriptionSchema)