Files
gridpilot.gg/core/identity/application/use-cases/GetCurrentUserSessionUseCase.ts
2026-01-08 15:34:51 +01:00

46 lines
1.4 KiB
TypeScript

import type { AuthSession, IdentitySessionPort } from '../ports/IdentitySessionPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger } from '@core/shared/application';
export type GetCurrentUserSessionInput = void;
export type GetCurrentUserSessionResult = AuthSession | null;
export type GetCurrentUserSessionErrorCode = 'REPOSITORY_ERROR';
export type GetCurrentUserSessionApplicationError = ApplicationErrorCode<
GetCurrentUserSessionErrorCode,
{ message: string }
>;
export class GetCurrentUserSessionUseCase {
constructor(
private readonly sessionPort: IdentitySessionPort,
private readonly logger: Logger,
) {}
async execute(): Promise<Result<GetCurrentUserSessionResult, GetCurrentUserSessionApplicationError>> {
try {
const session = await this.sessionPort.getCurrentSession();
return Result.ok(session);
} catch (error) {
const message =
error instanceof Error && error.message
? error.message
: 'Failed to execute GetCurrentUserSessionUseCase';
this.logger.error(
'GetCurrentUserSessionUseCase.execute failed',
error instanceof Error ? error : undefined,
{},
);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
} as GetCurrentUserSessionApplicationError);
}
}
}