@naturalcycles/abba 1.11.0 → 1.12.0

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.
package/dist/abba.d.ts CHANGED
@@ -54,10 +54,10 @@ export declare class Abba {
54
54
  getAllExistingUserAssignments(userId: string): Promise<Saved<UserAssignment>[]>;
55
55
  /**
56
56
  * Generate user assignments for all active experiments.
57
- * Will return any existing and attempt to generate any new assignments.
57
+ * Will return any existing and attempt to generate any new assignments if existingOnly is false.
58
58
  * Hot method.
59
59
  */
60
- generateUserAssignments(userId: string, segmentationData: SegmentationData): Promise<GeneratedUserAssignment[]>;
60
+ generateUserAssignments(userId: string, segmentationData: SegmentationData, existingOnly?: boolean): Promise<GeneratedUserAssignment[]>;
61
61
  /**
62
62
  * Get assignment statistics for an experiment.
63
63
  * Cold method.
package/dist/abba.js CHANGED
@@ -133,33 +133,35 @@ class Abba {
133
133
  }
134
134
  /**
135
135
  * Generate user assignments for all active experiments.
136
- * Will return any existing and attempt to generate any new assignments.
136
+ * Will return any existing and attempt to generate any new assignments if existingOnly is false.
137
137
  * Hot method.
138
138
  */
139
- async generateUserAssignments(userId, segmentationData) {
139
+ async generateUserAssignments(userId, segmentationData, existingOnly = false) {
140
140
  const experiments = await this.getActiveExperiments(); // cached
141
- const existingAssignments = await this.getAllExistingUserAssignments(userId);
142
- const newAssignments = [];
143
- for (const experiment of experiments) {
144
- const existing = existingAssignments.find(ua => ua.experimentId === experiment.id);
145
- if (!existing) {
146
- const assignment = this.generateUserAssignmentData(experiment, userId, segmentationData);
147
- if (assignment) {
148
- newAssignments.push(assignment);
149
- }
150
- }
151
- }
152
- existingAssignments.push(...(await this.userAssignmentDao.saveBatch(newAssignments)));
153
141
  const assignments = [];
142
+ const newAssignments = [];
143
+ const existingAssignments = await this.getAllExistingUserAssignments(userId);
154
144
  for (const experiment of experiments) {
155
145
  const existing = existingAssignments.find(ua => ua.experimentId === experiment.id);
156
146
  if (existing) {
157
147
  assignments.push({
158
148
  ...existing,
159
- bucketKey: experiment.buckets.find(i => i.id === existing.bucketId)?.key || null,
149
+ bucketKey: experiment.buckets.find(b => b.id === existing.bucketId)?.key || null,
160
150
  });
161
151
  }
152
+ else if (!existingOnly) {
153
+ const assignment = this.generateUserAssignmentData(experiment, userId, segmentationData);
154
+ if (assignment) {
155
+ const created = this.userAssignmentDao.create(assignment);
156
+ newAssignments.push(created);
157
+ assignments.push({
158
+ ...created,
159
+ bucketKey: experiment.buckets.find(b => b.id === created.bucketId)?.key || null,
160
+ });
161
+ }
162
+ }
162
163
  }
164
+ await this.userAssignmentDao.saveBatch(newAssignments);
163
165
  return assignments;
164
166
  }
165
167
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/abba",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build": "build",
package/src/abba.ts CHANGED
@@ -193,39 +193,41 @@ export class Abba {
193
193
 
194
194
  /**
195
195
  * Generate user assignments for all active experiments.
196
- * Will return any existing and attempt to generate any new assignments.
196
+ * Will return any existing and attempt to generate any new assignments if existingOnly is false.
197
197
  * Hot method.
198
198
  */
199
199
  async generateUserAssignments(
200
200
  userId: string,
201
201
  segmentationData: SegmentationData,
202
+ existingOnly = false,
202
203
  ): Promise<GeneratedUserAssignment[]> {
203
204
  const experiments = await this.getActiveExperiments() // cached
204
- const existingAssignments = await this.getAllExistingUserAssignments(userId)
205
205
 
206
+ const assignments: GeneratedUserAssignment[] = []
206
207
  const newAssignments: UserAssignment[] = []
207
- for (const experiment of experiments) {
208
- const existing = existingAssignments.find(ua => ua.experimentId === experiment.id)
209
- if (!existing) {
210
- const assignment = this.generateUserAssignmentData(experiment, userId, segmentationData)
211
- if (assignment) {
212
- newAssignments.push(assignment)
213
- }
214
- }
215
- }
216
-
217
- existingAssignments.push(...(await this.userAssignmentDao.saveBatch(newAssignments)))
208
+ const existingAssignments = await this.getAllExistingUserAssignments(userId)
218
209
 
219
- const assignments: GeneratedUserAssignment[] = []
220
210
  for (const experiment of experiments) {
221
211
  const existing = existingAssignments.find(ua => ua.experimentId === experiment.id)
222
212
  if (existing) {
223
213
  assignments.push({
224
214
  ...existing,
225
- bucketKey: experiment.buckets.find(i => i.id === existing.bucketId)?.key || null,
215
+ bucketKey: experiment.buckets.find(b => b.id === existing.bucketId)?.key || null,
226
216
  })
217
+ } else if (!existingOnly) {
218
+ const assignment = this.generateUserAssignmentData(experiment, userId, segmentationData)
219
+ if (assignment) {
220
+ const created = this.userAssignmentDao.create(assignment)
221
+ newAssignments.push(created)
222
+ assignments.push({
223
+ ...created,
224
+ bucketKey: experiment.buckets.find(b => b.id === created.bucketId)?.key || null,
225
+ })
226
+ }
227
227
  }
228
228
  }
229
+
230
+ await this.userAssignmentDao.saveBatch(newAssignments)
229
231
  return assignments
230
232
  }
231
233