SplitTest.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const _ = require('lodash')
  4. const MIN_NAME_LENGTH = 3
  5. const MAX_NAME_LENGTH = 200
  6. const MIN_VARIANT_NAME_LENGTH = 3
  7. const MAX_VARIANT_NAME_LENGTH = 255
  8. const NAME_REGEX = /^[a-zA-Z0-9\-_]+$/
  9. const RolloutPercentType = {
  10. type: Number,
  11. default: 0,
  12. min: [0, 'Rollout percentage must be between 0 and 100, got {VALUE}'],
  13. max: [100, 'Rollout percentage must be between 0 and 100, got {VALUE}'],
  14. required: true,
  15. }
  16. const VariantSchema = new Schema(
  17. {
  18. name: {
  19. type: String,
  20. minLength: MIN_VARIANT_NAME_LENGTH,
  21. maxLength: MAX_VARIANT_NAME_LENGTH,
  22. required: true,
  23. validate: {
  24. validator: function (input) {
  25. return input !== null && input !== 'default' && NAME_REGEX.test(input)
  26. },
  27. message: `invalid, cannot be 'default' and must match: ${NAME_REGEX}, got {VALUE}`,
  28. },
  29. },
  30. active: {
  31. type: Boolean,
  32. default: true,
  33. required: true,
  34. },
  35. rolloutPercent: RolloutPercentType,
  36. rolloutStripes: [
  37. {
  38. start: RolloutPercentType,
  39. end: RolloutPercentType,
  40. },
  41. ],
  42. },
  43. { _id: false }
  44. )
  45. const VersionSchema = new Schema(
  46. {
  47. versionNumber: {
  48. type: Number,
  49. default: 1,
  50. min: [1, 'must be 1 or higher, got {VALUE}'],
  51. required: true,
  52. },
  53. phase: {
  54. type: String,
  55. default: 'alpha',
  56. enum: ['alpha', 'beta', 'release'],
  57. required: true,
  58. },
  59. active: {
  60. type: Boolean,
  61. default: true,
  62. required: true,
  63. },
  64. variants: [VariantSchema],
  65. },
  66. { _id: false }
  67. )
  68. const SplitTestSchema = new Schema({
  69. name: {
  70. type: String,
  71. minLength: MIN_NAME_LENGTH,
  72. maxlength: MAX_NAME_LENGTH,
  73. required: true,
  74. unique: true,
  75. validate: {
  76. validator: function (input) {
  77. return input !== null && NAME_REGEX.test(input)
  78. },
  79. message: `invalid, must match: ${NAME_REGEX}`,
  80. },
  81. },
  82. versions: [VersionSchema],
  83. forbidReleasePhase: {
  84. type: Boolean,
  85. required: false,
  86. },
  87. })
  88. SplitTestSchema.methods.getCurrentVersion = function () {
  89. if (this.versions && this.versions.length > 0) {
  90. return _.maxBy(this.versions, 'versionNumber')
  91. } else {
  92. return undefined
  93. }
  94. }
  95. SplitTestSchema.methods.getVersion = function (versionNumber) {
  96. return _.find(this.versions || [], {
  97. versionNumber,
  98. })
  99. }
  100. module.exports = {
  101. SplitTest: mongoose.model('SplitTest', SplitTestSchema),
  102. SplitTestSchema,
  103. }