diff --git a/apps/users-application/src/users/users.resolver.spec.ts b/apps/users-application/src/users/users.resolver.spec.ts index e962238..26fcae9 100644 --- a/apps/users-application/src/users/users.resolver.spec.ts +++ b/apps/users-application/src/users/users.resolver.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersResolver } from './users.resolver'; import { UsersService } from './users.service'; -import { User } from './models/user.model'; +import { User } from '@user/domain'; describe('UsersResolver', () => { let resolver: UsersResolver; @@ -30,35 +30,35 @@ describe('UsersResolver', () => { describe('getUser', () => { it('should return a user by id', () => { - const user: User = { id: 1, name: 'John Doe' }; + const user: User = { id: '1', name: 'John Doe' }; jest.spyOn(service, 'findById').mockReturnValue(user); - expect(resolver.getUser(1)).toEqual(user); - expect(service.findById).toHaveBeenCalledWith(1); + expect(resolver.getUser('1')).toEqual(user); + expect(service.findById).toHaveBeenCalledWith('1'); }); it('should return undefined if user not found', () => { jest.spyOn(service, 'findById').mockReturnValue(undefined); - expect(resolver.getUser(1)).toBeUndefined(); - expect(service.findById).toHaveBeenCalledWith(1); + expect(resolver.getUser('2')).toBeUndefined(); + expect(service.findById).toHaveBeenCalledWith('2'); }); }); describe('resolveReference', () => { it('should return a user by reference id', () => { - const user: User = { id: 1, name: 'John Doe' }; + const user: User = { id: '1', name: 'John Doe' }; jest.spyOn(service, 'findById').mockReturnValue(user); - expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toEqual(user); - expect(service.findById).toHaveBeenCalledWith(1); + expect(resolver.resolveReference({ __typename: 'User', id: '1' })).toEqual(user); + expect(service.findById).toHaveBeenCalledWith('1'); }); it('should return undefined if user not found by reference id', () => { jest.spyOn(service, 'findById').mockReturnValue(undefined); - expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toBeUndefined(); - expect(service.findById).toHaveBeenCalledWith(1); + expect(resolver.resolveReference({ __typename: 'User', id: '2' })).toBeUndefined(); + expect(service.findById).toHaveBeenCalledWith('2'); }); }); }); \ No newline at end of file diff --git a/apps/users-application/src/users/users.resolver.ts b/apps/users-application/src/users/users.resolver.ts index 27c438d..be678b7 100644 --- a/apps/users-application/src/users/users.resolver.ts +++ b/apps/users-application/src/users/users.resolver.ts @@ -1,6 +1,6 @@ import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql'; +import { User } from '@user/domain'; -import { User } from './models/user.model'; import { UsersService } from './users.service'; @Resolver(() => User) @@ -8,14 +8,14 @@ export class UsersResolver { constructor(private usersService: UsersService) {} @Query(() => User, { nullable: true }) - getUser(@Args({ name: 'id', type: () => ID }) id: number): User | undefined { + getUser(@Args({ name: 'id', type: () => ID }) id: string): User | undefined { return this.usersService.findById(id); } @ResolveReference() resolveReference(reference: { __typename: string; - id: number; + id: string; }): User | undefined { return this.usersService.findById(reference.id); } diff --git a/apps/users-application/src/users/users.service.spec.ts b/apps/users-application/src/users/users.service.spec.ts index a253487..17af965 100644 --- a/apps/users-application/src/users/users.service.spec.ts +++ b/apps/users-application/src/users/users.service.spec.ts @@ -1,6 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; +import { User } from '@user/domain'; + import { UsersService } from './users.service'; -import { User } from './models/user.model'; describe('UsersService', () => { let service: UsersService; @@ -18,12 +19,12 @@ describe('UsersService', () => { }); it('should return a user by id', () => { - const user: User = service.findById(1); - expect(user).toEqual({ id: 1, name: 'John Doe' }); + const user: User = service.findById('1'); + expect(user).toEqual({ id: '1', name: 'John Doe' }); }); it('should return undefined if user is not found', () => { - const user: User = service.findById(3); + const user: User = service.findById('3'); expect(user).toBeUndefined(); }); }); \ No newline at end of file diff --git a/apps/users-application/src/users/users.service.ts b/apps/users-application/src/users/users.service.ts index 6048d31..e9757b0 100644 --- a/apps/users-application/src/users/users.service.ts +++ b/apps/users-application/src/users/users.service.ts @@ -1,14 +1,14 @@ import { Injectable } from '@nestjs/common'; -import { User } from './models/user.model'; +import { User } from '@user/domain'; @Injectable() export class UsersService { private users: User[] = [ - { id: 1, name: 'John Doe' }, - { id: 2, name: 'Richard Roe' }, + { id: '1', name: 'John Doe' }, + { id: '2', name: 'Richard Roe' }, ]; - findById(id: number): User | undefined { - return this.users.find((user) => user.id === Number(id)); + findById(id: string): User | undefined { + return this.users.find((user) => user.id === id); } -} \ No newline at end of file +} diff --git a/libs/user/domain/README.md b/libs/user/domain/README.md new file mode 100644 index 0000000..b3a89ab --- /dev/null +++ b/libs/user/domain/README.md @@ -0,0 +1,7 @@ +# domain + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test domain` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/user/domain/eslint.config.js b/libs/user/domain/eslint.config.js new file mode 100644 index 0000000..07e518f --- /dev/null +++ b/libs/user/domain/eslint.config.js @@ -0,0 +1,3 @@ +const baseConfig = require('../../../eslint.config.js'); + +module.exports = [...baseConfig]; diff --git a/libs/user/domain/jest.config.ts b/libs/user/domain/jest.config.ts new file mode 100644 index 0000000..4334d1b --- /dev/null +++ b/libs/user/domain/jest.config.ts @@ -0,0 +1,10 @@ +export default { + displayName: 'domain', + preset: '../../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../../coverage/libs/user/domain', +}; diff --git a/libs/user/domain/project.json b/libs/user/domain/project.json new file mode 100644 index 0000000..e7be118 --- /dev/null +++ b/libs/user/domain/project.json @@ -0,0 +1,9 @@ +{ + "name": "domain", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/user/domain/src", + "projectType": "library", + "tags": [], + "// targets": "to see all targets run: nx show project domain --web", + "targets": {} +} diff --git a/libs/user/domain/src/index.ts b/libs/user/domain/src/index.ts new file mode 100644 index 0000000..2dbc61e --- /dev/null +++ b/libs/user/domain/src/index.ts @@ -0,0 +1 @@ +export * from './lib/user.entity'; diff --git a/libs/user/domain/src/lib/user.entity.spec.ts b/libs/user/domain/src/lib/user.entity.spec.ts new file mode 100644 index 0000000..7e91f5c --- /dev/null +++ b/libs/user/domain/src/lib/user.entity.spec.ts @@ -0,0 +1,15 @@ +import { User } from './user.entity'; + +describe('User Entity', () => { + it('should create a user with id and name', () => { + const user = new User('1', 'John Doe'); + expect(user.id).toBe('1'); + expect(user.name).toBe('John Doe'); + }); + + it('should allow updating the name', () => { + const user = new User('1', 'John Doe'); + user.name = 'Jane Doe'; + expect(user.name).toBe('Jane Doe'); + }); +}); \ No newline at end of file diff --git a/libs/user/domain/src/lib/user.entity.ts b/libs/user/domain/src/lib/user.entity.ts new file mode 100644 index 0000000..202554e --- /dev/null +++ b/libs/user/domain/src/lib/user.entity.ts @@ -0,0 +1,6 @@ +export class User { + constructor( + public readonly id: string, + public name: string, + ) {} +} diff --git a/libs/user/domain/tsconfig.json b/libs/user/domain/tsconfig.json new file mode 100644 index 0000000..0dc79ca --- /dev/null +++ b/libs/user/domain/tsconfig.json @@ -0,0 +1,22 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noPropertyAccessFromIndexSignature": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/user/domain/tsconfig.lib.json b/libs/user/domain/tsconfig.lib.json new file mode 100644 index 0000000..e583571 --- /dev/null +++ b/libs/user/domain/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/libs/user/domain/tsconfig.spec.json b/libs/user/domain/tsconfig.spec.json new file mode 100644 index 0000000..69a251f --- /dev/null +++ b/libs/user/domain/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 91bb2ed..58a3b4a 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -15,15 +15,14 @@ "skipDefaultLibCheck": true, "baseUrl": ".", "paths": { - "@libs/config": [ - "libs/config/src/index.ts" - ], + "@libs/config": ["libs/config/src/index.ts"], "@prompt/user-prompt-log-entity": [ "libs/prompt/domain/user-prompt-log-entity/src/index.ts" ], "@prompt/wellbeing-prompt-entity": [ "libs/prompt/domain/wellbeing-prompt-entity/src/index.ts" - ] + ], + "@user/domain": ["libs/user/domain/src/index.ts"] } }, "exclude": ["node_modules", "tmp"]