-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: 🐛 id for MongooseUsersRepository is not objectId #62
Conversation
WalkthroughThe changes in this pull request involve updates to the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (3)
libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts (1)
16-17
: Add error handling for invalid ObjectId formatWhile the conversion to ObjectId is correct, consider adding error handling for invalid ObjectId strings to prevent runtime errors.
Consider wrapping the ObjectId creation in a try-catch:
- const _id = new Types.ObjectId(id); - const userDocument = await this.userModel.findById(_id).exec(); + try { + const _id = new Types.ObjectId(id); + const userDocument = await this.userModel.findById(_id).exec(); + } catch (error) { + if (error instanceof Error && error.message.includes('ObjectId')) { + return null; + } + throw error; + }libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts (2)
30-33
: Consider using actual ObjectId in mock dataFor better test fidelity, consider using an actual MongoDB ObjectId instance in the mock data. This would better represent what MongoDB actually returns and could catch type-related issues earlier.
+import { Types } from 'mongoose'; + const mockUser = { - id: '507f1f77bcf86cd799439011', + id: new Types.ObjectId('507f1f77bcf86cd799439011'), name: 'Test User', };
39-42
: LGTM! Consider adding edge case testsThe assertions are thorough, checking both the instance type and property values. However, consider adding tests for edge cases such as malformed ObjectIds.
Example test case to add:
it('should handle malformed ObjectId', async () => { const result = await repository.findById('invalid-id'); expect(result).toBeNull(); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts
(1 hunks)libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts
(1 hunks)
🔇 Additional comments (5)
libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.ts (2)
4-4
: LGTM: Proper import for MongoDB ObjectId handling
The addition of Types
import from mongoose is correct and necessary for proper ObjectId handling.
19-22
: LGTM: Clear and consistent variable naming
The variable renaming from userDoc
to userDocument
improves code readability and maintains consistency with the schema type name UserDocument
.
libs/users/infrastructure/mongoose/src/lib/mongoose-users.repository.spec.ts (3)
34-38
: LGTM!
The mock implementation is clean and follows standard Jest patterns.
50-50
: LGTM!
The update to use a valid ObjectId format is consistent with the PR's objective.
Line range hint 1-52
: Verify test coverage for ObjectId handling
Let's check if there are other areas in the codebase where ObjectId conversion might need similar updates.
✅ Verification successful
Let me gather more information about the ObjectId handling in the codebase.
Test coverage for ObjectId conversion is adequate
The test cases in the spec file implicitly verify ObjectId handling since:
- The implementation in
mongoose-users.repository.ts
shows thatfindById
converts the string ID toTypes.ObjectId
- The test cases pass string IDs ('507f1f77bcf86cd799439011') which would go through this conversion
- Both success and failure scenarios are covered
- No other repositories in the codebase use MongoDB ObjectId that would need similar updates
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for other MongoDB ID usages that might need similar updates
rg -t typescript "findById.*['\"][\w\d]+['\"]" --no-heading
# Look for other MongoDB-related test files that might need similar updates
fd -e spec.ts -e test.ts . | xargs rg "mongoose" --no-heading
Length of output: 758
Script:
#!/bin/bash
# Check the implementation of MongooseUsersRepository for ObjectId handling
ast-grep --pattern 'class MongooseUsersRepository {
$$$
findById($$$) {
$$$
}
$$$
}'
# Check the schema definition for any ObjectId related code
ast-grep --pattern 'Schema({
$$$
})'
# Look for ObjectId imports or usage
rg "ObjectId" -A 2
Length of output: 483
🎉 This PR is included in version 1.2.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Related #47
Summary by CodeRabbit
Bug Fixes
findById
method.Tests