import { User } from '../../domain/entities/User'; import { IUserRepository } from '../../domain/repositories/IUserRepository'; import { Result } from '@core/shared/application/Result'; import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode'; import type { Logger, UseCase } from '@core/shared/application'; export type GetUserInput = { userId: string; }; export type GetUserResult = { user: User; }; export type GetUserErrorCode = 'USER_NOT_FOUND' | 'REPOSITORY_ERROR'; export type GetUserApplicationError = ApplicationErrorCode< GetUserErrorCode, { message: string } >; export class GetUserUseCase implements UseCase { constructor( private readonly userRepo: IUserRepository, private readonly logger: Logger, ) {} async execute(input: GetUserInput): Promise> { try { const stored = await this.userRepo.findById(input.userId); if (!stored) { return Result.err({ code: 'USER_NOT_FOUND', details: { message: 'User not found' }, }); } const user = User.fromStored(stored); return Result.ok({ user }); } catch (error) { const message = error instanceof Error && error.message ? error.message : 'Failed to get user'; this.logger.error('GetUserUseCase.execute failed', error instanceof Error ? error : undefined, { input, }); return Result.err({ code: 'REPOSITORY_ERROR', details: { message }, }); } } }