39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { UnauthorizedException } from '@nestjs/common';
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
|
import { Messager } from '../../adapters/secondaries/messager';
|
|
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
|
import { DeleteUsernameCommand } from '../../commands/delete-username.command';
|
|
|
|
@CommandHandler(DeleteUsernameCommand)
|
|
export class DeleteUsernameUseCase {
|
|
constructor(
|
|
private readonly _usernameRepository: UsernameRepository,
|
|
private readonly _messager: Messager,
|
|
) {}
|
|
|
|
async execute(command: DeleteUsernameCommand) {
|
|
try {
|
|
const { username } = command.deleteUsernameRequest;
|
|
const usernameFound = await this._usernameRepository.findOne({
|
|
username,
|
|
});
|
|
const usernames = await this._usernameRepository.findAll(1, 1, {
|
|
uuid: usernameFound.uuid,
|
|
});
|
|
if (usernames.total > 1) {
|
|
return await this._usernameRepository.deleteMany({ username });
|
|
}
|
|
throw new UnauthorizedException();
|
|
} catch (error) {
|
|
this._messager.publish(
|
|
'logging.auth.username.delete.warning',
|
|
JSON.stringify({
|
|
command,
|
|
error,
|
|
}),
|
|
);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|