2022-12-14 10:37:13 +00:00
|
|
|
import { Mapper } from '@automapper/core';
|
|
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
2022-12-22 13:24:51 +00:00
|
|
|
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
2022-12-14 10:37:13 +00:00
|
|
|
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
|
|
|
import { CreateUserCommand } from '../../commands/create-user.command';
|
2022-12-16 16:31:08 +00:00
|
|
|
import { CreateUserRequest } from '../dtos/create-user.request';
|
2022-12-14 10:37:13 +00:00
|
|
|
import { User } from '../entities/user';
|
|
|
|
|
|
|
|
@CommandHandler(CreateUserCommand)
|
|
|
|
export class CreateUserUseCase {
|
|
|
|
constructor(
|
|
|
|
private readonly _repository: UsersRepository,
|
2022-12-22 13:24:51 +00:00
|
|
|
private readonly _messager: UserMessager,
|
2022-12-14 10:37:13 +00:00
|
|
|
@InjectMapper() private readonly _mapper: Mapper,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async execute(command: CreateUserCommand): Promise<User> {
|
|
|
|
const entity = this._mapper.map(
|
|
|
|
command.createUserRequest,
|
|
|
|
CreateUserRequest,
|
|
|
|
User,
|
|
|
|
);
|
|
|
|
|
2022-12-22 13:24:51 +00:00
|
|
|
const user = await this._repository.create(entity);
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
this._messager.publish('user.create', JSON.stringify(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
2022-12-14 10:37:13 +00:00
|
|
|
}
|
|
|
|
}
|