Skip to content

Commit 6611a79

Browse files
authored
BC-11491 do not update pseudonyms when already existing (#6208)
1 parent 7a7a38a commit 6611a79

File tree

4 files changed

+19
-40
lines changed

4 files changed

+19
-40
lines changed

apps/server/src/modules/pseudonym/repo/external-tool-pseudonym.repo.integration.spec.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ describe('ExternalToolPseudonymRepo', () => {
151151
it('should create a new pseudonym if it does not exist', async () => {
152152
const { domainObject } = setup();
153153

154-
const result: Pseudonym = await repo.createOrUpdate(domainObject);
154+
const result: Pseudonym = await repo.createOrUpdate(domainObject.userId, domainObject.toolId);
155155

156156
expect(result.id).toBeTruthy();
157-
expect(result.pseudonym).toEqual(domainObject.pseudonym);
157+
expect(result.pseudonym).toEqual(expect.any(String));
158158
expect(result.toolId).toEqual(domainObject.toolId);
159159
expect(result.userId).toEqual(domainObject.userId);
160160
});
@@ -177,23 +177,13 @@ describe('ExternalToolPseudonymRepo', () => {
177177
};
178178
};
179179

180-
it('should update the pseudonym value of the existing record', async () => {
180+
it('should not change createdAt and pseudonym on update', async () => {
181181
const { entity, domainObject } = await setup();
182182

183-
const result: Pseudonym = await repo.createOrUpdate(domainObject);
184-
185-
expect(result.id).toEqual(entity.id);
186-
expect(result.pseudonym).toEqual(domainObject.pseudonym);
187-
expect(result.toolId).toEqual(domainObject.toolId);
188-
expect(result.userId).toEqual(domainObject.userId);
189-
});
190-
191-
it('should not change createdAt on update', async () => {
192-
const { entity, domainObject } = await setup();
193-
194-
const result: Pseudonym = await repo.createOrUpdate(domainObject);
183+
const result: Pseudonym = await repo.createOrUpdate(domainObject.userId, domainObject.toolId);
195184

196185
expect(result.createdAt).toEqual(entity.createdAt);
186+
expect(result.pseudonym).toEqual(entity.pseudonym);
197187
});
198188
});
199189

@@ -221,14 +211,20 @@ describe('ExternalToolPseudonymRepo', () => {
221211
const { domainObject1, domainObject2 } = setup();
222212

223213
await expect(
224-
Promise.all([repo.createOrUpdate(domainObject1), repo.createOrUpdate(domainObject2)])
214+
Promise.all([
215+
repo.createOrUpdate(domainObject1.userId, domainObject1.toolId),
216+
repo.createOrUpdate(domainObject2.userId, domainObject2.toolId),
217+
])
225218
).resolves.not.toThrow();
226219
});
227220

228221
it('should result in exactly one document in the database', async () => {
229222
const { domainObject1, domainObject2, sharedUserId, sharedToolId } = setup();
230223

231-
await Promise.all([repo.createOrUpdate(domainObject1), repo.createOrUpdate(domainObject2)]);
224+
await Promise.all([
225+
repo.createOrUpdate(domainObject1.userId, domainObject1.toolId),
226+
repo.createOrUpdate(domainObject2.userId, domainObject2.toolId),
227+
]);
232228

233229
const count = await em.count(ExternalToolPseudonymEntity, {
234230
userId: new ObjectId(sharedUserId),

apps/server/src/modules/pseudonym/repo/external-tool-pseudonym.repo.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PseudonymSearchQuery } from '../domain';
88
import { ExternalToolPseudonymEntity } from '../entity';
99
import { PseudonymScope } from '../entity/pseudonym.scope';
1010
import { Pseudonym } from './pseudonym.do';
11+
import { v4 as uuidv4 } from 'uuid';
1112

1213
@Injectable()
1314
export class ExternalToolPseudonymRepo {
@@ -33,22 +34,20 @@ export class ExternalToolPseudonymRepo {
3334
return pseudonyms;
3435
}
3536

36-
public async createOrUpdate(domainObject: Pseudonym): Promise<Pseudonym> {
37+
public async createOrUpdate(userId: EntityId, toolId: EntityId): Promise<Pseudonym> {
3738
const collection = this.em.getCollection(ExternalToolPseudonymEntity);
3839

39-
const userId = new ObjectId(domainObject.userId);
40-
const toolId = new ObjectId(domainObject.toolId);
4140
const now = new Date();
4241

4342
const result = await collection.findOneAndUpdate(
44-
{ userId, toolId },
43+
{ userId: new ObjectId(userId), toolId: new ObjectId(toolId) },
4544
{
4645
$set: {
47-
pseudonym: domainObject.pseudonym,
4846
updatedAt: now,
4947
},
5048
$setOnInsert: {
5149
createdAt: now,
50+
pseudonym: uuidv4(),
5251
},
5352
},
5453
{

apps/server/src/modules/pseudonym/service/pseudonym.service.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ describe('PseudonymService', () => {
188188

189189
await service.findOrCreatePseudonym(user, externalTool);
190190

191-
expect(externalToolPseudonymRepo.createOrUpdate).toHaveBeenCalledWith(
192-
expect.objectContaining({
193-
userId: user.id,
194-
toolId: externalTool.id,
195-
})
196-
);
191+
expect(externalToolPseudonymRepo.createOrUpdate).toHaveBeenCalledWith(user.id, externalTool.id);
197192
});
198193

199194
it('should return the pseudonym', async () => {

apps/server/src/modules/pseudonym/service/pseudonym.service.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Logger } from '@core/logger';
2-
import { ObjectId } from '@mikro-orm/mongodb';
32
import { ExternalTool } from '@modules/tool/external-tool/domain';
43
import { UserDo } from '@modules/user';
54
import { Inject, Injectable, InternalServerErrorException } from '@nestjs/common';
65
import { Page } from '@shared/domain/domainobject';
76
import { IFindOptions } from '@shared/domain/interface';
8-
import { v4 as uuidv4 } from 'uuid';
97
import { PseudonymSearchQuery } from '../domain';
108
import { PSEUDONYM_CONFIG_TOKEN, PseudonymConfig } from '../pseudonym.config';
119
import { ExternalToolPseudonymRepo, Pseudonym } from '../repo';
@@ -45,16 +43,7 @@ export class PseudonymService {
4543
throw new InternalServerErrorException('User or tool id is missing');
4644
}
4745

48-
const pseudonym = new Pseudonym({
49-
id: new ObjectId().toHexString(),
50-
pseudonym: uuidv4(),
51-
userId: user.id,
52-
toolId: tool.id,
53-
createdAt: new Date(),
54-
updatedAt: new Date(),
55-
});
56-
57-
const result = await this.externalToolPseudonymRepo.createOrUpdate(pseudonym);
46+
const result = await this.externalToolPseudonymRepo.createOrUpdate(user.id, tool.id);
5847

5948
return result;
6049
}

0 commit comments

Comments
 (0)