Skip to content

Commit

Permalink
Added counter if username already exists and accessing KC for it
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-kiefer committed Nov 22, 2023
1 parent 7aa6dc5 commit 6710f26
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
32 changes: 32 additions & 0 deletions src/modules/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { KeycloakUserService, UserDo } from '../keycloak-administration/index.js';
import { DomainError, EntityNotFoundError } from '../../shared/error/index.js';

@Injectable()
export class UserRepository {
public constructor(private kcUserService: KeycloakUserService) {}

public async usernameExists(username: string): Promise<boolean> {
const searchResult: Result<UserDo<true>, DomainError> | { ok: false; error: DomainError } =
await this.kcUserService.findOne({ username: username });
if (searchResult.ok) {
return true;
} else {
if (searchResult.error instanceof EntityNotFoundError) {
return false;
}
}
throw searchResult.error;
}

public async getNextAvailableUsername(calculatedUsername: string): Promise<string> {
if (!(await this.usernameExists(calculatedUsername))) {
return calculatedUsername;
}
let counter: number = 1;
while (await this.usernameExists(calculatedUsername + counter)) {
counter = counter + 1;
}
return calculatedUsername + counter;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsernameGeneratorService } from './username-generator.service.js';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { KeycloakUserService, UserDo } from '../keycloak-administration/index.js';
import { FindUserFilter, KeycloakUserService, UserDo } from '../keycloak-administration/index.js';
import { UserRepository } from './user.repository.js';
import { EntityNotFoundError } from '../../shared/error/index.js';

Expand Down Expand Up @@ -73,20 +73,20 @@ describe('The UsernameGenerator Service', () => {
});

it('should add a number when username already exists', async () => {
kcUserService.findOne.mockResolvedValueOnce({ ok: true, value: new UserDo<true>() });
const generatedUsername = await service.generateUsername('Max', 'Meyer');
kcUserService.findOne
.mockResolvedValueOnce({ ok: true, value: new UserDo<true>() })
.mockResolvedValueOnce({ ok: false, error: new EntityNotFoundError('Not found') });
const generatedUsername: string = await service.generateUsername('Max', 'Meyer');
expect(generatedUsername).toBe('mmeyer1');
});

it('should increment the counter when a username would exist more than twice', async () => {
/*
kcUserService.findOne.mockImplementation((userFilter: FindUserFilter) => {
if (userFilter.username == 'mmeyer' || userFilter.username == 'mmeyer1') {
return Promise.resolve({ ok: true, value: new UserDo<true>() });
} else return Promise.resolve({ ok: false, error: new EntityNotFoundError('Not found') });
});*/
kcUserService.findOne.mockRejectedValueOnce('Blah');
const generatedUsername = await service.generateUsername('Max', 'Meyer');
});
const generatedUsername: string = await service.generateUsername('Max', 'Meyer');
expect(generatedUsername).toBe('mmeyer2');
});
});
2 changes: 1 addition & 1 deletion src/modules/user/username-generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class UsernameGeneratorService {
if (lastname.length == 0) {
throw new Error('Last name not given');
}
const calculatedUsername = this.cleanString(firstname)[0] + this.cleanString(lastname);
const calculatedUsername: string = this.cleanString(firstname)[0] + this.cleanString(lastname);

return this.repository.getNextAvailableUsername(calculatedUsername);
}
Expand Down

0 comments on commit 6710f26

Please sign in to comment.