refactor use cases
This commit is contained in:
@@ -3,19 +3,55 @@ import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
|
||||
import type { AuthenticatedUserDTO } from '../dto/AuthenticatedUserDTO';
|
||||
import type { IdentityProviderPort } from '../ports/IdentityProviderPort';
|
||||
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 HandleAuthCallbackInput = AuthCallbackCommandDTO;
|
||||
|
||||
export type HandleAuthCallbackResult = AuthSessionDTO;
|
||||
|
||||
export type HandleAuthCallbackErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export type HandleAuthCallbackApplicationError = ApplicationErrorCode<
|
||||
HandleAuthCallbackErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class HandleAuthCallbackUseCase {
|
||||
private readonly provider: IdentityProviderPort;
|
||||
private readonly sessionPort: IdentitySessionPort;
|
||||
constructor(
|
||||
private readonly provider: IdentityProviderPort,
|
||||
private readonly sessionPort: IdentitySessionPort,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<HandleAuthCallbackResult>,
|
||||
) {}
|
||||
|
||||
constructor(provider: IdentityProviderPort, sessionPort: IdentitySessionPort) {
|
||||
this.provider = provider;
|
||||
this.sessionPort = sessionPort;
|
||||
}
|
||||
async execute(input: HandleAuthCallbackInput): Promise<
|
||||
Result<void, HandleAuthCallbackApplicationError>
|
||||
> {
|
||||
try {
|
||||
const user: AuthenticatedUserDTO = await this.provider.completeAuth(input);
|
||||
const session = await this.sessionPort.createSession(user);
|
||||
|
||||
async execute(command: AuthCallbackCommandDTO): Promise<AuthSessionDTO> {
|
||||
const user: AuthenticatedUserDTO = await this.provider.completeAuth(command);
|
||||
const session = await this.sessionPort.createSession(user);
|
||||
return session;
|
||||
this.output.present(session);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to execute HandleAuthCallbackUseCase';
|
||||
|
||||
this.logger.error(
|
||||
'HandleAuthCallbackUseCase.execute failed',
|
||||
error instanceof Error ? error : undefined,
|
||||
{ input },
|
||||
);
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as HandleAuthCallbackApplicationError);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user