WIP handle unique constraint exception

This commit is contained in:
sbriat
2023-07-04 12:16:34 +02:00
parent 0162066557
commit f33f679e12
29 changed files with 1608 additions and 7657 deletions

View File

@@ -0,0 +1,6 @@
import { PaginatedResponseDto } from '@mobicoop/ddd-library';
import { AuthenticationResponseDto } from './authentication.response.dto';
export class AauthenticationPaginatedResponseDto extends PaginatedResponseDto<AuthenticationResponseDto> {
readonly data: readonly AuthenticationResponseDto[];
}

View File

@@ -0,0 +1,3 @@
import { ResponseBase } from '@mobicoop/ddd-library';
export class AuthenticationResponseDto extends ResponseBase {}

View File

@@ -0,0 +1,40 @@
syntax = "proto3";
package authentication;
service AuthenticationService {
rpc Validate(AuthenticationByUsernamePassword) returns (Id);
rpc Create(Authentication) returns (Id);
rpc AddUsername(Username) returns (Id);
rpc UpdatePassword(Password) returns (Id);
rpc UpdateUsername(Username) returns (Id);
rpc DeleteUsername(Username) returns (Id);
rpc Delete(Id) returns (Empty);
}
message AuthenticationByUsernamePassword {
string username = 1;
string password = 2;
}
message Authentication {
repeated Username usernames = 1;
string password = 2;
}
message Password {
string id = 1;
string password = 2;
}
message Username {
string id = 1;
string name = 2;
string type = 3;
}
message Id {
string id = 1;
}
message Empty {}

View File

@@ -0,0 +1,43 @@
import {
AggregateID,
IdResponse,
RpcExceptionCode,
RpcValidationPipe,
} from '@mobicoop/ddd-library';
import { Controller, UsePipes } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { CreateAuthenticationRequestDto } from './dtos/create-authentication.request.dto';
import { CreateAuthenticationCommand } from '@modules/newauthentication/core/application/commands/create-authentication/create-authentication.command';
import { AuthenticationAlreadyExistsException } from '@modules/newauthentication/core/domain/authentication.errors';
@UsePipes(
new RpcValidationPipe({
whitelist: true,
forbidUnknownValues: false,
}),
)
@Controller()
export class CreateAuthenticationGrpcController {
constructor(private readonly commandBus: CommandBus) {}
@GrpcMethod('AuthenticationService', 'Create')
async validate(data: CreateAuthenticationRequestDto): Promise<IdResponse> {
try {
const aggregateID: AggregateID = await this.commandBus.execute(
new CreateAuthenticationCommand(data),
);
return new IdResponse(aggregateID);
} catch (error: any) {
if (error instanceof AuthenticationAlreadyExistsException)
throw new RpcException({
code: RpcExceptionCode.ALREADY_EXISTS,
message: error.message,
});
throw new RpcException({
code: RpcExceptionCode.UNKNOWN,
message: error.message,
});
}
}
}

View File

@@ -0,0 +1,21 @@
import { Type } from 'class-transformer';
import {
ArrayMinSize,
IsArray,
IsNotEmpty,
IsString,
ValidateNested,
} from 'class-validator';
import { UsernameDto } from './username.dto';
export class CreateAuthenticationRequestDto {
@Type(() => UsernameDto)
@IsArray()
@ArrayMinSize(1)
@ValidateNested({ each: true })
usernames: UsernameDto[];
@IsString()
@IsNotEmpty()
password: string;
}

View File

@@ -0,0 +1,11 @@
import { Type } from '@modules/newauthentication/core/domain/username.types';
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
export class UsernameDto {
@IsString()
name: string;
@IsEnum(Type)
@IsNotEmpty()
type: Type;
}