From 38b4446674a24747d705218c297bd19a59631e2f Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Thu, 14 Nov 2024 16:17:44 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20import=20User?= =?UTF-8?q?=20from=20@user/domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/users/users.resolver.spec.ts | 22 +++++++++---------- .../src/users/users.resolver.ts | 6 ++--- .../src/users/users.service.spec.ts | 9 ++++---- .../src/users/users.service.ts | 12 +++++----- libs/user/domain/src/index.ts | 2 +- tsconfig.base.json | 7 +++--- 6 files changed, 29 insertions(+), 29 deletions(-) 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/src/index.ts b/libs/user/domain/src/index.ts index d6e22b8..2dbc61e 100644 --- a/libs/user/domain/src/index.ts +++ b/libs/user/domain/src/index.ts @@ -1 +1 @@ -export * from './lib/domain'; +export * from './lib/user.entity'; 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"]