diff --git a/libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts b/libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts index 650e7d2..47dbcb9 100644 --- a/libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts +++ b/libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts @@ -1,36 +1,36 @@ import { GetUserUseCase } from './get-user.use-case'; -import { UserRepository } from '@users/domain'; +import { UsersRepository } from '@users/domain'; describe('GetUserUseCase', () => { let getUserUseCase: GetUserUseCase; - let userRepository: jest.Mocked; + let usersRepository: jest.Mocked; beforeEach(() => { - userRepository = { + usersRepository = { findById: jest.fn(), - } as unknown as jest.Mocked; + } as unknown as jest.Mocked; - getUserUseCase = new GetUserUseCase(userRepository); + getUserUseCase = new GetUserUseCase(usersRepository); }); describe('execute', () => { it('should return a user when found', async () => { const user = { id: '1', name: 'John Doe' }; - userRepository.findById.mockResolvedValue(user); + usersRepository.findById.mockResolvedValue(user); const result = await getUserUseCase.execute('1'); expect(result).toEqual(user); - expect(userRepository.findById).toHaveBeenCalledWith('1'); + expect(usersRepository.findById).toHaveBeenCalledWith('1'); }); it('should return null when user is not found', async () => { - userRepository.findById.mockResolvedValue(null); + usersRepository.findById.mockResolvedValue(null); const result = await getUserUseCase.execute('1'); expect(result).toBeNull(); - expect(userRepository.findById).toHaveBeenCalledWith('1'); + expect(usersRepository.findById).toHaveBeenCalledWith('1'); }); }); }); \ No newline at end of file diff --git a/libs/users/application/src/lib/use-cases/get-user.use-case.ts b/libs/users/application/src/lib/use-cases/get-user.use-case.ts index a922928..25e328f 100644 --- a/libs/users/application/src/lib/use-cases/get-user.use-case.ts +++ b/libs/users/application/src/lib/use-cases/get-user.use-case.ts @@ -1,9 +1,19 @@ -import { User, UserRepository } from '@users/domain'; +import { Inject, Injectable } from '@nestjs/common'; +import { User, USERS_REPOSITORY, UsersRepository } from '@users/domain'; +@Injectable() export class GetUserUseCase { - constructor(private readonly userRepository: UserRepository) {} + constructor( + @Inject(USERS_REPOSITORY) + private readonly usersRepository: UsersRepository, + ) {} async execute(id: string): Promise { - return this.userRepository.findById(id); + const user = this.usersRepository.findById(id); + + if (!user) { + return null; + } + return user; } } diff --git a/libs/users/domain/src/lib/users.repository.ts b/libs/users/domain/src/lib/users.repository.ts index e80905f..4af6be0 100644 --- a/libs/users/domain/src/lib/users.repository.ts +++ b/libs/users/domain/src/lib/users.repository.ts @@ -3,3 +3,5 @@ import { User } from './user.entity'; export interface UsersRepository { findById(id: string): Promise; } + +export const USERS_REPOSITORY = Symbol('USERS_REPOSITORY'); \ No newline at end of file