34 lines
980 B
TypeScript
34 lines
980 B
TypeScript
import { CommandHandler } from '@nestjs/cqrs';
|
|
import { Messager } from '../../adapters/secondaries/messager';
|
|
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
|
import { AddUsernameCommand } from '../../commands/add-username.command';
|
|
import { Username } from '../entities/username';
|
|
|
|
@CommandHandler(AddUsernameCommand)
|
|
export class AddUsernameUseCase {
|
|
constructor(
|
|
private readonly usernameRepository: UsernameRepository,
|
|
private readonly messager: Messager,
|
|
) {}
|
|
|
|
execute = async (command: AddUsernameCommand): Promise<Username> => {
|
|
const { uuid, username, type } = command.addUsernameRequest;
|
|
try {
|
|
return await this.usernameRepository.create({
|
|
uuid,
|
|
type,
|
|
username,
|
|
});
|
|
} catch (error) {
|
|
this.messager.publish(
|
|
'logging.auth.username.add.warning',
|
|
JSON.stringify({
|
|
command,
|
|
error,
|
|
}),
|
|
);
|
|
throw error;
|
|
}
|
|
};
|
|
}
|