From 0937b70b485bc424e504a11e424f5e23555961b6 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 10 Oct 2023 12:05:49 +0200 Subject: [PATCH] register user with id --- .../create-user/create-user.command.ts | 2 ++ .../create-user/create-user.service.ts | 1 + src/modules/user/core/domain/user.entity.ts | 2 +- src/modules/user/core/domain/user.types.ts | 1 + .../create-user.grpc.controller.ts | 12 ++++++++++-- .../dtos/create-user.request.dto.ts | 18 ++++++++++++++---- .../user/tests/unit/core/user.entity.spec.ts | 15 +++++++++++++++ 7 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/modules/user/core/application/commands/create-user/create-user.command.ts b/src/modules/user/core/application/commands/create-user/create-user.command.ts index d7bb7c6..a066343 100644 --- a/src/modules/user/core/application/commands/create-user/create-user.command.ts +++ b/src/modules/user/core/application/commands/create-user/create-user.command.ts @@ -1,6 +1,7 @@ import { Command, CommandProps } from '@mobicoop/ddd-library'; export class CreateUserCommand extends Command { + readonly userId?: string; readonly firstName?: string; readonly lastName?: string; readonly email?: string; @@ -8,6 +9,7 @@ export class CreateUserCommand extends Command { constructor(props: CommandProps) { super(props); + this.userId = props.userId; this.firstName = props.firstName; this.lastName = props.lastName; this.email = props.email; diff --git a/src/modules/user/core/application/commands/create-user/create-user.service.ts b/src/modules/user/core/application/commands/create-user/create-user.service.ts index e564340..4166f7c 100644 --- a/src/modules/user/core/application/commands/create-user/create-user.service.ts +++ b/src/modules/user/core/application/commands/create-user/create-user.service.ts @@ -24,6 +24,7 @@ export class CreateUserService implements ICommandHandler { async execute(command: CreateUserCommand): Promise { const user = UserEntity.create({ + id: command.userId, firstName: command.firstName, lastName: command.lastName, email: command.email, diff --git a/src/modules/user/core/domain/user.entity.ts b/src/modules/user/core/domain/user.entity.ts index 593bf43..3eb77b2 100644 --- a/src/modules/user/core/domain/user.entity.ts +++ b/src/modules/user/core/domain/user.entity.ts @@ -9,7 +9,7 @@ export class UserEntity extends AggregateRoot { protected readonly _id: AggregateID; static create = (create: CreateUserProps): UserEntity => { - const id = v4(); + const id = create.id ?? v4(); const props: UserProps = { ...create }; const user = new UserEntity({ id, props }); diff --git a/src/modules/user/core/domain/user.types.ts b/src/modules/user/core/domain/user.types.ts index 1cf4f95..4410ab3 100644 --- a/src/modules/user/core/domain/user.types.ts +++ b/src/modules/user/core/domain/user.types.ts @@ -8,6 +8,7 @@ export interface UserProps { // Properties that are needed for a User creation export interface CreateUserProps { + id?: string; firstName?: string; lastName?: string; email?: string; diff --git a/src/modules/user/interface/grpc-controllers/create-user.grpc.controller.ts b/src/modules/user/interface/grpc-controllers/create-user.grpc.controller.ts index 15ce502..1aa077a 100644 --- a/src/modules/user/interface/grpc-controllers/create-user.grpc.controller.ts +++ b/src/modules/user/interface/grpc-controllers/create-user.grpc.controller.ts @@ -24,10 +24,18 @@ export class CreateUserGrpcController { constructor(private readonly commandBus: CommandBus) {} @GrpcMethod('UserService', 'Create') - async create(data: CreateUserRequestDto): Promise { + async create( + createUserRequestDto: CreateUserRequestDto, + ): Promise { try { const aggregateID: AggregateID = await this.commandBus.execute( - new CreateUserCommand(data), + new CreateUserCommand({ + firstName: createUserRequestDto.firstName, + lastName: createUserRequestDto.lastName, + email: createUserRequestDto.email, + phone: createUserRequestDto.phone, + userId: createUserRequestDto.id, + }), ); return new IdResponse(aggregateID); } catch (error: any) { diff --git a/src/modules/user/interface/grpc-controllers/dtos/create-user.request.dto.ts b/src/modules/user/interface/grpc-controllers/dtos/create-user.request.dto.ts index 9736173..12ed480 100644 --- a/src/modules/user/interface/grpc-controllers/dtos/create-user.request.dto.ts +++ b/src/modules/user/interface/grpc-controllers/dtos/create-user.request.dto.ts @@ -1,13 +1,23 @@ -import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; +import { + IsEmail, + IsOptional, + IsPhoneNumber, + IsString, + IsUUID, +} from 'class-validator'; export class CreateUserRequestDto { - @IsString() + @IsUUID(4) @IsOptional() - firstName: string; + id?: string; @IsString() @IsOptional() - lastName: string; + firstName?: string; + + @IsString() + @IsOptional() + lastName?: string; @IsEmail() @IsOptional() diff --git a/src/modules/user/tests/unit/core/user.entity.spec.ts b/src/modules/user/tests/unit/core/user.entity.spec.ts index bd32b45..2f7e2af 100644 --- a/src/modules/user/tests/unit/core/user.entity.spec.ts +++ b/src/modules/user/tests/unit/core/user.entity.spec.ts @@ -14,6 +14,14 @@ const createUserProps: CreateUserProps = { phone: '+33611223344', }; +const createUserPropsWithId: CreateUserProps = { + id: '66378860-cb75-40de-b6eb-061faf923fee', + firstName: 'John', + lastName: 'Doe', + email: 'john.doe@email.com', + phone: '+33611223344', +}; + const updateUserProps: UpdateUserProps = { firstName: 'Jane', lastName: 'Dane', @@ -28,6 +36,13 @@ describe('User entity create', () => { expect(userEntity.domainEvents.length).toBe(1); expect(userEntity.domainEvents[0]).toBeInstanceOf(UserCreatedDomainEvent); }); + it('should create a new user entity with a given id', async () => { + const userEntity: UserEntity = UserEntity.create(createUserPropsWithId); + expect(userEntity.id).toBe('66378860-cb75-40de-b6eb-061faf923fee'); + expect(userEntity.getProps().email).toBe('john.doe@email.com'); + expect(userEntity.domainEvents.length).toBe(1); + expect(userEntity.domainEvents[0]).toBeInstanceOf(UserCreatedDomainEvent); + }); }); describe('User entity update', () => {