2022-12-14 11:49:43 +00:00
|
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
2022-12-23 14:14:51 +00:00
|
|
|
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
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,
|
2022-12-23 14:14:51 +00:00
|
|
|
private readonly _userMessager: UserMessager,
|
|
|
|
private readonly _loggingMessager: LoggingMessager,
|
2022-12-22 13:24:51 +00:00
|
|
|
) {}
|
2022-12-14 11:49:43 +00:00
|
|
|
|
2022-12-22 13:24:51 +00:00
|
|
|
async execute(command: DeleteUserCommand): Promise<User> {
|
2022-12-23 14:14:51 +00:00
|
|
|
try {
|
|
|
|
const user = await this._repository.delete(command.uuid);
|
|
|
|
this._userMessager.publish('delete', JSON.stringify({ uuid: user.uuid }));
|
|
|
|
this._loggingMessager.publish(
|
|
|
|
'delete',
|
2022-12-22 13:24:51 +00:00
|
|
|
JSON.stringify({ uuid: user.uuid }),
|
|
|
|
);
|
2022-12-23 14:14:51 +00:00
|
|
|
return user;
|
|
|
|
} catch (error) {
|
|
|
|
this._loggingMessager.publish(
|
|
|
|
'user.delete.critical',
|
|
|
|
JSON.stringify({
|
|
|
|
command,
|
|
|
|
error,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
throw error;
|
2022-12-22 13:24:51 +00:00
|
|
|
}
|
2022-12-14 11:49:43 +00:00
|
|
|
}
|
|
|
|
}
|