refactor use cases
This commit is contained in:
@@ -3,6 +3,23 @@ import { UserId } from '../../domain/value-objects/UserId';
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { IAuthRepository } from '../../domain/repositories/IAuthRepository';
|
||||
import { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
|
||||
export type SignupInput = {
|
||||
email: string;
|
||||
password: string;
|
||||
displayName: string;
|
||||
};
|
||||
|
||||
export type SignupResult = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
export type SignupErrorCode = 'USER_ALREADY_EXISTS' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type SignupApplicationError = ApplicationErrorCode<SignupErrorCode, { message: string }>;
|
||||
|
||||
/**
|
||||
* Application Use Case: SignupUseCase
|
||||
@@ -11,31 +28,56 @@ import { IPasswordHashingService } from '../../domain/services/PasswordHashingSe
|
||||
*/
|
||||
export class SignupUseCase {
|
||||
constructor(
|
||||
private authRepo: IAuthRepository,
|
||||
private passwordService: IPasswordHashingService
|
||||
private readonly authRepo: IAuthRepository,
|
||||
private readonly passwordService: IPasswordHashingService,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<SignupResult>,
|
||||
) {}
|
||||
|
||||
async execute(email: string, password: string, displayName: string): Promise<User> {
|
||||
const emailVO = EmailAddress.create(email);
|
||||
async execute(input: SignupInput): Promise<Result<void, SignupApplicationError>> {
|
||||
try {
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await this.authRepo.findByEmail(emailVO);
|
||||
if (existingUser) {
|
||||
throw new Error('User already exists');
|
||||
const existingUser = await this.authRepo.findByEmail(emailVO);
|
||||
if (existingUser) {
|
||||
return Result.err({
|
||||
code: 'USER_ALREADY_EXISTS',
|
||||
details: { message: 'User already exists' },
|
||||
} as SignupApplicationError);
|
||||
}
|
||||
|
||||
const hashedPassword = await this.passwordService.hash(input.password);
|
||||
const passwordHashModule = await import('../../domain/value-objects/PasswordHash');
|
||||
const passwordHash = passwordHashModule.PasswordHash.fromHash(hashedPassword);
|
||||
|
||||
const userId = UserId.create();
|
||||
const user = User.create({
|
||||
id: userId,
|
||||
displayName: input.displayName,
|
||||
email: emailVO.value,
|
||||
passwordHash,
|
||||
});
|
||||
|
||||
await this.authRepo.save(user);
|
||||
|
||||
const result: SignupResult = { user };
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to execute SignupUseCase';
|
||||
|
||||
this.logger.error('SignupUseCase.execute failed', error instanceof Error ? error : undefined, {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as SignupApplicationError);
|
||||
}
|
||||
|
||||
const hashedPassword = await this.passwordService.hash(password);
|
||||
const passwordHash = await import('../../domain/value-objects/PasswordHash').then(m => m.PasswordHash.fromHash(hashedPassword));
|
||||
|
||||
const userId = UserId.create();
|
||||
const user = User.create({
|
||||
id: userId,
|
||||
displayName,
|
||||
email: emailVO.value,
|
||||
passwordHash,
|
||||
});
|
||||
|
||||
await this.authRepo.save(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user