clean code

This commit is contained in:
sbriat 2023-05-10 13:26:28 +02:00
parent e316198a03
commit 5c63e71200
11 changed files with 64 additions and 64 deletions

View File

@ -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),
);
}

View File

@ -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<AuthenticationPresenter> {
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<AuthenticationPresenter> {
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<UsernamePresenter> {
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<UsernamePresenter> {
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<AuthenticationPresenter> {
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) {

View File

@ -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<Username> => {
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,

View File

@ -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,

View File

@ -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<Authentication> => {
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,

View File

@ -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<void> => {
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,

View File

@ -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<Authentication> => {
@ -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,

View File

@ -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<Username> => {
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) {

View File

@ -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<Authentication> => {
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) {

View File

@ -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<Decision>(
this._configService.get<string>('OPA_URL') + domain + '/' + action,
this.httpService.post<Decision>(
this.configService.get<string>('OPA_URL') + domain + '/' + action,
{
input: {
...reducedContext,

View File

@ -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<HealthCheckResponse> {
const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy(
const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy(
'prisma',
);
return {