refactor use cases
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
@@ -32,11 +32,10 @@ export type SelectAvatarApplicationError = ApplicationErrorCode<
|
||||
export class SelectAvatarUseCase {
|
||||
constructor(
|
||||
private readonly avatarRepo: IAvatarGenerationRepository,
|
||||
private readonly output: UseCaseOutputPort<SelectAvatarResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: SelectAvatarInput): Promise<Result<void, SelectAvatarApplicationError>> {
|
||||
async execute(input: SelectAvatarInput): Promise<Result<SelectAvatarResult, SelectAvatarApplicationError>> {
|
||||
this.logger.info('[SelectAvatarUseCase] Selecting avatar', {
|
||||
requestId: input.requestId,
|
||||
selectedIndex: input.selectedIndex,
|
||||
@@ -46,14 +45,14 @@ export class SelectAvatarUseCase {
|
||||
const request = await this.avatarRepo.findById(input.requestId);
|
||||
|
||||
if (!request) {
|
||||
return Result.err<void, SelectAvatarApplicationError>({
|
||||
return Result.err<SelectAvatarResult, SelectAvatarApplicationError>({
|
||||
code: 'REQUEST_NOT_FOUND',
|
||||
details: { message: 'Avatar generation request not found' },
|
||||
});
|
||||
}
|
||||
|
||||
if (request.status !== 'completed') {
|
||||
return Result.err<void, SelectAvatarApplicationError>({
|
||||
return Result.err<SelectAvatarResult, SelectAvatarApplicationError>({
|
||||
code: 'REQUEST_NOT_COMPLETED',
|
||||
details: { message: 'Avatar generation is not completed yet' },
|
||||
});
|
||||
@@ -64,17 +63,15 @@ export class SelectAvatarUseCase {
|
||||
|
||||
const selectedAvatarUrl = request.selectedAvatarUrl!;
|
||||
|
||||
this.output.present({
|
||||
requestId: input.requestId,
|
||||
selectedAvatarUrl,
|
||||
});
|
||||
|
||||
this.logger.info('[SelectAvatarUseCase] Avatar selected successfully', {
|
||||
requestId: input.requestId,
|
||||
selectedAvatarUrl,
|
||||
});
|
||||
|
||||
return Result.ok(undefined);
|
||||
return Result.ok({
|
||||
requestId: input.requestId,
|
||||
selectedAvatarUrl,
|
||||
});
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
@@ -82,7 +79,7 @@ export class SelectAvatarUseCase {
|
||||
requestId: input.requestId,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
return Result.err<SelectAvatarResult, SelectAvatarApplicationError>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Unexpected repository error' },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user