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

37 lines
1.2 KiB
TypeScript

import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command';
import { User } from '../entities/user';
@CommandHandler(DeleteUserCommand)
export class DeleteUserUseCase {
constructor(
private readonly _repository: UsersRepository,
private readonly _userMessager: UserMessager,
private readonly _loggingMessager: LoggingMessager,
) {}
async execute(command: DeleteUserCommand): Promise<User> {
try {
const user = await this._repository.delete(command.uuid);
this._userMessager.publish('delete', JSON.stringify({ uuid: user.uuid }));
this._loggingMessager.publish(
'delete',
JSON.stringify({ uuid: user.uuid }),
);
return user;
} catch (error) {
this._loggingMessager.publish(
'user.delete.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
}
}
}