refactor
This commit is contained in:
@@ -2,7 +2,7 @@ import { User } from '../../domain/entities/User';
|
||||
import { IUserRepository } from '../../domain/repositories/IUserRepository';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort, Logger, UseCase } from '@core/shared/application';
|
||||
|
||||
export type GetUserInput = {
|
||||
userId: string;
|
||||
@@ -19,28 +19,29 @@ export type GetUserApplicationError = ApplicationErrorCode<
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class GetUserUseCase {
|
||||
export class GetUserUseCase implements UseCase<GetUserInput, GetUserResult, GetUserErrorCode> {
|
||||
constructor(
|
||||
private readonly userRepo: IUserRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetUserResult>,
|
||||
private readonly output: UseCaseOutputPort<Result<GetUserResult, GetUserApplicationError>>,
|
||||
) {}
|
||||
|
||||
async execute(input: GetUserInput): Promise<Result<void, GetUserApplicationError>> {
|
||||
async execute(input: GetUserInput): Promise<Result<GetUserResult, GetUserApplicationError>> {
|
||||
try {
|
||||
const stored = await this.userRepo.findById(input.userId);
|
||||
if (!stored) {
|
||||
return Result.err({
|
||||
const result = Result.err<GetUserResult, GetUserApplicationError>({
|
||||
code: 'USER_NOT_FOUND',
|
||||
details: { message: 'User not found' },
|
||||
} as GetUserApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const user = User.fromStored(stored);
|
||||
const result: GetUserResult = { user };
|
||||
const result = Result.ok<GetUserResult, GetUserApplicationError>({ user });
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
return result;
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message ? error.message : 'Failed to get user';
|
||||
@@ -49,10 +50,12 @@ export class GetUserUseCase {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
const result = Result.err<GetUserResult, GetUserApplicationError>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as GetUserApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ 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';
|
||||
import type { UseCaseOutputPort, Logger, UseCase } from '@core/shared/application';
|
||||
|
||||
export type LoginInput = {
|
||||
email: string;
|
||||
@@ -24,40 +24,43 @@ export type LoginApplicationError = ApplicationErrorCode<LoginErrorCode, { messa
|
||||
*
|
||||
* Handles user login by verifying credentials.
|
||||
*/
|
||||
export class LoginUseCase {
|
||||
export class LoginUseCase implements UseCase<LoginInput, LoginResult, LoginErrorCode> {
|
||||
constructor(
|
||||
private readonly authRepo: IAuthRepository,
|
||||
private readonly passwordService: IPasswordHashingService,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<LoginResult>,
|
||||
private readonly output: UseCaseOutputPort<Result<LoginResult, LoginApplicationError>>,
|
||||
) {}
|
||||
|
||||
async execute(input: LoginInput): Promise<Result<void, LoginApplicationError>> {
|
||||
async execute(input: LoginInput): Promise<Result<LoginResult, LoginApplicationError>> {
|
||||
try {
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
const user = await this.authRepo.findByEmail(emailVO);
|
||||
|
||||
if (!user || !user.getPasswordHash()) {
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const passwordHash = user.getPasswordHash()!;
|
||||
const isValid = await this.passwordService.verify(input.password, passwordHash.value);
|
||||
|
||||
if (!isValid) {
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const result: LoginResult = { user };
|
||||
const result = Result.ok<LoginResult, LoginApplicationError>({ user });
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
return result;
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
@@ -68,10 +71,12 @@ export class LoginUseCase {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ 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';
|
||||
import type { UseCaseOutputPort, Logger, UseCase } from '@core/shared/application';
|
||||
|
||||
export type SignupInput = {
|
||||
email: string;
|
||||
@@ -26,24 +26,26 @@ export type SignupApplicationError = ApplicationErrorCode<SignupErrorCode, { mes
|
||||
*
|
||||
* Handles user registration.
|
||||
*/
|
||||
export class SignupUseCase {
|
||||
export class SignupUseCase implements UseCase<SignupInput, SignupResult, SignupErrorCode> {
|
||||
constructor(
|
||||
private readonly authRepo: IAuthRepository,
|
||||
private readonly passwordService: IPasswordHashingService,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<SignupResult>,
|
||||
private readonly output: UseCaseOutputPort<Result<SignupResult, SignupApplicationError>>,
|
||||
) {}
|
||||
|
||||
async execute(input: SignupInput): Promise<Result<void, SignupApplicationError>> {
|
||||
async execute(input: SignupInput): Promise<Result<SignupResult, SignupApplicationError>> {
|
||||
try {
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
|
||||
const existingUser = await this.authRepo.findByEmail(emailVO);
|
||||
if (existingUser) {
|
||||
return Result.err({
|
||||
const result = Result.err<SignupResult, SignupApplicationError>({
|
||||
code: 'USER_ALREADY_EXISTS',
|
||||
details: { message: 'User already exists' },
|
||||
} as SignupApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const hashedPassword = await this.passwordService.hash(input.password);
|
||||
@@ -60,10 +62,9 @@ export class SignupUseCase {
|
||||
|
||||
await this.authRepo.save(user);
|
||||
|
||||
const result: SignupResult = { user };
|
||||
const result = Result.ok<SignupResult, SignupApplicationError>({ user });
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
return result;
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
@@ -74,10 +75,12 @@ export class SignupUseCase {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
const result = Result.err<SignupResult, SignupApplicationError>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as SignupApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user