refactor use cases
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
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 type GetCurrentUserSessionInput = void;
|
||||
|
||||
export type GetCurrentUserSessionResult = AuthSessionDTO | null;
|
||||
|
||||
export type GetCurrentUserSessionErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export type GetCurrentUserSessionApplicationError = ApplicationErrorCode<
|
||||
GetCurrentUserSessionErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class GetCurrentUserSessionUseCase {
|
||||
private readonly sessionPort: IdentitySessionPort;
|
||||
constructor(
|
||||
private readonly sessionPort: IdentitySessionPort,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetCurrentUserSessionResult>,
|
||||
) {}
|
||||
|
||||
constructor(sessionPort: IdentitySessionPort) {
|
||||
this.sessionPort = sessionPort;
|
||||
}
|
||||
async execute(): Promise<Result<void, GetCurrentUserSessionApplicationError>> {
|
||||
try {
|
||||
const session = await this.sessionPort.getCurrentSession();
|
||||
|
||||
async execute(): Promise<AuthSessionDTO | null> {
|
||||
return this.sessionPort.getCurrentSession();
|
||||
this.output.present(session);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user