Merge branch 'cleanCode' into 'main'

clean code

See merge request v3/service/auth!40
This commit is contained in:
Sylvain Briat 2023-05-10 11:30:05 +00:00
commit 0162066557
11 changed files with 64 additions and 64 deletions

View File

@ -9,7 +9,7 @@ import { DeleteAuthenticationCommand } from '../../commands/delete-authenticatio
@Controller() @Controller()
export class AuthenticationMessagerController { export class AuthenticationMessagerController {
constructor(private readonly _commandBus: CommandBus) {} constructor(private readonly commandBus: CommandBus) {}
@RabbitSubscribe({ @RabbitSubscribe({
name: 'userUpdate', name: 'userUpdate',
@ -22,7 +22,7 @@ export class AuthenticationMessagerController {
updateUsernameRequest.uuid = updatedUser.uuid; updateUsernameRequest.uuid = updatedUser.uuid;
updateUsernameRequest.username = updatedUser.email; updateUsernameRequest.username = updatedUser.email;
updateUsernameRequest.type = Type.EMAIL; updateUsernameRequest.type = Type.EMAIL;
await this._commandBus.execute( await this.commandBus.execute(
new UpdateUsernameCommand(updateUsernameRequest), new UpdateUsernameCommand(updateUsernameRequest),
); );
} }
@ -31,7 +31,7 @@ export class AuthenticationMessagerController {
updateUsernameRequest.uuid = updatedUser.uuid; updateUsernameRequest.uuid = updatedUser.uuid;
updateUsernameRequest.username = updatedUser.phone; updateUsernameRequest.username = updatedUser.phone;
updateUsernameRequest.type = Type.PHONE; updateUsernameRequest.type = Type.PHONE;
await this._commandBus.execute( await this.commandBus.execute(
new UpdateUsernameCommand(updateUsernameRequest), new UpdateUsernameCommand(updateUsernameRequest),
); );
} }
@ -45,7 +45,7 @@ export class AuthenticationMessagerController {
if (!deletedUser.hasOwnProperty('uuid')) throw new Error(); if (!deletedUser.hasOwnProperty('uuid')) throw new Error();
const deleteAuthenticationRequest = new DeleteAuthenticationRequest(); const deleteAuthenticationRequest = new DeleteAuthenticationRequest();
deleteAuthenticationRequest.uuid = deletedUser.uuid; deleteAuthenticationRequest.uuid = deletedUser.uuid;
await this._commandBus.execute( await this.commandBus.execute(
new DeleteAuthenticationCommand(deleteAuthenticationRequest), new DeleteAuthenticationCommand(deleteAuthenticationRequest),
); );
} }

View File

@ -33,9 +33,9 @@ import { UsernamePresenter } from './username.presenter';
@Controller() @Controller()
export class AuthenticationController { export class AuthenticationController {
constructor( constructor(
private readonly _commandBus: CommandBus, private readonly commandBus: CommandBus,
private readonly _queryBus: QueryBus, private readonly queryBus: QueryBus,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly mapper: Mapper,
) {} ) {}
@GrpcMethod('AuthenticationService', 'Validate') @GrpcMethod('AuthenticationService', 'Validate')
@ -43,10 +43,10 @@ export class AuthenticationController {
data: ValidateAuthenticationRequest, data: ValidateAuthenticationRequest,
): Promise<AuthenticationPresenter> { ): Promise<AuthenticationPresenter> {
try { try {
const authentication: Authentication = await this._queryBus.execute( const authentication: Authentication = await this.queryBus.execute(
new ValidateAuthenticationQuery(data.username, data.password), new ValidateAuthenticationQuery(data.username, data.password),
); );
return this._mapper.map( return this.mapper.map(
authentication, authentication,
Authentication, Authentication,
AuthenticationPresenter, AuthenticationPresenter,
@ -64,10 +64,10 @@ export class AuthenticationController {
data: CreateAuthenticationRequest, data: CreateAuthenticationRequest,
): Promise<AuthenticationPresenter> { ): Promise<AuthenticationPresenter> {
try { try {
const authentication: Authentication = await this._commandBus.execute( const authentication: Authentication = await this.commandBus.execute(
new CreateAuthenticationCommand(data), new CreateAuthenticationCommand(data),
); );
return this._mapper.map( return this.mapper.map(
authentication, authentication,
Authentication, Authentication,
AuthenticationPresenter, AuthenticationPresenter,
@ -91,11 +91,11 @@ export class AuthenticationController {
@GrpcMethod('AuthenticationService', 'AddUsername') @GrpcMethod('AuthenticationService', 'AddUsername')
async addUsername(data: AddUsernameRequest): Promise<UsernamePresenter> { async addUsername(data: AddUsernameRequest): Promise<UsernamePresenter> {
try { try {
const username: Username = await this._commandBus.execute( const username: Username = await this.commandBus.execute(
new AddUsernameCommand(data), new AddUsernameCommand(data),
); );
return this._mapper.map(username, Username, UsernamePresenter); return this.mapper.map(username, Username, UsernamePresenter);
} catch (e) { } catch (e) {
if (e instanceof DatabaseException) { if (e instanceof DatabaseException) {
if (e.message.includes('Already exists')) { if (e.message.includes('Already exists')) {
@ -117,11 +117,11 @@ export class AuthenticationController {
data: UpdateUsernameRequest, data: UpdateUsernameRequest,
): Promise<UsernamePresenter> { ): Promise<UsernamePresenter> {
try { try {
const username: Username = await this._commandBus.execute( const username: Username = await this.commandBus.execute(
new UpdateUsernameCommand(data), new UpdateUsernameCommand(data),
); );
return this._mapper.map(username, Username, UsernamePresenter); return this.mapper.map(username, Username, UsernamePresenter);
} catch (e) { } catch (e) {
if (e instanceof DatabaseException) { if (e instanceof DatabaseException) {
if (e.message.includes('Already exists')) { if (e.message.includes('Already exists')) {
@ -143,11 +143,11 @@ export class AuthenticationController {
data: UpdatePasswordRequest, data: UpdatePasswordRequest,
): Promise<AuthenticationPresenter> { ): Promise<AuthenticationPresenter> {
try { try {
const authentication: Authentication = await this._commandBus.execute( const authentication: Authentication = await this.commandBus.execute(
new UpdatePasswordCommand(data), new UpdatePasswordCommand(data),
); );
return this._mapper.map( return this.mapper.map(
authentication, authentication,
Authentication, Authentication,
AuthenticationPresenter, AuthenticationPresenter,
@ -163,7 +163,7 @@ export class AuthenticationController {
@GrpcMethod('AuthenticationService', 'DeleteUsername') @GrpcMethod('AuthenticationService', 'DeleteUsername')
async deleteUsername(data: DeleteUsernameRequest) { async deleteUsername(data: DeleteUsernameRequest) {
try { try {
return await this._commandBus.execute(new DeleteUsernameCommand(data)); return await this.commandBus.execute(new DeleteUsernameCommand(data));
} catch (e) { } catch (e) {
throw new RpcException({ throw new RpcException({
code: 7, code: 7,
@ -175,7 +175,7 @@ export class AuthenticationController {
@GrpcMethod('AuthenticationService', 'Delete') @GrpcMethod('AuthenticationService', 'Delete')
async deleteAuthentication(data: DeleteAuthenticationRequest) { async deleteAuthentication(data: DeleteAuthenticationRequest) {
try { try {
return await this._commandBus.execute( return await this.commandBus.execute(
new DeleteAuthenticationCommand(data), new DeleteAuthenticationCommand(data),
); );
} catch (e) { } catch (e) {

View File

@ -7,20 +7,20 @@ import { Username } from '../entities/username';
@CommandHandler(AddUsernameCommand) @CommandHandler(AddUsernameCommand)
export class AddUsernameUseCase { export class AddUsernameUseCase {
constructor( constructor(
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async (command: AddUsernameCommand): Promise<Username> => { execute = async (command: AddUsernameCommand): Promise<Username> => {
const { uuid, username, type } = command.addUsernameRequest; const { uuid, username, type } = command.addUsernameRequest;
try { try {
return await this._usernameRepository.create({ return await this.usernameRepository.create({
uuid, uuid,
type, type,
username, username,
}); });
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.username.add.warning', 'logging.auth.username.add.warning',
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -9,9 +9,9 @@ import { Messager } from '../../adapters/secondaries/messager';
@CommandHandler(CreateAuthenticationCommand) @CommandHandler(CreateAuthenticationCommand)
export class CreateAuthenticationUseCase { export class CreateAuthenticationUseCase {
constructor( constructor(
private readonly _authenticationRepository: AuthenticationRepository, private readonly authenticationRepository: AuthenticationRepository,
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async ( execute = async (
@ -21,19 +21,19 @@ export class CreateAuthenticationUseCase {
const hash = await bcrypt.hash(password, 10); const hash = await bcrypt.hash(password, 10);
try { try {
const auth = await this._authenticationRepository.create({ const auth = await this.authenticationRepository.create({
uuid, uuid,
password: hash, password: hash,
}); });
await this._usernameRepository.create({ await this.usernameRepository.create({
uuid, uuid,
...username, ...username,
}); });
return auth; return auth;
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.create.crit', 'logging.auth.create.crit',
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -8,23 +8,23 @@ import { Authentication } from '../entities/authentication';
@CommandHandler(DeleteAuthenticationCommand) @CommandHandler(DeleteAuthenticationCommand)
export class DeleteAuthenticationUseCase { export class DeleteAuthenticationUseCase {
constructor( constructor(
private readonly _authenticationRepository: AuthenticationRepository, private readonly authenticationRepository: AuthenticationRepository,
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async ( execute = async (
command: DeleteAuthenticationCommand, command: DeleteAuthenticationCommand,
): Promise<Authentication> => { ): Promise<Authentication> => {
try { try {
await this._usernameRepository.deleteMany({ await this.usernameRepository.deleteMany({
uuid: command.deleteAuthenticationRequest.uuid, uuid: command.deleteAuthenticationRequest.uuid,
}); });
return await this._authenticationRepository.delete( return await this.authenticationRepository.delete(
command.deleteAuthenticationRequest.uuid, command.deleteAuthenticationRequest.uuid,
); );
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.delete.crit', 'logging.auth.delete.crit',
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -7,25 +7,25 @@ import { DeleteUsernameCommand } from '../../commands/delete-username.command';
@CommandHandler(DeleteUsernameCommand) @CommandHandler(DeleteUsernameCommand)
export class DeleteUsernameUseCase { export class DeleteUsernameUseCase {
constructor( constructor(
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async (command: DeleteUsernameCommand): Promise<void> => { execute = async (command: DeleteUsernameCommand): Promise<void> => {
try { try {
const { username } = command.deleteUsernameRequest; const { username } = command.deleteUsernameRequest;
const usernameFound = await this._usernameRepository.findOne({ const usernameFound = await this.usernameRepository.findOne({
username, username,
}); });
const usernames = await this._usernameRepository.findAll(1, 1, { const usernames = await this.usernameRepository.findAll(1, 1, {
uuid: usernameFound.uuid, uuid: usernameFound.uuid,
}); });
if (usernames.total > 1) { if (usernames.total > 1) {
return await this._usernameRepository.deleteMany({ username }); return await this.usernameRepository.deleteMany({ username });
} }
throw new UnauthorizedException(); throw new UnauthorizedException();
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.username.delete.warning', 'logging.auth.username.delete.warning',
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -8,8 +8,8 @@ import { Messager } from '../../adapters/secondaries/messager';
@CommandHandler(UpdatePasswordCommand) @CommandHandler(UpdatePasswordCommand)
export class UpdatePasswordUseCase { export class UpdatePasswordUseCase {
constructor( constructor(
private readonly _authenticationRepository: AuthenticationRepository, private readonly authenticationRepository: AuthenticationRepository,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async (command: UpdatePasswordCommand): Promise<Authentication> => { execute = async (command: UpdatePasswordCommand): Promise<Authentication> => {
@ -17,11 +17,11 @@ export class UpdatePasswordUseCase {
const hash = await bcrypt.hash(password, 10); const hash = await bcrypt.hash(password, 10);
try { try {
return await this._authenticationRepository.update(uuid, { return await this.authenticationRepository.update(uuid, {
password: hash, password: hash,
}); });
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.password.update.warning', 'logging.auth.password.update.warning',
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -13,23 +13,23 @@ import { Username } from '../entities/username';
@CommandHandler(UpdateUsernameCommand) @CommandHandler(UpdateUsernameCommand)
export class UpdateUsernameUseCase { export class UpdateUsernameUseCase {
constructor( constructor(
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
private readonly _commandBus: CommandBus, private readonly commandBus: CommandBus,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly mapper: Mapper,
private readonly _messager: Messager, private readonly messager: Messager,
) {} ) {}
execute = async (command: UpdateUsernameCommand): Promise<Username> => { execute = async (command: UpdateUsernameCommand): Promise<Username> => {
const { uuid, username, type } = command.updateUsernameRequest; const { uuid, username, type } = command.updateUsernameRequest;
if (!username) throw new BadRequestException(); if (!username) throw new BadRequestException();
// update username if it exists, otherwise create it // update username if it exists, otherwise create it
const existingUsername = await this._usernameRepository.findOne({ const existingUsername = await this.usernameRepository.findOne({
uuid, uuid,
type, type,
}); });
if (existingUsername) { if (existingUsername) {
try { try {
return await this._usernameRepository.updateWhere( return await this.usernameRepository.updateWhere(
{ {
uuid_type: { uuid_type: {
uuid, uuid,
@ -41,7 +41,7 @@ export class UpdateUsernameUseCase {
}, },
); );
} catch (error) { } catch (error) {
this._messager.publish( this.messager.publish(
'logging.auth.username.update.warning', 'logging.auth.username.update.warning',
JSON.stringify({ JSON.stringify({
command, command,
@ -51,13 +51,13 @@ export class UpdateUsernameUseCase {
throw error; throw error;
} }
} }
const addUsernameRequest = this._mapper.map( const addUsernameRequest = this.mapper.map(
command.updateUsernameRequest, command.updateUsernameRequest,
UpdateUsernameRequest, UpdateUsernameRequest,
AddUsernameRequest, AddUsernameRequest,
); );
try { try {
return await this._commandBus.execute( return await this.commandBus.execute(
new AddUsernameCommand(addUsernameRequest), new AddUsernameCommand(addUsernameRequest),
); );
} catch (e) { } catch (e) {

View File

@ -10,8 +10,8 @@ import { Username } from '../entities/username';
@QueryHandler(ValidateAuthenticationQuery) @QueryHandler(ValidateAuthenticationQuery)
export class ValidateAuthenticationUseCase { export class ValidateAuthenticationUseCase {
constructor( constructor(
private readonly _authenticationRepository: AuthenticationRepository, private readonly authenticationRepository: AuthenticationRepository,
private readonly _usernameRepository: UsernameRepository, private readonly usernameRepository: UsernameRepository,
) {} ) {}
execute = async ( execute = async (
@ -19,14 +19,14 @@ export class ValidateAuthenticationUseCase {
): Promise<Authentication> => { ): Promise<Authentication> => {
let username = new Username(); let username = new Username();
try { try {
username = await this._usernameRepository.findOne({ username = await this.usernameRepository.findOne({
username: validate.username, username: validate.username,
}); });
} catch (e) { } catch (e) {
throw new NotFoundException(); throw new NotFoundException();
} }
try { try {
const auth = await this._authenticationRepository.findOne({ const auth = await this.authenticationRepository.findOne({
uuid: username.uuid, uuid: username.uuid,
}); });
if (auth) { if (auth) {

View File

@ -12,8 +12,8 @@ import { Authorization } from '../../domain/entities/authorization';
@Injectable() @Injectable()
export class OpaDecisionMaker extends IMakeDecision { export class OpaDecisionMaker extends IMakeDecision {
constructor( constructor(
private readonly _configService: ConfigService, private readonly configService: ConfigService,
private readonly _httpService: HttpService, private readonly httpService: HttpService,
) { ) {
super(); super();
} }
@ -29,8 +29,8 @@ export class OpaDecisionMaker extends IMakeDecision {
); );
try { try {
const { data } = await lastValueFrom( const { data } = await lastValueFrom(
this._httpService.post<Decision>( this.httpService.post<Decision>(
this._configService.get<string>('OPA_URL') + domain + '/' + action, this.configService.get<string>('OPA_URL') + domain + '/' + action,
{ {
input: { input: {
...reducedContext, ...reducedContext,

View File

@ -20,7 +20,7 @@ interface HealthCheckResponse {
@Controller() @Controller()
export class HealthServerController { export class HealthServerController {
constructor( constructor(
private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase,
) {} ) {}
@GrpcMethod('Health', 'Check') @GrpcMethod('Health', 'Check')
@ -30,7 +30,7 @@ export class HealthServerController {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
metadata: any, metadata: any,
): Promise<HealthCheckResponse> { ): Promise<HealthCheckResponse> {
const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy( const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy(
'prisma', 'prisma',
); );
return { return {