refactor use cases

This commit is contained in:
2025-12-21 01:20:27 +01:00
parent c12656d671
commit 8ecd638396
39 changed files with 2523 additions and 686 deletions

View File

@@ -1,84 +1,145 @@
/**
* Signup with Email Use Case
*
*
* Creates a new user account with email and password.
*/
import type { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
import type { AuthenticatedUserDTO } from '../dto/AuthenticatedUserDTO';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
export interface SignupCommandDTO {
export type SignupWithEmailInput = {
email: string;
password: string;
displayName: string;
}
};
export interface SignupResultDTO {
session: AuthSessionDTO;
export type SignupWithEmailResult = {
sessionToken: string;
userId: string;
displayName: string;
email: string;
createdAt: Date;
isNewUser: boolean;
}
};
export type SignupWithEmailErrorCode =
| 'INVALID_EMAIL_FORMAT'
| 'WEAK_PASSWORD'
| 'INVALID_DISPLAY_NAME'
| 'EMAIL_ALREADY_EXISTS'
| 'REPOSITORY_ERROR';
export type SignupWithEmailApplicationError = ApplicationErrorCode<
SignupWithEmailErrorCode,
{ message: string }
>;
export class SignupWithEmailUseCase {
constructor(
private readonly userRepository: IUserRepository,
private readonly sessionPort: IdentitySessionPort,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<SignupWithEmailResult>,
) {}
async execute(command: SignupCommandDTO): Promise<SignupResultDTO> {
async execute(input: SignupWithEmailInput): Promise<
Result<void, SignupWithEmailApplicationError>
> {
// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(command.email)) {
throw new Error('Invalid email format');
if (!emailRegex.test(input.email)) {
return Result.err({
code: 'INVALID_EMAIL_FORMAT',
details: { message: 'Invalid email format' },
} as SignupWithEmailApplicationError);
}
// Validate password strength
if (command.password.length < 8) {
throw new Error('Password must be at least 8 characters');
if (input.password.length < 8) {
return Result.err({
code: 'WEAK_PASSWORD',
details: { message: 'Password must be at least 8 characters' },
} as SignupWithEmailApplicationError);
}
// Validate display name
if (!command.displayName || command.displayName.trim().length < 2) {
throw new Error('Display name must be at least 2 characters');
if (!input.displayName || input.displayName.trim().length < 2) {
return Result.err({
code: 'INVALID_DISPLAY_NAME',
details: { message: 'Display name must be at least 2 characters' },
} as SignupWithEmailApplicationError);
}
// Check if email already exists
const existingUser = await this.userRepository.findByEmail(command.email);
const existingUser = await this.userRepository.findByEmail(input.email);
if (existingUser) {
throw new Error('An account with this email already exists');
return Result.err({
code: 'EMAIL_ALREADY_EXISTS',
details: { message: 'An account with this email already exists' },
} as SignupWithEmailApplicationError);
}
// Hash password (simple hash for demo - in production use bcrypt)
const salt = this.generateSalt();
const passwordHash = await this.hashPassword(command.password, salt);
try {
// Hash password (simple hash for demo - in production use bcrypt)
const salt = this.generateSalt();
const passwordHash = await this.hashPassword(input.password, salt);
// Create user
const userId = this.generateUserId();
const newUser: StoredUser = {
id: userId,
email: command.email.toLowerCase().trim(),
displayName: command.displayName.trim(),
passwordHash,
salt,
createdAt: new Date(),
};
// Create user
const userId = this.generateUserId();
const createdAt = new Date();
const newUser: StoredUser = {
id: userId,
email: input.email.toLowerCase().trim(),
displayName: input.displayName.trim(),
passwordHash,
salt,
createdAt,
};
await this.userRepository.create(newUser);
await this.userRepository.create(newUser);
// Create session
const authenticatedUser: AuthenticatedUserDTO = {
id: newUser.id,
displayName: newUser.displayName,
email: newUser.email,
};
// Create session
const authenticatedUser: AuthenticatedUserDTO = {
id: newUser.id,
displayName: newUser.displayName,
email: newUser.email,
};
const session = await this.sessionPort.createSession(authenticatedUser);
const session = await this.sessionPort.createSession(authenticatedUser);
return {
session,
isNewUser: true,
};
const result: SignupWithEmailResult = {
sessionToken: session.token,
userId: session.user.id,
displayName: session.user.displayName,
email: session.user.email ?? newUser.email,
createdAt,
isNewUser: true,
};
this.output.present(result);
return Result.ok(undefined);
} catch (error) {
const message =
error instanceof Error && error.message
? error.message
: 'Failed to execute SignupWithEmailUseCase';
this.logger.error(
'SignupWithEmailUseCase.execute failed',
error instanceof Error ? error : undefined,
{ input },
);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
} as SignupWithEmailApplicationError);
}
}
private generateSalt(): string {