diff --git a/src/keycloak/keycloak.service.ts b/src/keycloak/keycloak.service.ts index d065f119..54735776 100644 --- a/src/keycloak/keycloak.service.ts +++ b/src/keycloak/keycloak.service.ts @@ -30,7 +30,7 @@ export interface RegisterUserData { } export interface ExistingUserData { - email: string, + email: string; } export interface GenerateTokenData { @@ -142,14 +142,12 @@ export class KeyCloakService implements OnModuleInit { }); } - public async isUserExists({ - email, - }: ExistingUserData): Promise { - this.log.debug(`Called isUserExist`); + public async isUserExists({ email }: ExistingUserData): Promise { + this.log.debug(`Called isUserExist`); const { access_token, token_type } = await this.generateToken(); - const [existingUser] = await this.httpClient.get( + const [existingUser]: unknown[] = await this.httpClient.get( `${this.server_uri}/admin/realms/${this.realm}/users?email=${email}`, { headers: { @@ -157,7 +155,7 @@ export class KeyCloakService implements OnModuleInit { Authorization: `${token_type} ${access_token}`, }, responseType: 'json', - } + }, ); return !!existingUser; } @@ -172,7 +170,7 @@ export class KeyCloakService implements OnModuleInit { const { access_token, token_type } = await this.generateToken(); - const user: User = await this.httpClient.post( + return this.httpClient.post( `${this.server_uri}/admin/realms/${this.realm}/users`, { firstName, @@ -195,8 +193,7 @@ export class KeyCloakService implements OnModuleInit { }, responseType: 'json', }, - ) - return user; + ); } public async generateToken(tokenData?: GenerateTokenData): Promise { diff --git a/src/users/api/UserDto.ts b/src/users/api/UserDto.ts index 99ffe965..50c999a4 100644 --- a/src/users/api/UserDto.ts +++ b/src/users/api/UserDto.ts @@ -1,5 +1,4 @@ import { ApiProperty } from '@nestjs/swagger'; -import { User } from '../../model/user.entity'; export class UserDto { @ApiProperty() @@ -11,11 +10,11 @@ export class UserDto { @ApiProperty() lastName: string; - public static convertToApi(user: User): UserDto { - return { - email: user.email, - firstName: user.firstName, - lastName: user.lastName, - }; + constructor( + params: { + [P in keyof UserDto]: UserDto[P]; + }, + ) { + Object.assign(this, params); } } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 390ef1bb..8255756a 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -60,7 +60,7 @@ export class UsersController { async getUser(@Param('email') email: string): Promise { try { this.logger.debug(`Find a user by email: ${email}`); - return UserDto.convertToApi(await this.usersService.findByEmail(email)); + return new UserDto(await this.usersService.findByEmail(email)); } catch (err) { throw new InternalServerErrorException({ error: err.message, @@ -151,7 +151,7 @@ export class UsersController { throw new NotFoundException('User not found in ldap'); } - return users.map(UserDto.convertToApi); + return users.map((user: User) => new UserDto(user)); } @Post('/basic') @@ -165,14 +165,14 @@ export class UsersController { async createUser(@Body() user: CreateUserRequest): Promise { try { this.logger.debug(`Create a basic user: ${user}`); + const userExists = await this.usersService.findByEmail(user.email); + if (userExists) { - throw new HttpException( - 'User already exists', - 409 - ) + throw new HttpException('User already exists', 409); } - const newUser = UserDto.convertToApi( + + return new UserDto( await this.usersService.createUser( user.email, user.firstName, @@ -180,9 +180,11 @@ export class UsersController { user.password, ), ); - return newUser; } catch (err) { - throw new HttpException(err.message ?? 'Something went wrong', err.status ?? 500) + throw new HttpException( + err.message ?? 'Something went wrong', + err.status ?? 500, + ); } } @@ -194,30 +196,31 @@ export class UsersController { type: UserDto, status: 200, }) - async createOIDCUser(@Body() user: CreateUserRequest): Promise { + async createOIDCUser(@Body() user: CreateUserRequest): Promise { try { this.logger.debug(`Create a OIDC user: ${user}`); const userExists = await this.keyCloakService.isUserExists({ email: user.email, }); + if (userExists) { - throw new HttpException( - 'User already exists', - 409 - ) + throw new HttpException('User already exists', 409); } - const newUser = UserDto.convertToApi( + + return new UserDto( await this.keyCloakService.registerUser({ email: user.email, firstName: user.firstName, lastName: user.lastName, password: user.password, - })); - return newUser; - - } catch ({ response = 'Something went wrong', status = 500 }) { - throw new HttpException(response, status) + }), + ); + } catch (err) { + throw new HttpException( + err.message ?? 'Something went wrong', + err.status ?? 500, + ); } }