@naturalcycles/abba 2.1.1 → 2.3.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
@@ -18,9 +18,10 @@ export declare class Abba {
18
18
  getAllExperimentsWithBucketsNoCache(opts?: GetAllExperimentsOpts): Promise<ExperimentWithBuckets[]>;
19
19
  getUserExperiments(userId: string): Promise<UserExperiment[]>;
20
20
  /**
21
- * Updates all user assignments with a given userId with the provided userId.
21
+ * Changes all user assignments from one userId to another, as long as no
22
+ * assignment for a given experiment already exists with the new userId.
22
23
  */
23
- updateUserId(oldId: string, newId: string): Promise<void>;
24
+ mergeAssignmentsForUserIds(fromUserId: string, intoUserId: string): Promise<void>;
24
25
  /**
25
26
  * Creates a new experiment.
26
27
  * Cold method.
package/dist/abba.js CHANGED
@@ -58,11 +58,17 @@ export class Abba {
58
58
  });
59
59
  }
60
60
  /**
61
- * Updates all user assignments with a given userId with the provided userId.
61
+ * Changes all user assignments from one userId to another, as long as no
62
+ * assignment for a given experiment already exists with the new userId.
62
63
  */
63
- async updateUserId(oldId, newId) {
64
- const query = this.userAssignmentDao.query().filterEq('userId', oldId);
65
- await this.userAssignmentDao.patchByQuery(query, { userId: newId });
64
+ async mergeAssignmentsForUserIds(fromUserId, intoUserId) {
65
+ const fromAssignments = await this.userAssignmentDao.getBy('userId', fromUserId);
66
+ const existingIntoAssignments = await this.userAssignmentDao.getBy('userId', intoUserId);
67
+ await pMap(fromAssignments, async (from) => {
68
+ if (!existingIntoAssignments.some(into => into.experimentId === from.experimentId)) {
69
+ await this.userAssignmentDao.patch(from, { userId: intoUserId });
70
+ }
71
+ });
66
72
  }
67
73
  /**
68
74
  * Creates a new experiment.
@@ -121,7 +127,7 @@ export class Abba {
121
127
  await this.experimentDao.saveBatch(requiresUpdating, { saveMethod: 'update' });
122
128
  }
123
129
  async softDeleteExperiment(experimentId) {
124
- await this.experimentDao.patchById(experimentId, { deleted: true, exclusions: [] }, { saveMethod: 'update' });
130
+ await this.experimentDao.patchById(experimentId, { deleted: true, status: AssignmentStatus.Inactive, exclusions: [] }, { saveMethod: 'update' });
125
131
  await this.updateExclusions(experimentId, []);
126
132
  }
127
133
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/abba",
3
3
  "type": "module",
4
- "version": "2.1.1",
4
+ "version": "2.3.0",
5
5
  "scripts": {
6
6
  "prepare": "husky",
7
7
  "build": "dev-lib build",
package/src/abba.ts CHANGED
@@ -94,11 +94,18 @@ export class Abba {
94
94
  }
95
95
 
96
96
  /**
97
- * Updates all user assignments with a given userId with the provided userId.
97
+ * Changes all user assignments from one userId to another, as long as no
98
+ * assignment for a given experiment already exists with the new userId.
98
99
  */
99
- async updateUserId(oldId: string, newId: string): Promise<void> {
100
- const query = this.userAssignmentDao.query().filterEq('userId', oldId)
101
- await this.userAssignmentDao.patchByQuery(query, { userId: newId })
100
+ async mergeAssignmentsForUserIds(fromUserId: string, intoUserId: string): Promise<void> {
101
+ const fromAssignments = await this.userAssignmentDao.getBy('userId', fromUserId)
102
+ const existingIntoAssignments = await this.userAssignmentDao.getBy('userId', intoUserId)
103
+
104
+ await pMap(fromAssignments, async from => {
105
+ if (!existingIntoAssignments.some(into => into.experimentId === from.experimentId)) {
106
+ await this.userAssignmentDao.patch(from, { userId: intoUserId })
107
+ }
108
+ })
102
109
  }
103
110
 
104
111
  /**
@@ -187,7 +194,7 @@ export class Abba {
187
194
  async softDeleteExperiment(experimentId: string): Promise<void> {
188
195
  await this.experimentDao.patchById(
189
196
  experimentId,
190
- { deleted: true, exclusions: [] },
197
+ { deleted: true, status: AssignmentStatus.Inactive, exclusions: [] },
191
198
  { saveMethod: 'update' },
192
199
  )
193
200
  await this.updateExclusions(experimentId, [])