@@ -4,16 +4,15 @@ import { ConflictException } from '@libs/exceptions';
4
4
import { Address } from '@modules/user/domain/value-objects/address.value-object' ;
5
5
import { Email } from '@modules/user/domain/value-objects/email.value-object' ;
6
6
import { UnitOfWork } from '@src/infrastructure/database/unit-of-work' ;
7
+ import { CommandHandler } from '@src/libs/ddd/domain/base-classes/command-handler.base' ;
7
8
import { CreateUserCommand } from './create-user.command' ;
8
9
import { UserEntity } from '../../domain/entities/user.entity' ;
9
10
10
- export class CreateUserService {
11
- constructor ( private readonly unitOfWork : UnitOfWork ) { }
12
-
13
- async create (
14
- command : CreateUserCommand ,
15
- userRepo : UserRepositoryPort ,
16
- ) : Promise < ID > {
11
+ export class CreateUserService extends CommandHandler < UnitOfWork > {
12
+ protected async execute ( command : CreateUserCommand ) : Promise < ID > {
13
+ const userRepo : UserRepositoryPort = this . unitOfWork . getUserRepository (
14
+ command . correlationId ,
15
+ ) ;
17
16
// user uniqueness guard
18
17
if ( await userRepo . exists ( command . email ) ) {
19
18
throw new ConflictException ( 'User already exists' ) ;
@@ -34,15 +33,4 @@ export class CreateUserService {
34
33
35
34
return created . id ;
36
35
}
37
-
38
- async execute ( command : CreateUserCommand ) : Promise < ID > {
39
- const correlationId = this . unitOfWork . init ( ) ;
40
- const userRepo : UserRepositoryPort = this . unitOfWork . getUserRepository (
41
- correlationId ,
42
- ) ;
43
- // Wrapping user creation in a UnitOfWork so events get included in a transaction
44
- return UnitOfWork . execute ( correlationId , async ( ) =>
45
- this . create ( command , userRepo ) ,
46
- ) ;
47
- }
48
36
}
0 commit comments