user/src/modules/users/domain/usecases/delete-user.usecase.ts

27 lines
778 B
TypeScript
Raw Normal View History

2022-12-14 11:49:43 +00:00
import { CommandHandler } from '@nestjs/cqrs';
2022-12-22 13:24:51 +00:00
import { UserMessager } from '../../adapters/secondaries/user.messager';
2022-12-14 11:49:43 +00:00
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command';
2022-12-22 13:24:51 +00:00
import { User } from '../entities/user';
2022-12-14 11:49:43 +00:00
@CommandHandler(DeleteUserCommand)
export class DeleteUserUseCase {
2022-12-22 13:24:51 +00:00
constructor(
private readonly _repository: UsersRepository,
private readonly _messager: UserMessager,
) {}
2022-12-14 11:49:43 +00:00
2022-12-22 13:24:51 +00:00
async execute(command: DeleteUserCommand): Promise<User> {
const user = await this._repository.delete(command.uuid);
if (user) {
this._messager.publish(
'user.delete',
JSON.stringify({ uuid: user.uuid }),
);
}
return user;
2022-12-14 11:49:43 +00:00
}
}