authorization presenter

This commit is contained in:
Gsk54
2023-01-18 11:30:37 +01:00
parent 1d2e7da673
commit 7dc6e7795f
12 changed files with 93 additions and 26 deletions

View File

@@ -21,7 +21,7 @@ import { Authentication } from '../../domain/entities/authentication';
import { Username } from '../../domain/entities/username';
import { ValidateAuthenticationQuery } from '../../queries/validate-authentication.query';
import { AuthenticationPresenter } from './authentication.presenter';
import { RpcValidationPipe } from './rpc.validation-pipe';
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
import { UsernamePresenter } from './username.presenter';
@UsePipes(
@@ -43,10 +43,14 @@ export class AuthenticationController {
data: ValidateAuthenticationRequest,
): Promise<AuthenticationPresenter> {
try {
const auth: Authentication = await this._queryBus.execute(
const authentication: Authentication = await this._queryBus.execute(
new ValidateAuthenticationQuery(data.username, data.password),
);
return this._mapper.map(auth, Authentication, AuthenticationPresenter);
return this._mapper.map(
authentication,
Authentication,
AuthenticationPresenter,
);
} catch (e) {
throw new RpcException({
code: 7,
@@ -60,10 +64,14 @@ export class AuthenticationController {
data: CreateAuthenticationRequest,
): Promise<AuthenticationPresenter> {
try {
const auth: Authentication = await this._commandBus.execute(
const authentication: Authentication = await this._commandBus.execute(
new CreateAuthenticationCommand(data),
);
return this._mapper.map(auth, Authentication, AuthenticationPresenter);
return this._mapper.map(
authentication,
Authentication,
AuthenticationPresenter,
);
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('Already exists')) {
@@ -135,11 +143,15 @@ export class AuthenticationController {
data: UpdatePasswordRequest,
): Promise<AuthenticationPresenter> {
try {
const auth: Authentication = await this._commandBus.execute(
const authentication: Authentication = await this._commandBus.execute(
new UpdatePasswordCommand(data),
);
return this._mapper.map(auth, Authentication, AuthenticationPresenter);
return this._mapper.map(
authentication,
Authentication,
AuthenticationPresenter,
);
} catch (e) {
throw new RpcException({
code: 7,

View File

@@ -1,14 +0,0 @@
import { Injectable, ValidationPipe } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
@Injectable()
export class RpcValidationPipe extends ValidationPipe {
createExceptionFactory() {
return (validationErrors = []) => {
return new RpcException({
code: 3,
message: this.flattenValidationErrors(validationErrors),
});
};
}
}