Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ move user/usecases to users/application #44

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"cSpell.words": [
"nestjs",
"supergraph",
"usecase"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx

| Layer | Description |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| usecase | Define business use cases and encapsulate business logic. |
| use-case(application) | Define business use cases and encapsulate business logic. |
| entity(domain) | Define core business entities and business rules.<br/> Maintain entity independence from database and framework. |
| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations. <br/>By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. |

Expand Down
7 changes: 0 additions & 7 deletions libs/user/usecase/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions libs/user/usecase/project.json

This file was deleted.

1 change: 0 additions & 1 deletion libs/user/usecase/src/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions libs/user/usecase/tsconfig.json

This file was deleted.

7 changes: 7 additions & 0 deletions libs/users/application/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# users-application

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test users-application` to execute the unit tests via [Jest](https://jestjs.io).
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
displayName: 'user-usecase',
displayName: 'users-application',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/user/usecase',
coverageDirectory: '../../../coverage/libs/users/application',
};
9 changes: 9 additions & 0 deletions libs/users/application/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "users-application",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/users/application/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project users-application --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/users/application/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/use-case/get-user.use-case';
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { GetUserUsecase } from './get-user.usecase';
import { GetUserUseCase } from './get-user.use-case';
import { UserRepository } from '@user/domain';

describe('GetUserUsecase', () => {
let getUserUsecase: GetUserUsecase;
describe('GetUserUseCase', () => {
let getUserUseCase: GetUserUseCase;
let userRepository: jest.Mocked<UserRepository>;

beforeEach(() => {
userRepository = {
findById: jest.fn(),
} as unknown as jest.Mocked<UserRepository>;

getUserUsecase = new GetUserUsecase(userRepository);
getUserUseCase = new GetUserUseCase(userRepository);
});

describe('execute', () => {
it('should return a user when found', async () => {
const user = { id: '1', name: 'John Doe' };
userRepository.findById.mockResolvedValue(user);

const result = await getUserUsecase.execute('1');
const result = await getUserUseCase.execute('1');

expect(result).toEqual(user);
expect(userRepository.findById).toHaveBeenCalledWith('1');
Expand All @@ -27,7 +27,7 @@ describe('GetUserUsecase', () => {
it('should return null when user is not found', async () => {
userRepository.findById.mockResolvedValue(null);

const result = await getUserUsecase.execute('1');
const result = await getUserUseCase.execute('1');

expect(result).toBeNull();
expect(userRepository.findById).toHaveBeenCalledWith('1');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User, UserRepository } from '@user/domain';

export class GetUserUsecase {
export class GetUserUseCase {
constructor(private readonly userRepository: UserRepository) {}

async execute(id: string): Promise<User | null> {
Expand Down
16 changes: 16 additions & 0 deletions libs/users/application/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@libs/config": ["libs/config/src/index.ts"],
"@prompt/domain": ["libs/prompt/domain/src/index.ts"],
"@user/domain": ["libs/user/domain/src/index.ts"],
"@user/usecase": ["libs/user/usecase/src/index.ts"]
"@users/application": ["libs/users/application/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down