refactor use cases
This commit is contained in:
@@ -1,6 +1,23 @@
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { IUserRepository } from '../../domain/repositories/IUserRepository';
|
||||
// No direct import of apps/api DTOs in core module
|
||||
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 GetCurrentSessionInput = {
|
||||
userId: string;
|
||||
};
|
||||
|
||||
export type GetCurrentSessionResult = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
export type GetCurrentSessionErrorCode = 'USER_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type GetCurrentSessionApplicationError = ApplicationErrorCode<
|
||||
GetCurrentSessionErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
/**
|
||||
* Application Use Case: GetCurrentSessionUseCase
|
||||
@@ -8,13 +25,45 @@ import { IUserRepository } from '../../domain/repositories/IUserRepository';
|
||||
* Retrieves the current user session information.
|
||||
*/
|
||||
export class GetCurrentSessionUseCase {
|
||||
constructor(private userRepo: IUserRepository) {}
|
||||
constructor(
|
||||
private readonly userRepo: IUserRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetCurrentSessionResult>,
|
||||
) {}
|
||||
|
||||
async execute(userId: string): Promise<User | null> {
|
||||
const stored = await this.userRepo.findById(userId);
|
||||
if (!stored) {
|
||||
return null;
|
||||
async execute(input: GetCurrentSessionInput): Promise<
|
||||
Result<void, GetCurrentSessionApplicationError>
|
||||
> {
|
||||
try {
|
||||
const stored = await this.userRepo.findById(input.userId);
|
||||
if (!stored) {
|
||||
return Result.err({
|
||||
code: 'USER_NOT_FOUND',
|
||||
details: { message: 'User not found' },
|
||||
} as GetCurrentSessionApplicationError);
|
||||
}
|
||||
|
||||
const user = User.fromStored(stored);
|
||||
const result: GetCurrentSessionResult = { user };
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to execute GetCurrentSessionUseCase';
|
||||
|
||||
this.logger.error(
|
||||
'GetCurrentSessionUseCase.execute failed',
|
||||
error instanceof Error ? error : undefined,
|
||||
{ input },
|
||||
);
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as GetCurrentSessionApplicationError);
|
||||
}
|
||||
return User.fromStored(stored);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user