refactor use cases
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
*/
|
||||
|
||||
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { ISelectAvatarPresenter } from '../presenters/ISelectAvatarPresenter';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface SelectAvatarInput {
|
||||
requestId: string;
|
||||
@@ -14,47 +15,48 @@ export interface SelectAvatarInput {
|
||||
}
|
||||
|
||||
export interface SelectAvatarResult {
|
||||
success: boolean;
|
||||
selectedAvatarUrl?: string;
|
||||
errorMessage?: string;
|
||||
requestId: string;
|
||||
selectedAvatarUrl: string;
|
||||
}
|
||||
|
||||
export interface ISelectAvatarPresenter {
|
||||
present(result: SelectAvatarResult): void;
|
||||
}
|
||||
export type SelectAvatarErrorCode =
|
||||
| 'REQUEST_NOT_FOUND'
|
||||
| 'REQUEST_NOT_COMPLETED'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export type SelectAvatarApplicationError = ApplicationErrorCode<
|
||||
SelectAvatarErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class SelectAvatarUseCase {
|
||||
constructor(
|
||||
private readonly avatarRepo: IAvatarGenerationRepository,
|
||||
private readonly output: UseCaseOutputPort<SelectAvatarResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: SelectAvatarInput,
|
||||
presenter: ISelectAvatarPresenter,
|
||||
): Promise<void> {
|
||||
try {
|
||||
this.logger.info('[SelectAvatarUseCase] Selecting avatar', {
|
||||
requestId: input.requestId,
|
||||
selectedIndex: input.selectedIndex,
|
||||
});
|
||||
async execute(input: SelectAvatarInput): Promise<Result<void, SelectAvatarApplicationError>> {
|
||||
this.logger.info('[SelectAvatarUseCase] Selecting avatar', {
|
||||
requestId: input.requestId,
|
||||
selectedIndex: input.selectedIndex,
|
||||
});
|
||||
|
||||
try {
|
||||
const request = await this.avatarRepo.findById(input.requestId);
|
||||
|
||||
if (!request) {
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Avatar generation request not found',
|
||||
return Result.err({
|
||||
code: 'REQUEST_NOT_FOUND',
|
||||
details: { message: 'Avatar generation request not found' },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.status !== 'completed') {
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Avatar generation is not completed yet',
|
||||
return Result.err({
|
||||
code: 'REQUEST_NOT_COMPLETED',
|
||||
details: { message: 'Avatar generation is not completed yet' },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
request.selectAvatar(input.selectedIndex);
|
||||
@@ -62,8 +64,8 @@ export class SelectAvatarUseCase {
|
||||
|
||||
const selectedAvatarUrl = request.selectedAvatarUrl;
|
||||
|
||||
presenter.present({
|
||||
success: true,
|
||||
this.output.present({
|
||||
requestId: input.requestId,
|
||||
selectedAvatarUrl,
|
||||
});
|
||||
|
||||
@@ -72,15 +74,17 @@ export class SelectAvatarUseCase {
|
||||
selectedAvatarUrl,
|
||||
});
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('[SelectAvatarUseCase] Error selecting avatar', {
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
this.logger.error('[SelectAvatarUseCase] Error selecting avatar', err, {
|
||||
requestId: input.requestId,
|
||||
});
|
||||
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Internal error occurred while selecting avatar',
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Unexpected repository error' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user