codebase-models 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/index.d.ts +3 -1
  2. package/dist/index.js +6 -1
  3. package/dist/src/constant.d.ts +7 -0
  4. package/dist/src/constant.js +17 -0
  5. package/dist/src/models/Announcement.d.ts +1 -0
  6. package/dist/src/models/Announcement.js +31 -0
  7. package/dist/src/models/Audience.d.ts +1 -0
  8. package/dist/src/models/Audience.js +31 -0
  9. package/dist/src/models/BqPreCompiledData.d.ts +1 -0
  10. package/dist/src/models/BqPreCompiledData.js +31 -0
  11. package/dist/src/models/Client.d.ts +1 -0
  12. package/dist/src/models/Client.js +31 -0
  13. package/dist/src/models/ClientAdditionalRevenue.d.ts +3 -1
  14. package/dist/src/models/ClientAdditionalRevenue.js +48 -0
  15. package/dist/src/models/ClientLearning.d.ts +1 -0
  16. package/dist/src/models/ClientLearning.js +31 -0
  17. package/dist/src/models/ClientLinks.d.ts +1 -0
  18. package/dist/src/models/ClientLinks.js +31 -0
  19. package/dist/src/models/ClientNextSteps.d.ts +1 -0
  20. package/dist/src/models/ClientNextSteps.js +30 -0
  21. package/dist/src/models/ClientNote.d.ts +1 -0
  22. package/dist/src/models/ClientNote.js +31 -0
  23. package/dist/src/models/ClientScript.d.ts +12 -11
  24. package/dist/src/models/ClientScript.js +54 -8
  25. package/dist/src/models/ClientStrategy.d.ts +1 -0
  26. package/dist/src/models/ClientStrategy.js +31 -0
  27. package/dist/src/models/CustomQuery.d.ts +1 -0
  28. package/dist/src/models/CustomQuery.js +30 -1
  29. package/dist/src/models/Environment.d.ts +2 -0
  30. package/dist/src/models/Environment.js +34 -0
  31. package/dist/src/models/Goal.d.ts +1 -0
  32. package/dist/src/models/Goal.js +31 -1
  33. package/dist/src/models/Page.d.ts +2 -0
  34. package/dist/src/models/Page.js +48 -0
  35. package/dist/src/models/Report.d.ts +1 -0
  36. package/dist/src/models/Report.js +30 -0
  37. package/dist/src/models/StageInCustomerJourney.d.ts +1 -0
  38. package/dist/src/models/StageInCustomerJourney.js +31 -0
  39. package/dist/src/models/Test.d.ts +2 -0
  40. package/dist/src/models/Test.js +46 -2
  41. package/dist/src/models/TestTimeline.d.ts +39 -0
  42. package/dist/src/models/TestTimeline.js +79 -0
  43. package/index.ts +4 -1
  44. package/package.json +3 -2
  45. package/src/constant.ts +17 -0
  46. package/src/models/Announcement.ts +22 -0
  47. package/src/models/Audience.ts +23 -0
  48. package/src/models/BqPreCompiledData.ts +22 -0
  49. package/src/models/Client.ts +22 -0
  50. package/src/models/ClientAdditionalRevenue.ts +42 -1
  51. package/src/models/ClientLearning.ts +22 -0
  52. package/src/models/ClientLinks.ts +23 -2
  53. package/src/models/ClientNextSteps.ts +20 -0
  54. package/src/models/ClientNote.ts +22 -1
  55. package/src/models/ClientScript.ts +56 -20
  56. package/src/models/ClientStrategy.ts +24 -0
  57. package/src/models/CustomQuery.ts +22 -2
  58. package/src/models/Environment.ts +27 -0
  59. package/src/models/Goal.ts +22 -2
  60. package/src/models/Page.ts +43 -0
  61. package/src/models/Report.ts +22 -0
  62. package/src/models/StageInCustomerJourney.ts +22 -0
  63. package/src/models/Test.ts +40 -3
  64. package/src/models/TestTimeline.ts +61 -0
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IClientNote extends Document {
5
+ iid: string;
4
6
  organizationId?: mongoose.Schema.Types.ObjectId;
5
7
  client?: mongoose.Schema.Types.ObjectId;
6
8
  viewId?: string;
@@ -14,6 +16,10 @@ export interface IClientNote extends Document {
14
16
 
15
17
  const ClientNoteSchema = new Schema<IClientNote>(
16
18
  {
19
+ iid: {
20
+ type: String,
21
+ trim: true,
22
+ },
17
23
  organizationId: {
18
24
  type: mongoose.Schema.Types.ObjectId,
19
25
  ref: "organization",
@@ -51,9 +57,24 @@ const ClientNoteSchema = new Schema<IClientNote>(
51
57
  {
52
58
  timestamps: true,
53
59
  }
54
- );
60
+ );
55
61
 
62
+ ClientNoteSchema.pre('save', async function(next) {
63
+ if (!this.iid) {
64
+ let unique = false;
65
+ while (!unique) {
66
+ const id = generateRandomIID();
67
+ const existing = await mongoose.models.clientnote.findOne({ iid: id });
68
+ if (!existing) {
69
+ this.iid = id;
70
+ unique = true;
71
+ }
72
+ }
73
+ }
74
+ next();
75
+ });
56
76
  ClientNoteSchema.index({
77
+ iid: 1,
57
78
  client: 1,
58
79
  viewId: 1,
59
80
  });
@@ -1,26 +1,24 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
-
3
- export interface IClientScriptSchema extends Document {
4
- scriptUrl: string;
5
- viewId: string;
6
- }
2
+ import { generateRandomIID } from "../constant";
7
3
 
8
4
  export interface IClientScript extends Document {
5
+ iid: string;
9
6
  organizationId?: mongoose.Schema.Types.ObjectId;
10
7
  client?: mongoose.Schema.Types.ObjectId;
8
+ adhere_dnt: Boolean;
9
+ allow_in_iframes: Boolean;
11
10
  viewId?: string;
12
11
  clientScriptURL: string;
13
12
  jquery_include: string;
14
13
  debug: Boolean;
15
14
  restrict_debug: Boolean;
16
- adhere_dnt: Boolean;
17
15
  max_cookie_lifetime: Number;
18
16
  activation_mode: string;
19
17
  use_antiflicker: Boolean;
20
18
  snippet_revision: Number;
19
+ snippet_version: Number;
21
20
  rules: string;
22
21
  rules_tracking: string;
23
- tracker_url: string;
24
22
  asset_url: string;
25
23
  metadata_key_exp_1: mongoose.Schema.Types.Mixed;
26
24
  metadata_key_exp_2: mongoose.Schema.Types.Mixed;
@@ -28,19 +26,26 @@ export interface IClientScript extends Document {
28
26
  metadata_key_var_1: mongoose.Schema.Types.Mixed;
29
27
  metadata_key_var_2: mongoose.Schema.Types.Mixed;
30
28
  metadata_key_var_3: mongoose.Schema.Types.Mixed;
31
- cookie_domain: string;
32
- snippet_version: Number;
29
+ logDomain: string;
33
30
  storage: string;
34
31
  run_only_on_reinit: Boolean;
35
- allow_in_iframes: Boolean;
36
32
  visual_editor_version: Number;
37
- jscode: string;
38
- csscode: string;
33
+ js: string;
34
+ css: string;
35
+ helper_js: string;
39
36
  is_spa: Boolean;
37
+ isPreview: Boolean;
38
+ goalsEnabled: Boolean;
39
+ optedOut: Boolean;
40
+ liveEventListenersSet: Boolean;
40
41
  }
41
42
 
42
43
  const ClientScriptSchema = new Schema<IClientScript>(
43
44
  {
45
+ iid: {
46
+ type: String,
47
+ trim: true,
48
+ },
44
49
  organizationId: {
45
50
  type: mongoose.Schema.Types.ObjectId,
46
51
  ref: "organization",
@@ -100,10 +105,6 @@ const ClientScriptSchema = new Schema<IClientScript>(
100
105
  default: "return true;",
101
106
  trim: true,
102
107
  },
103
- tracker_url: {
104
- type: String,
105
- trim: true,
106
- },
107
108
  asset_url: {
108
109
  type: String,
109
110
  trim: true,
@@ -126,7 +127,7 @@ const ClientScriptSchema = new Schema<IClientScript>(
126
127
  metadata_key_var_3: {
127
128
  type: mongoose.Schema.Types.Mixed,
128
129
  },
129
- cookie_domain: {
130
+ logDomain: {
130
131
  type: String,
131
132
  trim: true,
132
133
  },
@@ -149,11 +150,15 @@ const ClientScriptSchema = new Schema<IClientScript>(
149
150
  visual_editor_version: {
150
151
  type: Number,
151
152
  },
152
- jscode: {
153
+ js: {
154
+ type: String,
155
+ trim: true,
156
+ },
157
+ css: {
153
158
  type: String,
154
159
  trim: true,
155
160
  },
156
- csscode: {
161
+ helper_js: {
157
162
  type: String,
158
163
  trim: true,
159
164
  },
@@ -161,17 +166,48 @@ const ClientScriptSchema = new Schema<IClientScript>(
161
166
  type: Boolean,
162
167
  default: false,
163
168
  },
169
+ isPreview: {
170
+ type: Boolean,
171
+ default: false,
172
+ },
173
+ goalsEnabled: {
174
+ type: Boolean,
175
+ default: false,
176
+ },
177
+ optedOut: {
178
+ type: Boolean,
179
+ default: false,
180
+ },
181
+ liveEventListenersSet: {
182
+ type: Boolean,
183
+ default: false,
184
+ },
164
185
  },
165
186
  {
166
187
  timestamps: true,
167
188
  }
168
189
  );
169
190
 
191
+ ClientScriptSchema.pre('save', async function(next) {
192
+ if (!this.iid) {
193
+ let unique = false;
194
+ while (!unique) {
195
+ const id = generateRandomIID();
196
+ const existing = await mongoose.models.clientscript.findOne({ iid: id });
197
+ if (!existing) {
198
+ this.iid = id;
199
+ unique = true;
200
+ }
201
+ }
202
+ }
203
+ next();
204
+ });
205
+
206
+ ClientScriptSchema.index({ iid: 1 }, { unique: true });
170
207
  // Compound indexes for common query patterns
171
208
  ClientScriptSchema.index({ client: 1 });
172
209
  // Sparse indexes for optional fields
173
210
  ClientScriptSchema.index({ organisationId: 1 }, { sparse: true });
174
- ClientScriptSchema.index({ tracker_url: 1 }, { sparse: true });
175
211
  ClientScriptSchema.index({ asset_url: 1 }, { sparse: true });
176
212
 
177
213
 
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IClientStrategy extends Document {
5
+ iid: string;
4
6
  year: mongoose.Schema.Types.Mixed;
5
7
  month: mongoose.Schema.Types.Mixed;
6
8
  startDate?: Date;
@@ -15,6 +17,10 @@ export interface IClientStrategy extends Document {
15
17
 
16
18
  const ClientStrategySchema = new Schema<IClientStrategy>(
17
19
  {
20
+ iid: {
21
+ type: String,
22
+ trim: true,
23
+ },
18
24
  year: {
19
25
  type: mongoose.Schema.Types.Mixed,
20
26
  required: true,
@@ -55,8 +61,26 @@ const ClientStrategySchema = new Schema<IClientStrategy>(
55
61
  {
56
62
  timestamps: true,
57
63
  }
64
+
58
65
  );
59
66
 
67
+ ClientStrategySchema.pre('save', async function(next) {
68
+ if (!this.iid) {
69
+ let unique = false;
70
+ while (!unique) {
71
+ const id = generateRandomIID();
72
+ const existing = await mongoose.models.clientstrategy.findOne({ iid: id });
73
+ if (!existing) {
74
+ this.iid = id;
75
+ unique = true;
76
+ }
77
+ }
78
+ }
79
+ next();
80
+ });
81
+
82
+ ClientStrategySchema.index({ iid: 1 }, { unique: true });
83
+
60
84
  ClientStrategySchema.index({
61
85
  year: 1,
62
86
  month: 1,
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Schema, model, Document } from 'mongoose';
2
+ import { generateRandomIID } from '../constant';
2
3
 
3
4
  interface ICustomQuery extends Document {
5
+ iid: string;
4
6
  organizationId?: mongoose.Schema.Types.ObjectId;
5
7
  title: string;
6
8
  module: string;
@@ -11,6 +13,10 @@ interface ICustomQuery extends Document {
11
13
 
12
14
  const CustomQuerySchema = new Schema<ICustomQuery>(
13
15
  {
16
+ iid: {
17
+ type: String,
18
+ trim: true,
19
+ },
14
20
  organizationId: { type: mongoose.Schema.Types.ObjectId, ref: "organization", default: null },
15
21
  title: { type: String, required: true },
16
22
  module: { type: String, required: true },
@@ -21,8 +27,22 @@ const CustomQuerySchema = new Schema<ICustomQuery>(
21
27
  {
22
28
  timestamps: true, // Automatically adds createdAt and updatedAt fields
23
29
  }
24
- );
25
- CustomQuerySchema.index({ module: 1, client: 1 }, { unique: true });
30
+ );
31
+ CustomQuerySchema.pre('save', async function(next) {
32
+ if (!this.iid) {
33
+ let unique = false;
34
+ while (!unique) {
35
+ const id = generateRandomIID();
36
+ const existing = await mongoose.models.customquery.findOne({ iid: id });
37
+ if (!existing) {
38
+ this.iid = id;
39
+ unique = true;
40
+ }
41
+ }
42
+ }
43
+ next();
44
+ });
45
+
26
46
 
27
47
  const CustomQuery = model<ICustomQuery>('CustomQuery', CustomQuerySchema);
28
48
 
@@ -1,20 +1,28 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IEnvironment extends Document {
5
+ iid: string;
4
6
  name: string;
5
7
  rules_js?: string;
6
8
  client?: mongoose.Schema.Types.ObjectId;
7
9
  organizationId?: mongoose.Schema.Types.ObjectId;
8
10
  isActive?: boolean;
11
+ default?: boolean;
9
12
  }
10
13
 
11
14
  const EnvironmentSchema = new Schema<IEnvironment>({
15
+ iid: {
16
+ type: String,
17
+ trim: true,
18
+ },
12
19
  name: {
13
20
  type: String,
14
21
  required: true,
15
22
  default: 'Default',
16
23
  trim: true,
17
24
  },
25
+
18
26
  client: {
19
27
  type: mongoose.Schema.Types.ObjectId,
20
28
  ref: "client",
@@ -24,6 +32,10 @@ const EnvironmentSchema = new Schema<IEnvironment>({
24
32
  type: mongoose.Schema.Types.ObjectId,
25
33
  ref: "organization",
26
34
  },
35
+ default: {
36
+ type: Boolean,
37
+ default: false,
38
+ },
27
39
  rules_js: {
28
40
  type: String,
29
41
  default: "return true",
@@ -37,6 +49,21 @@ const EnvironmentSchema = new Schema<IEnvironment>({
37
49
  timestamps: true
38
50
  });
39
51
 
52
+ EnvironmentSchema.pre('save', async function(next) {
53
+ if (!this.iid) {
54
+ let unique = false;
55
+ while (!unique) {
56
+ const id = generateRandomIID();
57
+ const existing = await mongoose.models.environment.findOne({ iid: id });
58
+ if (!existing) {
59
+ this.iid = id;
60
+ unique = true;
61
+ }
62
+ }
63
+ }
64
+ next();
65
+ });
66
+
40
67
  // Compound indexes for common query patterns
41
68
  EnvironmentSchema.index({ client: 1, name: 1 }, { unique: true });
42
69
  EnvironmentSchema.index({ client: 1, isActive: 1 });
@@ -1,8 +1,10 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
2
  import slug from "mongoose-slug-updater"
3
+ import { generateRandomIID } from "../constant";
3
4
  mongoose.plugin(slug)
4
5
 
5
6
  export interface IGoal extends Document {
7
+ iid: string;
6
8
  name: string;
7
9
  details?: string;
8
10
  slug?: string;
@@ -19,6 +21,10 @@ export interface IGoal extends Document {
19
21
  }
20
22
 
21
23
  const GoalSchema = new Schema<IGoal>({
24
+ iid: {
25
+ type: String,
26
+ trim: true,
27
+ },
22
28
  name: {
23
29
  type: String,
24
30
  required: true,
@@ -75,12 +81,26 @@ const GoalSchema = new Schema<IGoal>({
75
81
  timestamps: true
76
82
  });
77
83
 
78
- // Compound indexes for common query patterns
84
+ GoalSchema.pre('save', async function(next) {
85
+ if (!this.iid) {
86
+ let unique = false;
87
+ while (!unique) {
88
+ const id = generateRandomIID();
89
+ const existing = await mongoose.models.goal.findOne({ iid: id });
90
+ if (!existing) {
91
+ this.iid = id;
92
+ unique = true;
93
+ }
94
+ }
95
+ }
96
+ next();
97
+ });
98
+
79
99
  GoalSchema.index({ client: 1});
80
100
  GoalSchema.index({ client: 1, isActive: 1 });
81
101
  GoalSchema.index({ organizationId: 1, client: 1 });
82
102
  GoalSchema.index({ client: 1, name: 1 }, { unique: true });
83
-
103
+ GoalSchema.index({ iid: 1 }, { unique: true });
84
104
  const Goal = mongoose.models.goal || model<IGoal>("goal", GoalSchema);
85
105
 
86
106
  export default Goal;
@@ -1,14 +1,20 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
2
  import slug from "mongoose-slug-updater"
3
+ import { generateRandomIID } from "../constant";
3
4
  mongoose.plugin(slug)
4
5
 
6
+ // Helper function
7
+
8
+
5
9
  export interface IUrltargetings extends Document {
10
+ iid: string;
6
11
  type: string;
7
12
  url: string;
8
13
  url_type: string;
9
14
  }
10
15
 
11
16
  export interface IPage extends Document {
17
+ iid: string;
12
18
  name: string;
13
19
  client?: mongoose.Schema.Types.ObjectId;
14
20
  organizationId?: mongoose.Schema.Types.ObjectId;
@@ -23,6 +29,10 @@ export interface IPage extends Document {
23
29
  }
24
30
 
25
31
  const UrltargetingsSchema = new Schema<IUrltargetings>({
32
+ iid: {
33
+ type: String,
34
+ unique: true,
35
+ },
26
36
  type: {
27
37
  type: String,
28
38
  required: true,
@@ -41,6 +51,10 @@ const UrltargetingsSchema = new Schema<IUrltargetings>({
41
51
  });
42
52
 
43
53
  const PageSchema = new Schema<IPage>({
54
+ iid: {
55
+ type: String,
56
+ unique: true,
57
+ },
44
58
  name: {
45
59
  type: String,
46
60
  required: true,
@@ -98,6 +112,35 @@ const PageSchema = new Schema<IPage>({
98
112
  timestamps: true
99
113
  });
100
114
 
115
+ PageSchema.pre('save', async function(next) {
116
+ if (!this.iid) {
117
+ let unique = false;
118
+ while (!unique) {
119
+ const id = generateRandomIID();
120
+ const existing = await mongoose.models.page.findOne({ iid: id });
121
+ if (!existing) {
122
+ this.iid = id;
123
+ unique = true;
124
+ }
125
+ }
126
+ }
127
+ for (const urltargeting of this.urltargetings) {
128
+ if (!urltargeting.iid) {
129
+ let uniqueVar = false;
130
+ while (!uniqueVar) {
131
+ const id = generateRandomIID();
132
+ const existingVar = await mongoose.models.page.findOne({ "urltargetings.iid": id });
133
+ if (!existingVar) {
134
+ urltargeting.iid = id;
135
+ uniqueVar = true;
136
+ }
137
+ }
138
+ }
139
+ }
140
+ next();
141
+ });
142
+
143
+ PageSchema.index({ iid: 1 }, { unique: true });
101
144
  // Compound indexes for common query patterns
102
145
  PageSchema.index({ client: 1, name: 1 }, { unique: true });
103
146
  PageSchema.index({ client: 1, isActive: 1 });
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IReports extends Document {
5
+ iid: string;
4
6
  name: string;
5
7
  segmentName?: string;
6
8
  reportLink?: string;
@@ -23,6 +25,10 @@ export interface IReports extends Document {
23
25
 
24
26
  const ReportsSchema = new Schema<IReports>(
25
27
  {
28
+ iid: {
29
+ type: String,
30
+ trim: true,
31
+ },
26
32
  organizationId: {
27
33
  type: mongoose.Schema.Types.ObjectId,
28
34
  ref: "organization",
@@ -64,6 +70,22 @@ const ReportsSchema = new Schema<IReports>(
64
70
  }
65
71
  );
66
72
 
73
+ ReportsSchema.pre('save', async function(next) {
74
+ if (!this.iid) {
75
+ let unique = false;
76
+ while (!unique) {
77
+ const id = generateRandomIID();
78
+ const existing = await mongoose.models.report.findOne({ iid: id });
79
+ if (!existing) {
80
+ this.iid = id;
81
+ unique = true;
82
+ }
83
+ }
84
+ }
85
+ next();
86
+ });
87
+
88
+
67
89
  // Add compound indexes for better query performance
68
90
  ReportsSchema.index({ client: 1, conclusion: 1 });
69
91
  ReportsSchema.index({ test: 1, to: -1 }); // For sorting reports by date
@@ -1,8 +1,10 @@
1
1
  import mongoose, { Document, Mongoose, Schema, model } from "mongoose";
2
2
  import slug from "mongoose-slug-updater"
3
+ import { generateRandomIID } from "../constant";
3
4
  mongoose.plugin(slug)
4
5
 
5
6
  export interface IStageInCustomerJourney extends Document {
7
+ iid: string;
6
8
  name?: string;
7
9
  stages?: string;
8
10
  slug?: string;
@@ -13,6 +15,10 @@ export interface IStageInCustomerJourney extends Document {
13
15
 
14
16
  const StageInCustomerJourneySchema = new Schema<IStageInCustomerJourney>(
15
17
  {
18
+ iid: {
19
+ type: String,
20
+ unique: true,
21
+ },
16
22
  name: {
17
23
  type: String,
18
24
  required: true,
@@ -53,6 +59,22 @@ const StageInCustomerJourneySchema = new Schema<IStageInCustomerJourney>(
53
59
  }
54
60
  );
55
61
 
62
+ StageInCustomerJourneySchema.pre('save', async function(next) {
63
+ if (!this.iid) {
64
+ let unique = false;
65
+ while (!unique) {
66
+ const id = generateRandomIID();
67
+ const existing = await mongoose.models.stageincustomerjourney.findOne({ iid: id });
68
+ if (!existing) {
69
+ this.iid = id;
70
+ unique = true;
71
+ }
72
+ }
73
+ }
74
+ next();
75
+ });
76
+
77
+ StageInCustomerJourneySchema.index({ iid: 1 }, { unique: true });
56
78
  // Compound indexes for common query patterns
57
79
  // StageInCustomerJourneySchema.index({ client: 1, slug: 1 });
58
80
  const StageInCustomerJourney = mongoose.models.stageincustomerjourney || model<IStageInCustomerJourney>(
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IVariations extends Document {
5
+ iid: string;
4
6
  name: string;
5
7
  css_code: string;
6
8
  baseline: boolean;
@@ -17,6 +19,7 @@ export interface IVariations extends Document {
17
19
  export interface ITest extends Document {
18
20
  // Core test properties
19
21
  organizationId?: mongoose.Schema.Types.ObjectId;
22
+ iid: string;
20
23
  status: string;
21
24
  name: string;
22
25
  traffic_allocation: number;
@@ -76,6 +79,7 @@ export interface ITest extends Document {
76
79
  }
77
80
 
78
81
  const VariationsSchema = new Schema<IVariations>({
82
+ iid: { type: String, unique: true },
79
83
  name: { type: String, required: true },
80
84
  css_code: { type: String, default: "" },
81
85
  baseline: { type: Boolean, default: false },
@@ -91,6 +95,7 @@ const VariationsSchema = new Schema<IVariations>({
91
95
 
92
96
  const TestSchema = new Schema<ITest>(
93
97
  {
98
+ iid: { type: String, unique: true },
94
99
  organizationId: {
95
100
  type: mongoose.Schema.Types.ObjectId,
96
101
  ref: "organization",
@@ -99,7 +104,7 @@ const TestSchema = new Schema<ITest>(
99
104
  status: {
100
105
  type: String,
101
106
  default: "draft",
102
- enum: ["live", "draft", "ended", "paused", "preview", "running"]
107
+ enum: ["live", "draft", "ended", "paused", "preview", "running", "discard"]
103
108
  },
104
109
  name: { type: String, required: true },
105
110
  traffic_allocation: { type: Number, default: 100 },
@@ -160,7 +165,7 @@ const TestSchema = new Schema<ITest>(
160
165
  },
161
166
  hypothesis: {
162
167
  type: mongoose.Schema.Types.ObjectId,
163
- ref: "hypos"
168
+ ref: "hypos"
164
169
  },
165
170
  trigger: {
166
171
  type: [mongoose.Schema.Types.ObjectId],
@@ -169,7 +174,8 @@ const TestSchema = new Schema<ITest>(
169
174
  },
170
175
  stageincustomerjourney: {
171
176
  type: mongoose.Schema.Types.ObjectId,
172
- ref: "stageincustomerjourney"
177
+ ref: "stageincustomerjourney",
178
+ default: null
173
179
  },
174
180
 
175
181
  // Test configuration
@@ -202,6 +208,37 @@ const TestSchema = new Schema<ITest>(
202
208
  timestamps: true
203
209
  }
204
210
  );
211
+ TestSchema.pre('save', async function(next) {
212
+ if (!this.iid) {
213
+ let unique = false;
214
+ while (!unique) {
215
+ const id = generateRandomIID();
216
+ const existing = await mongoose.models.test.findOne({ iid: id });
217
+ if (!existing) {
218
+ this.iid = id;
219
+ unique = true;
220
+ }
221
+ }
222
+ }
223
+ // For each Variation inside Test
224
+ for (const variation of this.variations) {
225
+ if (!variation.iid) {
226
+ let uniqueVar = false;
227
+ while (!uniqueVar) {
228
+ const id = generateRandomIID();
229
+ const existingVar = await mongoose.models.test.findOne({ "variations.iid": id });
230
+ if (!existingVar) {
231
+ variation.iid = id;
232
+ uniqueVar = true;
233
+ }
234
+ }
235
+ }
236
+ }
237
+ next();
238
+ });
239
+
240
+
241
+ TestSchema.index({ iid: 1 }, { unique: true });
205
242
  // Add compound indexes for better query performance
206
243
  TestSchema.index({ client: 1, status: 1 });
207
244
  TestSchema.index({ client: 1, status: 1, livedate: 1 });