codebase-models 2.0.5 → 2.0.7

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 (72) 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 -9
  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 +3 -0
  30. package/dist/src/models/Environment.js +38 -0
  31. package/dist/src/models/Goal.d.ts +1 -0
  32. package/dist/src/models/Goal.js +31 -2
  33. package/dist/src/models/Page.d.ts +2 -0
  34. package/dist/src/models/Page.js +48 -0
  35. package/dist/src/models/PageElement.js +0 -1
  36. package/dist/src/models/PageTestType.js +0 -2
  37. package/dist/src/models/Report.d.ts +1 -0
  38. package/dist/src/models/Report.js +30 -0
  39. package/dist/src/models/Role.js +0 -3
  40. package/dist/src/models/Snippet.js +0 -4
  41. package/dist/src/models/StageInCustomerJourney.d.ts +1 -0
  42. package/dist/src/models/StageInCustomerJourney.js +31 -4
  43. package/dist/src/models/Test.d.ts +3 -0
  44. package/dist/src/models/Test.js +47 -2
  45. package/dist/src/models/TestTimeline.d.ts +39 -0
  46. package/dist/src/models/TestTimeline.js +79 -0
  47. package/index.ts +4 -1
  48. package/package.json +3 -2
  49. package/src/constant.ts +17 -0
  50. package/src/models/Announcement.ts +22 -0
  51. package/src/models/Audience.ts +23 -0
  52. package/src/models/BqPreCompiledData.ts +22 -0
  53. package/src/models/Client.ts +22 -0
  54. package/src/models/ClientAdditionalRevenue.ts +42 -1
  55. package/src/models/ClientLearning.ts +22 -0
  56. package/src/models/ClientLinks.ts +23 -2
  57. package/src/models/ClientNextSteps.ts +20 -0
  58. package/src/models/ClientNote.ts +22 -1
  59. package/src/models/ClientScript.ts +56 -21
  60. package/src/models/ClientStrategy.ts +24 -0
  61. package/src/models/CustomQuery.ts +22 -2
  62. package/src/models/Environment.ts +33 -1
  63. package/src/models/Goal.ts +22 -3
  64. package/src/models/Page.ts +43 -0
  65. package/src/models/PageElement.ts +0 -1
  66. package/src/models/PageTestType.ts +0 -2
  67. package/src/models/Report.ts +22 -0
  68. package/src/models/Role.ts +1 -3
  69. package/src/models/Snippet.ts +1 -4
  70. package/src/models/StageInCustomerJourney.ts +22 -4
  71. package/src/models/Test.ts +42 -4
  72. package/src/models/TestTimeline.ts +61 -0
@@ -22,19 +22,32 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
25
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
36
  };
28
37
  Object.defineProperty(exports, "__esModule", { value: true });
29
38
  const mongoose_1 = __importStar(require("mongoose"));
30
39
  const mongoose_slug_updater_1 = __importDefault(require("mongoose-slug-updater"));
40
+ const constant_1 = require("../constant");
31
41
  mongoose_1.default.plugin(mongoose_slug_updater_1.default);
32
42
  const StageInCustomerJourneySchema = new mongoose_1.Schema({
43
+ iid: {
44
+ type: String,
45
+ unique: true,
46
+ },
33
47
  name: {
34
48
  type: String,
35
49
  required: true,
36
50
  trim: true,
37
- index: true,
38
51
  },
39
52
  stages: {
40
53
  type: String,
@@ -52,21 +65,35 @@ const StageInCustomerJourneySchema = new mongoose_1.Schema({
52
65
  type: mongoose_1.default.Schema.Types.ObjectId,
53
66
  default: null,
54
67
  ref: "organization",
55
- index: true,
56
68
  },
57
69
  client: {
58
70
  type: mongoose_1.default.Schema.Types.ObjectId,
59
71
  ref: "client",
60
- index: true,
61
72
  },
62
73
  isActive: {
63
74
  type: Boolean,
64
75
  default: true,
65
- index: true,
66
76
  }
67
77
  }, {
68
78
  timestamps: true,
69
79
  });
80
+ StageInCustomerJourneySchema.pre('save', function (next) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (!this.iid) {
83
+ let unique = false;
84
+ while (!unique) {
85
+ const id = (0, constant_1.generateRandomIID)();
86
+ const existing = yield mongoose_1.default.models.stageincustomerjourney.findOne({ iid: id });
87
+ if (!existing) {
88
+ this.iid = id;
89
+ unique = true;
90
+ }
91
+ }
92
+ }
93
+ next();
94
+ });
95
+ });
96
+ StageInCustomerJourneySchema.index({ iid: 1 }, { unique: true });
70
97
  // Compound indexes for common query patterns
71
98
  // StageInCustomerJourneySchema.index({ client: 1, slug: 1 });
72
99
  const StageInCustomerJourney = mongoose_1.default.models.stageincustomerjourney || (0, mongoose_1.model)("stageincustomerjourney", StageInCustomerJourneySchema);
@@ -24,6 +24,7 @@
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
25
  import mongoose, { Document } from "mongoose";
26
26
  export interface IVariations extends Document {
27
+ iid: string;
27
28
  name: string;
28
29
  css_code: string;
29
30
  baseline: boolean;
@@ -38,6 +39,7 @@ export interface IVariations extends Document {
38
39
  }
39
40
  export interface ITest extends Document {
40
41
  organizationId?: mongoose.Schema.Types.ObjectId;
42
+ iid: string;
41
43
  status: string;
42
44
  name: string;
43
45
  traffic_allocation: number;
@@ -76,6 +78,7 @@ export interface ITest extends Document {
76
78
  testid: string;
77
79
  testtool: string;
78
80
  personalization: boolean;
81
+ qaMode: boolean;
79
82
  kameleoontestdetails: mongoose.Schema.Types.Mixed;
80
83
  vwotestdetails: mongoose.Schema.Types.Mixed;
81
84
  triggerevents: any[];
@@ -22,9 +22,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
25
34
  Object.defineProperty(exports, "__esModule", { value: true });
26
35
  const mongoose_1 = __importStar(require("mongoose"));
36
+ const constant_1 = require("../constant");
27
37
  const VariationsSchema = new mongoose_1.Schema({
38
+ iid: { type: String, unique: true },
28
39
  name: { type: String, required: true },
29
40
  css_code: { type: String, default: "" },
30
41
  baseline: { type: Boolean, default: false },
@@ -38,6 +49,7 @@ const VariationsSchema = new mongoose_1.Schema({
38
49
  changesets: { type: String, default: "[]" }
39
50
  });
40
51
  const TestSchema = new mongoose_1.Schema({
52
+ iid: { type: String, unique: true },
41
53
  organizationId: {
42
54
  type: mongoose_1.default.Schema.Types.ObjectId,
43
55
  ref: "organization",
@@ -46,7 +58,7 @@ const TestSchema = new mongoose_1.Schema({
46
58
  status: {
47
59
  type: String,
48
60
  default: "draft",
49
- enum: ["live", "draft", "ended", "paused", "preview", "running"]
61
+ enum: ["live", "draft", "ended", "paused", "preview", "running", "discard"]
50
62
  },
51
63
  name: { type: String, required: true },
52
64
  traffic_allocation: { type: Number, default: 100 },
@@ -113,7 +125,8 @@ const TestSchema = new mongoose_1.Schema({
113
125
  },
114
126
  stageincustomerjourney: {
115
127
  type: mongoose_1.default.Schema.Types.ObjectId,
116
- ref: "stageincustomerjourney"
128
+ ref: "stageincustomerjourney",
129
+ default: null
117
130
  },
118
131
  // Test configuration
119
132
  property: { type: mongoose_1.default.Schema.Types.Mixed, default: null },
@@ -130,6 +143,7 @@ const TestSchema = new mongoose_1.Schema({
130
143
  testid: { type: String, default: null },
131
144
  testtool: { type: String, default: "" },
132
145
  personalization: { type: Boolean, default: false },
146
+ qaMode: { type: Boolean, default: false },
133
147
  kameleoontestdetails: { type: mongoose_1.default.Schema.Types.Mixed, default: null },
134
148
  vwotestdetails: { type: mongoose_1.default.Schema.Types.Mixed, default: null },
135
149
  triggerevents: { type: [], default: [] },
@@ -143,6 +157,37 @@ const TestSchema = new mongoose_1.Schema({
143
157
  }, {
144
158
  timestamps: true
145
159
  });
160
+ TestSchema.pre('save', function (next) {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ if (!this.iid) {
163
+ let unique = false;
164
+ while (!unique) {
165
+ const id = (0, constant_1.generateRandomIID)();
166
+ const existing = yield mongoose_1.default.models.test.findOne({ iid: id });
167
+ if (!existing) {
168
+ this.iid = id;
169
+ unique = true;
170
+ }
171
+ }
172
+ }
173
+ // For each Variation inside Test
174
+ for (const variation of this.variations) {
175
+ if (!variation.iid) {
176
+ let uniqueVar = false;
177
+ while (!uniqueVar) {
178
+ const id = (0, constant_1.generateRandomIID)();
179
+ const existingVar = yield mongoose_1.default.models.test.findOne({ "variations.iid": id });
180
+ if (!existingVar) {
181
+ variation.iid = id;
182
+ uniqueVar = true;
183
+ }
184
+ }
185
+ }
186
+ }
187
+ next();
188
+ });
189
+ });
190
+ TestSchema.index({ iid: 1 }, { unique: true });
146
191
  // Add compound indexes for better query performance
147
192
  TestSchema.index({ client: 1, status: 1 });
148
193
  TestSchema.index({ client: 1, status: 1, livedate: 1 });
@@ -0,0 +1,39 @@
1
+ /// <reference types="mongoose/types/aggregate" />
2
+ /// <reference types="mongoose/types/callback" />
3
+ /// <reference types="mongoose/types/collection" />
4
+ /// <reference types="mongoose/types/connection" />
5
+ /// <reference types="mongoose/types/cursor" />
6
+ /// <reference types="mongoose/types/document" />
7
+ /// <reference types="mongoose/types/error" />
8
+ /// <reference types="mongoose/types/expressions" />
9
+ /// <reference types="mongoose/types/helpers" />
10
+ /// <reference types="mongoose/types/middlewares" />
11
+ /// <reference types="mongoose/types/indexes" />
12
+ /// <reference types="mongoose/types/models" />
13
+ /// <reference types="mongoose/types/mongooseoptions" />
14
+ /// <reference types="mongoose/types/pipelinestage" />
15
+ /// <reference types="mongoose/types/populate" />
16
+ /// <reference types="mongoose/types/query" />
17
+ /// <reference types="mongoose/types/schemaoptions" />
18
+ /// <reference types="mongoose/types/schematypes" />
19
+ /// <reference types="mongoose/types/session" />
20
+ /// <reference types="mongoose/types/types" />
21
+ /// <reference types="mongoose/types/utility" />
22
+ /// <reference types="mongoose/types/validation" />
23
+ /// <reference types="mongoose/types/virtuals" />
24
+ /// <reference types="mongoose/types/inferschematype" />
25
+ import mongoose, { Document } from "mongoose";
26
+ export interface ITestTimeline extends Document {
27
+ iid: string;
28
+ organizationId?: mongoose.Schema.Types.ObjectId;
29
+ test: mongoose.Schema.Types.ObjectId;
30
+ event: string;
31
+ client: mongoose.Schema.Types.ObjectId;
32
+ user: mongoose.Schema.Types.ObjectId;
33
+ changes: {
34
+ type: mongoose.Schema.Types.Mixed;
35
+ default: {};
36
+ };
37
+ }
38
+ declare const TestTimeline: mongoose.Model<any, {}, {}, {}, any, any>;
39
+ export default TestTimeline;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const mongoose_1 = __importStar(require("mongoose"));
36
+ const constant_1 = require("../constant");
37
+ const TestTimelineSchema = new mongoose_1.Schema({
38
+ iid: {
39
+ type: String,
40
+ unique: true,
41
+ },
42
+ event: {
43
+ type: String,
44
+ enum: ["test_created", "test_updated", "test_status_changed", "test_deleted", "variant_created", "variant_updated", "variant_deleted"],
45
+ },
46
+ organizationId: {
47
+ type: mongoose_1.default.Schema.Types.ObjectId,
48
+ ref: "organization",
49
+ default: null,
50
+ },
51
+ client: {
52
+ type: mongoose_1.default.Schema.Types.ObjectId,
53
+ ref: "client",
54
+ },
55
+ test: {
56
+ type: mongoose_1.default.Schema.Types.ObjectId,
57
+ ref: "test",
58
+ },
59
+ user: {
60
+ type: mongoose_1.default.Schema.Types.ObjectId,
61
+ ref: "user",
62
+ },
63
+ changes: {
64
+ type: mongoose_1.default.Schema.Types.Mixed,
65
+ default: {},
66
+ },
67
+ }, {
68
+ timestamps: true,
69
+ });
70
+ TestTimelineSchema.pre("save", function (next) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ if (!this.iid) {
73
+ this.iid = (0, constant_1.generateRandomIID)();
74
+ }
75
+ next();
76
+ });
77
+ });
78
+ const TestTimeline = mongoose_1.default.models.testTimeline || (0, mongoose_1.model)("testTimeline", TestTimelineSchema);
79
+ exports.default = TestTimeline;
package/index.ts CHANGED
@@ -48,7 +48,8 @@ import Environment from "./src/models/Environment";
48
48
  import PrecalculationFilters from "./src/models/PrecalculationFilters";
49
49
  import BqPreCompiledData from "./src/models/BqPreCompiledData";
50
50
  import CustomQuery from "./src/models/CustomQuery";
51
-
51
+ import { generateRandomIID } from "./src/constant";
52
+ import TestTimeline from "./src/models/TestTimeline";
52
53
  async function seedRoles() {
53
54
  const checkRoles = await Role.countDocuments();
54
55
  if (checkRoles < 5) {
@@ -97,6 +98,7 @@ export async function connect(
97
98
  }
98
99
 
99
100
  export {
101
+ generateRandomIID,
100
102
  Client,
101
103
  ClientReportsTemp,
102
104
  QueryLog,
@@ -131,6 +133,7 @@ export {
131
133
  Role,
132
134
  CustomQuery,
133
135
  SavedSegment,
136
+ TestTimeline,
134
137
  Segment,
135
138
  SegmentCombination,
136
139
  Snippet,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codebase-models",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "Common models for codebase",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -13,7 +13,8 @@
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
15
  "mongoose": "^8.2.3",
16
- "mongoose-slug-updater": "^3.3.0"
16
+ "mongoose-slug-updater": "^3.3.0",
17
+ "uuid": "^11.1.0"
17
18
  },
18
19
  "devDependencies": {
19
20
  "@types/mongoose": "^5.11.97",
package/src/constant.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+
1
3
  const HypothesisSheetConstants = {
2
4
  NO: 0,
3
5
  YES: 1,
@@ -11,4 +13,19 @@ const HypothesisSheetConstants = {
11
13
  GOOD: 2,
12
14
  };
13
15
 
16
+ /**
17
+ * Generates a random unique identifier
18
+ * @returns A 12-character uppercase string (increased from 8 for better uniqueness)
19
+ * @throws Error if UUID generation fails
20
+ */
21
+ const generateRandomIID = (): string => {
22
+ try {
23
+ // Take first 12 characters instead of 8 for better uniqueness
24
+ return uuidv4().replace(/-/g, '').substring(0, 12).toUpperCase();
25
+ } catch (error) {
26
+ throw new Error(`Failed to generate random IID: ${error instanceof Error ? error.message : 'Unknown error'}`);
27
+ }
28
+ };
29
+
30
+ export { generateRandomIID };
14
31
  export default HypothesisSheetConstants;
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IAnnouncement extends Document {
5
+ iid: string;
4
6
  title: string;
5
7
  description?: string;
6
8
  active?: boolean;
@@ -12,6 +14,10 @@ export interface IAnnouncement extends Document {
12
14
 
13
15
  const AnnouncementSchema = new Schema<IAnnouncement>(
14
16
  {
17
+ iid: {
18
+ type: String,
19
+ trim: true,
20
+ },
15
21
  organizationId: {
16
22
  type: mongoose.Schema.Types.ObjectId,
17
23
  ref: "organization",
@@ -49,6 +55,22 @@ const AnnouncementSchema = new Schema<IAnnouncement>(
49
55
  }
50
56
  );
51
57
 
58
+ AnnouncementSchema.pre('save', async function(next) {
59
+ if (!this.iid) {
60
+ let unique = false;
61
+ while (!unique) {
62
+ const id = generateRandomIID();
63
+ const existing = await mongoose.models.announcement.findOne({ iid: id });
64
+ if (!existing) {
65
+ this.iid = id;
66
+ unique = true;
67
+ }
68
+ }
69
+ }
70
+ next();
71
+ });
72
+
73
+ AnnouncementSchema.index({ iid: 1 }, { unique: true });
52
74
  // Add compound indexes for better query performance
53
75
  AnnouncementSchema.index({ active: 1, createdAt: -1 }); // For active announcements sorted by date
54
76
  AnnouncementSchema.index({ forRoles: 1, active: 1 }); // For role-based announcements
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IAudience extends Document {
5
+ iid: string;
4
6
  organizationId?: mongoose.Schema.Types.ObjectId;
5
7
  name: string;
6
8
  rules_js?: string;
@@ -9,6 +11,10 @@ export interface IAudience extends Document {
9
11
 
10
12
  const AudienceSchema = new Schema<IAudience>(
11
13
  {
14
+ iid: {
15
+ type: String,
16
+ trim: true,
17
+ },
12
18
  organizationId: {
13
19
  type: mongoose.Schema.Types.ObjectId,
14
20
  ref: "organization",
@@ -31,6 +37,23 @@ const AudienceSchema = new Schema<IAudience>(
31
37
  }
32
38
  );
33
39
 
40
+ AudienceSchema.pre('save', async function(next) {
41
+ if (!this.iid) {
42
+ let unique = false;
43
+ while (!unique) {
44
+ const id = generateRandomIID();
45
+ const existing = await mongoose.models.audience.findOne({ iid: id });
46
+ if (!existing) {
47
+ this.iid = id;
48
+ unique = true;
49
+ }
50
+ }
51
+ }
52
+ next();
53
+ });
54
+
55
+ AudienceSchema.index({ iid: 1 }, { unique: true });
56
+
34
57
  const Audience = mongoose.models.audience || model<IAudience>("audience", AudienceSchema);
35
58
 
36
59
  export default Audience;
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IBqPreCompiledData extends Document {
5
+ iid: string;
4
6
  title: string;
5
7
  organizationId: mongoose.Schema.Types.ObjectId;
6
8
  client: mongoose.Schema.Types.ObjectId;
@@ -13,6 +15,10 @@ export interface IBqPreCompiledData extends Document {
13
15
  }
14
16
 
15
17
  const BqPreCompiledDataSchema = new Schema<IBqPreCompiledData>({
18
+ iid: {
19
+ type: String,
20
+ trim: true,
21
+ },
16
22
  title: {
17
23
  type: String,
18
24
  default: null,
@@ -56,6 +62,22 @@ const BqPreCompiledDataSchema = new Schema<IBqPreCompiledData>({
56
62
  timestamps: true
57
63
  });
58
64
 
65
+ BqPreCompiledDataSchema.pre('save', async function(next) {
66
+ if (!this.iid) {
67
+ let unique = false;
68
+ while (!unique) {
69
+ const id = generateRandomIID();
70
+ const existing = await mongoose.models.bqprecompileddata.findOne({ iid: id });
71
+ if (!existing) {
72
+ this.iid = id;
73
+ unique = true;
74
+ }
75
+ }
76
+ }
77
+ next();
78
+ });
79
+
80
+ BqPreCompiledDataSchema.index({ iid: 1 }, { unique: true });
59
81
  BqPreCompiledDataSchema.index({ organizationId: 1, client: 1 });
60
82
  const BqPreCompiledData = mongoose.models.bqprecompileddata || model<IBqPreCompiledData>("bqprecompileddata", BqPreCompiledDataSchema);
61
83
 
@@ -1,5 +1,6 @@
1
1
  import mongoose, { Schema, Document, model } from "mongoose";
2
2
  import slug from "mongoose-slug-updater";
3
+ import { generateRandomIID } from "../constant";
3
4
 
4
5
  mongoose.plugin(slug);
5
6
 
@@ -16,6 +17,7 @@ const PropertySchema: Schema = new Schema<IProperty>({
16
17
  });
17
18
 
18
19
  export interface IClient extends Document {
20
+ iid: string;
19
21
  organizationId?: mongoose.Schema.Types.ObjectId;
20
22
  name: string;
21
23
  slug?: string;
@@ -37,6 +39,10 @@ export interface IClient extends Document {
37
39
 
38
40
  const ClientSchema: Schema = new Schema<IClient>(
39
41
  {
42
+ iid: {
43
+ type: String,
44
+ trim: true,
45
+ },
40
46
  organizationId: { type: mongoose.Schema.Types.ObjectId,
41
47
  ref: "organization",
42
48
  default: null },
@@ -75,6 +81,22 @@ const ClientSchema: Schema = new Schema<IClient>(
75
81
  }
76
82
  );
77
83
 
84
+ ClientSchema.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.client.findOne({ iid: id });
90
+ if (!existing) {
91
+ this.iid = id;
92
+ unique = true;
93
+ }
94
+ }
95
+ }
96
+ next();
97
+ });
98
+
99
+ ClientSchema.index({ iid: 1 }, { unique: true });
78
100
  // Add compound indexes for better query performance
79
101
  ClientSchema.index({ active: 1 });
80
102
  ClientSchema.index({ organizationId: 1, active: 1 });
@@ -1,13 +1,16 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IClientAdditionalRevenue extends Document {
5
+ iid: string;
4
6
  organizationId?: mongoose.Schema.Types.ObjectId;
5
7
  client?: mongoose.Schema.Types.ObjectId;
6
- additionalRevenueForTests: IAdditionalRevenue;
8
+ additionalRevenueForTests: IAdditionalRevenue[];
7
9
  createdBy?: mongoose.Schema.Types.ObjectId;
8
10
  }
9
11
 
10
12
  export interface IAdditionalRevenue extends Document {
13
+ iid: string;
11
14
  segmentName: string;
12
15
  lastDate?: Date;
13
16
  test?: mongoose.Schema.Types.ObjectId;
@@ -15,6 +18,10 @@ export interface IAdditionalRevenue extends Document {
15
18
  }
16
19
 
17
20
  const AdditionalRevenueSchema = new Schema<IAdditionalRevenue>({
21
+ iid: {
22
+ type: String,
23
+ trim: true,
24
+ },
18
25
  // add_rev: {
19
26
  // type: Number,
20
27
  // },
@@ -34,8 +41,13 @@ const AdditionalRevenueSchema = new Schema<IAdditionalRevenue>({
34
41
  },
35
42
  });
36
43
 
44
+
37
45
  const ClientAdditionalRevenueSchema = new Schema<IClientAdditionalRevenue>(
38
46
  {
47
+ iid: {
48
+ type: String,
49
+ trim: true,
50
+ },
39
51
  organizationId: {
40
52
  type: mongoose.Schema.Types.ObjectId,
41
53
  ref: "organization",
@@ -57,6 +69,35 @@ const ClientAdditionalRevenueSchema = new Schema<IClientAdditionalRevenue>(
57
69
  }
58
70
  );
59
71
 
72
+ ClientAdditionalRevenueSchema.pre('save', async function(next) {
73
+ if (!this.iid) {
74
+ let unique = false;
75
+ while (!unique) {
76
+ const id = generateRandomIID();
77
+ const existing = await mongoose.models.clientadditionalrevenue.findOne({ iid: id });
78
+ if (!existing) {
79
+ this.iid = id;
80
+ unique = true;
81
+ }
82
+ }
83
+ }
84
+ // For each Variation inside Test
85
+ for (const additionalRevenue of this.additionalRevenueForTests) {
86
+ if (!additionalRevenue.iid) {
87
+ let uniqueVar = false;
88
+ while (!uniqueVar) {
89
+ const id = generateRandomIID();
90
+ const existingVar = await mongoose.models.clientadditionalrevenue.findOne({ "additionalRevenueForTests.iid": id });
91
+ if (!existingVar) {
92
+ additionalRevenue.iid = id;
93
+ uniqueVar = true;
94
+ }
95
+ }
96
+ }
97
+ }
98
+ next();
99
+ });
100
+
60
101
  ClientAdditionalRevenueSchema.index({
61
102
  client: 1,
62
103
  });
@@ -1,6 +1,8 @@
1
1
  import mongoose, { Document, Schema, model } from "mongoose";
2
+ import { generateRandomIID } from "../constant";
2
3
 
3
4
  export interface IClientLearning extends Document {
5
+ iid: string;
4
6
  name: string;
5
7
  organizationId?: mongoose.Schema.Types.ObjectId;
6
8
  client?: mongoose.Schema.Types.ObjectId;
@@ -10,6 +12,10 @@ export interface IClientLearning extends Document {
10
12
 
11
13
  const ClientLearningSchema = new Schema<IClientLearning>(
12
14
  {
15
+ iid: {
16
+ type: String,
17
+ trim: true,
18
+ },
13
19
  name: {
14
20
  type: String,
15
21
  required: true,
@@ -37,6 +43,22 @@ const ClientLearningSchema = new Schema<IClientLearning>(
37
43
  }
38
44
  );
39
45
 
46
+ ClientLearningSchema.pre('save', async function(next) {
47
+ if (!this.iid) {
48
+ let unique = false;
49
+ while (!unique) {
50
+ const id = generateRandomIID();
51
+ const existing = await mongoose.models.clientlearning.findOne({ iid: id });
52
+ if (!existing) {
53
+ this.iid = id;
54
+ unique = true;
55
+ }
56
+ }
57
+ }
58
+ next();
59
+ });
60
+
61
+ ClientLearningSchema.index({ iid: 1 }, { unique: true });
40
62
  // Compound indexes for common query patterns
41
63
  ClientLearningSchema.index({ client: 1 });
42
64
  // Sparse indexes for optional fields