74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
/**
|
|
* Use Case: GetAvatarUseCase
|
|
*
|
|
* Handles the business logic for retrieving a driver's avatar.
|
|
*/
|
|
|
|
import type { Logger } from '@core/shared/domain/Logger';
|
|
import { Result } from '@core/shared/domain/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import { AvatarRepository } from '../../domain/repositories/AvatarRepository';
|
|
|
|
export interface GetAvatarInput {
|
|
driverId: string;
|
|
}
|
|
|
|
export interface GetAvatarResult {
|
|
avatar: {
|
|
id: string;
|
|
driverId: string;
|
|
mediaUrl: string;
|
|
selectedAt: Date;
|
|
};
|
|
}
|
|
|
|
export type GetAvatarErrorCode = 'AVATAR_NOT_FOUND' | 'REPOSITORY_ERROR';
|
|
|
|
export type GetAvatarApplicationError = ApplicationErrorCode<
|
|
GetAvatarErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
export class GetAvatarUseCase {
|
|
constructor(
|
|
private readonly avatarRepo: AvatarRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(input: GetAvatarInput): Promise<Result<GetAvatarResult, GetAvatarApplicationError>> {
|
|
this.logger.info('[GetAvatarUseCase] Getting avatar', {
|
|
driverId: input.driverId,
|
|
});
|
|
|
|
try {
|
|
const avatar = await this.avatarRepo.findActiveByDriverId(input.driverId);
|
|
|
|
if (!avatar) {
|
|
return Result.err<GetAvatarResult, GetAvatarApplicationError>({
|
|
code: 'AVATAR_NOT_FOUND',
|
|
details: { message: 'Avatar not found' },
|
|
});
|
|
}
|
|
|
|
return Result.ok({
|
|
avatar: {
|
|
id: avatar.id,
|
|
driverId: avatar.driverId,
|
|
mediaUrl: avatar.mediaUrl.value,
|
|
selectedAt: avatar.selectedAt,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
|
|
this.logger.error('[GetAvatarUseCase] Error getting avatar', err, {
|
|
driverId: input.driverId,
|
|
});
|
|
|
|
return Result.err<GetAvatarResult, GetAvatarApplicationError>({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: err.message ?? 'Unexpected repository error' },
|
|
});
|
|
}
|
|
}
|
|
} |