From b1990b96413b36cde1eab5e94081b9fd8a2f47f9 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Wed, 20 Nov 2024 14:07:45 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20id=20for=20MongooseUsersR?= =?UTF-8?q?epository=20is=20not=20objectId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/mongoose-users.repository.spec.ts | 16 +++++++++++----- .../src/lib/mongoose-users.repository.ts | 12 +++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts b/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts index a31ff77..ae36827 100644 --- a/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts +++ b/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts @@ -27,13 +27,19 @@ describe('MongooseUsersRepository', () => { }); it('should return a user when found', async () => { - const userDoc = { id: '1', name: 'John Doe' }; + const mockUser = { + id: '507f1f77bcf86cd799439011', + name: 'Test User', + }; + jest.spyOn(userModel, 'findById').mockReturnValue({ - exec: jest.fn().mockResolvedValue(userDoc), + exec: jest.fn().mockResolvedValue(mockUser), } as any); - const user = await repository.findById('1'); - expect(user).toEqual(new User('1', 'John Doe')); + const user = await repository.findById(mockUser.id); + expect(user).toBeInstanceOf(User); + expect(user?.id).toBe(mockUser.id); + expect(user?.name).toBe(mockUser.name); }); it('should return null when user is not found', async () => { @@ -41,7 +47,7 @@ describe('MongooseUsersRepository', () => { exec: jest.fn().mockResolvedValue(null), } as any); - const user = await repository.findById('1'); + const user = await repository.findById('507f1f77bcf86cd799439011'); expect(user).toBeNull(); }); }); \ No newline at end of file diff --git a/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts b/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts index ed004d4..9da4285 100644 --- a/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts +++ b/libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts @@ -1,22 +1,24 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { User, UsersRepository } from '@users/domain'; -import { Model } from 'mongoose'; +import { Model, Types } from 'mongoose'; import { UserDocument } from './user.schema'; -@Injectable() +@Injectable() export class MongooseUsersRepository implements UsersRepository { constructor( @InjectModel(UserDocument.name) private userModel: Model, ) {} async findById(id: string): Promise { - const userDoc = await this.userModel.findById(id).exec(); - if (!userDoc) { + const _id = new Types.ObjectId(id); + const userDocument = await this.userModel.findById(_id).exec(); + + if (!userDocument) { return null; } - return new User(userDoc.id, userDoc.name); + return new User(userDocument.id, userDocument.name); } }