From 5c63e712002f15a64db22a6ac4878330e80be5bb Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 10 May 2023 13:26:28 +0200 Subject: [PATCH] clean code --- .../authentication-messager.controller.ts | 8 ++--- .../primaries/authentication.controller.ts | 30 +++++++++---------- .../domain/usecases/add-username.usecase.ts | 8 ++--- .../usecases/create-authentication.usecase.ts | 12 ++++---- .../usecases/delete-authentication.usecase.ts | 12 ++++---- .../usecases/delete-username.usecase.ts | 12 ++++---- .../usecases/update-password.usecase.ts | 8 ++--- .../usecases/update-username.usecase.ts | 18 +++++------ .../validate-authentication.usecase.ts | 8 ++--- .../secondaries/opa.decision-maker.ts | 8 ++--- .../primaries/health-server.controller.ts | 4 +-- 11 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/modules/authentication/adapters/primaries/authentication-messager.controller.ts b/src/modules/authentication/adapters/primaries/authentication-messager.controller.ts index 634f005..1c0cba2 100644 --- a/src/modules/authentication/adapters/primaries/authentication-messager.controller.ts +++ b/src/modules/authentication/adapters/primaries/authentication-messager.controller.ts @@ -9,7 +9,7 @@ import { DeleteAuthenticationCommand } from '../../commands/delete-authenticatio @Controller() export class AuthenticationMessagerController { - constructor(private readonly _commandBus: CommandBus) {} + constructor(private readonly commandBus: CommandBus) {} @RabbitSubscribe({ name: 'userUpdate', @@ -22,7 +22,7 @@ export class AuthenticationMessagerController { updateUsernameRequest.uuid = updatedUser.uuid; updateUsernameRequest.username = updatedUser.email; updateUsernameRequest.type = Type.EMAIL; - await this._commandBus.execute( + await this.commandBus.execute( new UpdateUsernameCommand(updateUsernameRequest), ); } @@ -31,7 +31,7 @@ export class AuthenticationMessagerController { updateUsernameRequest.uuid = updatedUser.uuid; updateUsernameRequest.username = updatedUser.phone; updateUsernameRequest.type = Type.PHONE; - await this._commandBus.execute( + await this.commandBus.execute( new UpdateUsernameCommand(updateUsernameRequest), ); } @@ -45,7 +45,7 @@ export class AuthenticationMessagerController { if (!deletedUser.hasOwnProperty('uuid')) throw new Error(); const deleteAuthenticationRequest = new DeleteAuthenticationRequest(); deleteAuthenticationRequest.uuid = deletedUser.uuid; - await this._commandBus.execute( + await this.commandBus.execute( new DeleteAuthenticationCommand(deleteAuthenticationRequest), ); } diff --git a/src/modules/authentication/adapters/primaries/authentication.controller.ts b/src/modules/authentication/adapters/primaries/authentication.controller.ts index 987c1f0..af72330 100644 --- a/src/modules/authentication/adapters/primaries/authentication.controller.ts +++ b/src/modules/authentication/adapters/primaries/authentication.controller.ts @@ -33,9 +33,9 @@ import { UsernamePresenter } from './username.presenter'; @Controller() export class AuthenticationController { constructor( - private readonly _commandBus: CommandBus, - private readonly _queryBus: QueryBus, - @InjectMapper() private readonly _mapper: Mapper, + private readonly commandBus: CommandBus, + private readonly queryBus: QueryBus, + @InjectMapper() private readonly mapper: Mapper, ) {} @GrpcMethod('AuthenticationService', 'Validate') @@ -43,10 +43,10 @@ export class AuthenticationController { data: ValidateAuthenticationRequest, ): Promise { try { - const authentication: Authentication = await this._queryBus.execute( + const authentication: Authentication = await this.queryBus.execute( new ValidateAuthenticationQuery(data.username, data.password), ); - return this._mapper.map( + return this.mapper.map( authentication, Authentication, AuthenticationPresenter, @@ -64,10 +64,10 @@ export class AuthenticationController { data: CreateAuthenticationRequest, ): Promise { try { - const authentication: Authentication = await this._commandBus.execute( + const authentication: Authentication = await this.commandBus.execute( new CreateAuthenticationCommand(data), ); - return this._mapper.map( + return this.mapper.map( authentication, Authentication, AuthenticationPresenter, @@ -91,11 +91,11 @@ export class AuthenticationController { @GrpcMethod('AuthenticationService', 'AddUsername') async addUsername(data: AddUsernameRequest): Promise { try { - const username: Username = await this._commandBus.execute( + const username: Username = await this.commandBus.execute( new AddUsernameCommand(data), ); - return this._mapper.map(username, Username, UsernamePresenter); + return this.mapper.map(username, Username, UsernamePresenter); } catch (e) { if (e instanceof DatabaseException) { if (e.message.includes('Already exists')) { @@ -117,11 +117,11 @@ export class AuthenticationController { data: UpdateUsernameRequest, ): Promise { try { - const username: Username = await this._commandBus.execute( + const username: Username = await this.commandBus.execute( new UpdateUsernameCommand(data), ); - return this._mapper.map(username, Username, UsernamePresenter); + return this.mapper.map(username, Username, UsernamePresenter); } catch (e) { if (e instanceof DatabaseException) { if (e.message.includes('Already exists')) { @@ -143,11 +143,11 @@ export class AuthenticationController { data: UpdatePasswordRequest, ): Promise { try { - const authentication: Authentication = await this._commandBus.execute( + const authentication: Authentication = await this.commandBus.execute( new UpdatePasswordCommand(data), ); - return this._mapper.map( + return this.mapper.map( authentication, Authentication, AuthenticationPresenter, @@ -163,7 +163,7 @@ export class AuthenticationController { @GrpcMethod('AuthenticationService', 'DeleteUsername') async deleteUsername(data: DeleteUsernameRequest) { try { - return await this._commandBus.execute(new DeleteUsernameCommand(data)); + return await this.commandBus.execute(new DeleteUsernameCommand(data)); } catch (e) { throw new RpcException({ code: 7, @@ -175,7 +175,7 @@ export class AuthenticationController { @GrpcMethod('AuthenticationService', 'Delete') async deleteAuthentication(data: DeleteAuthenticationRequest) { try { - return await this._commandBus.execute( + return await this.commandBus.execute( new DeleteAuthenticationCommand(data), ); } catch (e) { diff --git a/src/modules/authentication/domain/usecases/add-username.usecase.ts b/src/modules/authentication/domain/usecases/add-username.usecase.ts index d2bd577..a8d5489 100644 --- a/src/modules/authentication/domain/usecases/add-username.usecase.ts +++ b/src/modules/authentication/domain/usecases/add-username.usecase.ts @@ -7,20 +7,20 @@ import { Username } from '../entities/username'; @CommandHandler(AddUsernameCommand) export class AddUsernameUseCase { constructor( - private readonly _usernameRepository: UsernameRepository, - private readonly _messager: Messager, + private readonly usernameRepository: UsernameRepository, + private readonly messager: Messager, ) {} execute = async (command: AddUsernameCommand): Promise => { const { uuid, username, type } = command.addUsernameRequest; try { - return await this._usernameRepository.create({ + return await this.usernameRepository.create({ uuid, type, username, }); } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.username.add.warning', JSON.stringify({ command, diff --git a/src/modules/authentication/domain/usecases/create-authentication.usecase.ts b/src/modules/authentication/domain/usecases/create-authentication.usecase.ts index 275d148..9ca07e4 100644 --- a/src/modules/authentication/domain/usecases/create-authentication.usecase.ts +++ b/src/modules/authentication/domain/usecases/create-authentication.usecase.ts @@ -9,9 +9,9 @@ import { Messager } from '../../adapters/secondaries/messager'; @CommandHandler(CreateAuthenticationCommand) export class CreateAuthenticationUseCase { constructor( - private readonly _authenticationRepository: AuthenticationRepository, - private readonly _usernameRepository: UsernameRepository, - private readonly _messager: Messager, + private readonly authenticationRepository: AuthenticationRepository, + private readonly usernameRepository: UsernameRepository, + private readonly messager: Messager, ) {} execute = async ( @@ -21,19 +21,19 @@ export class CreateAuthenticationUseCase { const hash = await bcrypt.hash(password, 10); try { - const auth = await this._authenticationRepository.create({ + const auth = await this.authenticationRepository.create({ uuid, password: hash, }); - await this._usernameRepository.create({ + await this.usernameRepository.create({ uuid, ...username, }); return auth; } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.create.crit', JSON.stringify({ command, diff --git a/src/modules/authentication/domain/usecases/delete-authentication.usecase.ts b/src/modules/authentication/domain/usecases/delete-authentication.usecase.ts index b8f4c8e..e429a5d 100644 --- a/src/modules/authentication/domain/usecases/delete-authentication.usecase.ts +++ b/src/modules/authentication/domain/usecases/delete-authentication.usecase.ts @@ -8,23 +8,23 @@ import { Authentication } from '../entities/authentication'; @CommandHandler(DeleteAuthenticationCommand) export class DeleteAuthenticationUseCase { constructor( - private readonly _authenticationRepository: AuthenticationRepository, - private readonly _usernameRepository: UsernameRepository, - private readonly _messager: Messager, + private readonly authenticationRepository: AuthenticationRepository, + private readonly usernameRepository: UsernameRepository, + private readonly messager: Messager, ) {} execute = async ( command: DeleteAuthenticationCommand, ): Promise => { try { - await this._usernameRepository.deleteMany({ + await this.usernameRepository.deleteMany({ uuid: command.deleteAuthenticationRequest.uuid, }); - return await this._authenticationRepository.delete( + return await this.authenticationRepository.delete( command.deleteAuthenticationRequest.uuid, ); } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.delete.crit', JSON.stringify({ command, diff --git a/src/modules/authentication/domain/usecases/delete-username.usecase.ts b/src/modules/authentication/domain/usecases/delete-username.usecase.ts index e58a914..76d3430 100644 --- a/src/modules/authentication/domain/usecases/delete-username.usecase.ts +++ b/src/modules/authentication/domain/usecases/delete-username.usecase.ts @@ -7,25 +7,25 @@ import { DeleteUsernameCommand } from '../../commands/delete-username.command'; @CommandHandler(DeleteUsernameCommand) export class DeleteUsernameUseCase { constructor( - private readonly _usernameRepository: UsernameRepository, - private readonly _messager: Messager, + private readonly usernameRepository: UsernameRepository, + private readonly messager: Messager, ) {} execute = async (command: DeleteUsernameCommand): Promise => { try { const { username } = command.deleteUsernameRequest; - const usernameFound = await this._usernameRepository.findOne({ + const usernameFound = await this.usernameRepository.findOne({ username, }); - const usernames = await this._usernameRepository.findAll(1, 1, { + const usernames = await this.usernameRepository.findAll(1, 1, { uuid: usernameFound.uuid, }); if (usernames.total > 1) { - return await this._usernameRepository.deleteMany({ username }); + return await this.usernameRepository.deleteMany({ username }); } throw new UnauthorizedException(); } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.username.delete.warning', JSON.stringify({ command, diff --git a/src/modules/authentication/domain/usecases/update-password.usecase.ts b/src/modules/authentication/domain/usecases/update-password.usecase.ts index a7e4bb4..5809f54 100644 --- a/src/modules/authentication/domain/usecases/update-password.usecase.ts +++ b/src/modules/authentication/domain/usecases/update-password.usecase.ts @@ -8,8 +8,8 @@ import { Messager } from '../../adapters/secondaries/messager'; @CommandHandler(UpdatePasswordCommand) export class UpdatePasswordUseCase { constructor( - private readonly _authenticationRepository: AuthenticationRepository, - private readonly _messager: Messager, + private readonly authenticationRepository: AuthenticationRepository, + private readonly messager: Messager, ) {} execute = async (command: UpdatePasswordCommand): Promise => { @@ -17,11 +17,11 @@ export class UpdatePasswordUseCase { const hash = await bcrypt.hash(password, 10); try { - return await this._authenticationRepository.update(uuid, { + return await this.authenticationRepository.update(uuid, { password: hash, }); } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.password.update.warning', JSON.stringify({ command, diff --git a/src/modules/authentication/domain/usecases/update-username.usecase.ts b/src/modules/authentication/domain/usecases/update-username.usecase.ts index 24f93ef..7870bf0 100644 --- a/src/modules/authentication/domain/usecases/update-username.usecase.ts +++ b/src/modules/authentication/domain/usecases/update-username.usecase.ts @@ -13,23 +13,23 @@ import { Username } from '../entities/username'; @CommandHandler(UpdateUsernameCommand) export class UpdateUsernameUseCase { constructor( - private readonly _usernameRepository: UsernameRepository, - private readonly _commandBus: CommandBus, - @InjectMapper() private readonly _mapper: Mapper, - private readonly _messager: Messager, + private readonly usernameRepository: UsernameRepository, + private readonly commandBus: CommandBus, + @InjectMapper() private readonly mapper: Mapper, + private readonly messager: Messager, ) {} execute = async (command: UpdateUsernameCommand): Promise => { const { uuid, username, type } = command.updateUsernameRequest; if (!username) throw new BadRequestException(); // update username if it exists, otherwise create it - const existingUsername = await this._usernameRepository.findOne({ + const existingUsername = await this.usernameRepository.findOne({ uuid, type, }); if (existingUsername) { try { - return await this._usernameRepository.updateWhere( + return await this.usernameRepository.updateWhere( { uuid_type: { uuid, @@ -41,7 +41,7 @@ export class UpdateUsernameUseCase { }, ); } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.auth.username.update.warning', JSON.stringify({ command, @@ -51,13 +51,13 @@ export class UpdateUsernameUseCase { throw error; } } - const addUsernameRequest = this._mapper.map( + const addUsernameRequest = this.mapper.map( command.updateUsernameRequest, UpdateUsernameRequest, AddUsernameRequest, ); try { - return await this._commandBus.execute( + return await this.commandBus.execute( new AddUsernameCommand(addUsernameRequest), ); } catch (e) { diff --git a/src/modules/authentication/domain/usecases/validate-authentication.usecase.ts b/src/modules/authentication/domain/usecases/validate-authentication.usecase.ts index 7762938..56829e9 100644 --- a/src/modules/authentication/domain/usecases/validate-authentication.usecase.ts +++ b/src/modules/authentication/domain/usecases/validate-authentication.usecase.ts @@ -10,8 +10,8 @@ import { Username } from '../entities/username'; @QueryHandler(ValidateAuthenticationQuery) export class ValidateAuthenticationUseCase { constructor( - private readonly _authenticationRepository: AuthenticationRepository, - private readonly _usernameRepository: UsernameRepository, + private readonly authenticationRepository: AuthenticationRepository, + private readonly usernameRepository: UsernameRepository, ) {} execute = async ( @@ -19,14 +19,14 @@ export class ValidateAuthenticationUseCase { ): Promise => { let username = new Username(); try { - username = await this._usernameRepository.findOne({ + username = await this.usernameRepository.findOne({ username: validate.username, }); } catch (e) { throw new NotFoundException(); } try { - const auth = await this._authenticationRepository.findOne({ + const auth = await this.authenticationRepository.findOne({ uuid: username.uuid, }); if (auth) { diff --git a/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts b/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts index bc86940..9a012fa 100644 --- a/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts +++ b/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts @@ -12,8 +12,8 @@ import { Authorization } from '../../domain/entities/authorization'; @Injectable() export class OpaDecisionMaker extends IMakeDecision { constructor( - private readonly _configService: ConfigService, - private readonly _httpService: HttpService, + private readonly configService: ConfigService, + private readonly httpService: HttpService, ) { super(); } @@ -29,8 +29,8 @@ export class OpaDecisionMaker extends IMakeDecision { ); try { const { data } = await lastValueFrom( - this._httpService.post( - this._configService.get('OPA_URL') + domain + '/' + action, + this.httpService.post( + this.configService.get('OPA_URL') + domain + '/' + action, { input: { ...reducedContext, diff --git a/src/modules/health/adapters/primaries/health-server.controller.ts b/src/modules/health/adapters/primaries/health-server.controller.ts index b5e7b74..858f705 100644 --- a/src/modules/health/adapters/primaries/health-server.controller.ts +++ b/src/modules/health/adapters/primaries/health-server.controller.ts @@ -20,7 +20,7 @@ interface HealthCheckResponse { @Controller() export class HealthServerController { constructor( - private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, + private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, ) {} @GrpcMethod('Health', 'Check') @@ -30,7 +30,7 @@ export class HealthServerController { // eslint-disable-next-line @typescript-eslint/no-unused-vars metadata: any, ): Promise { - const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy( + const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy( 'prisma', ); return {