70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
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';
|
|
|
|
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
|
|
*
|
|
* Retrieves the current user session information.
|
|
*/
|
|
export class GetCurrentSessionUseCase {
|
|
constructor(
|
|
private readonly userRepo: IUserRepository,
|
|
private readonly logger: Logger,
|
|
private readonly output: UseCaseOutputPort<GetCurrentSessionResult>,
|
|
) {}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|