-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from zhumeisongsong/feature/get-user-id-to-Obj…
…ectId fix: 🐛 id for MongooseUsersRepository is not objectId
- Loading branch information
Showing
2 changed files
with
18 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserDocument>, | ||
) {} | ||
|
||
async findById(id: string): Promise<User | null> { | ||
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); | ||
} | ||
} |