refactor use cases
This commit is contained in:
@@ -5,55 +5,53 @@
|
||||
*/
|
||||
|
||||
import type { IAvatarRepository } from '../../domain/repositories/IAvatarRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { IGetAvatarPresenter } from '../presenters/IGetAvatarPresenter';
|
||||
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 GetAvatarInput {
|
||||
driverId: string;
|
||||
}
|
||||
|
||||
export interface GetAvatarResult {
|
||||
success: boolean;
|
||||
avatar?: {
|
||||
avatar: {
|
||||
id: string;
|
||||
driverId: string;
|
||||
mediaUrl: string;
|
||||
selectedAt: Date;
|
||||
};
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
export interface IGetAvatarPresenter {
|
||||
present(result: GetAvatarResult): void;
|
||||
}
|
||||
export type GetAvatarErrorCode = 'AVATAR_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type GetAvatarApplicationError = ApplicationErrorCode<
|
||||
GetAvatarErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class GetAvatarUseCase {
|
||||
constructor(
|
||||
private readonly avatarRepo: IAvatarRepository,
|
||||
private readonly output: UseCaseOutputPort<GetAvatarResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetAvatarInput,
|
||||
presenter: IGetAvatarPresenter,
|
||||
): Promise<void> {
|
||||
try {
|
||||
this.logger.info('[GetAvatarUseCase] Getting avatar', {
|
||||
driverId: input.driverId,
|
||||
});
|
||||
async execute(input: GetAvatarInput): Promise<Result<void, GetAvatarApplicationError>> {
|
||||
this.logger.info('[GetAvatarUseCase] Getting avatar', {
|
||||
driverId: input.driverId,
|
||||
});
|
||||
|
||||
try {
|
||||
const avatar = await this.avatarRepo.findActiveByDriverId(input.driverId);
|
||||
|
||||
if (!avatar) {
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Avatar not found',
|
||||
return Result.err({
|
||||
code: 'AVATAR_NOT_FOUND',
|
||||
details: { message: 'Avatar not found' },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
presenter.present({
|
||||
success: true,
|
||||
this.output.present({
|
||||
avatar: {
|
||||
id: avatar.id,
|
||||
driverId: avatar.driverId,
|
||||
@@ -62,15 +60,17 @@ export class GetAvatarUseCase {
|
||||
},
|
||||
});
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('[GetAvatarUseCase] Error getting avatar', {
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
this.logger.error('[GetAvatarUseCase] Error getting avatar', err, {
|
||||
driverId: input.driverId,
|
||||
});
|
||||
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Internal error occurred while retrieving avatar',
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Unexpected repository error' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user