Repository navigation
fix(achievements): handle empty unlockedAchievements array (#2394) - #2395
shrinivas-sn wants to merge 1 commit into
Conversation
|
Thank you for submitting your pull request, @shrinivas-sn! 🙌 |
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Save response handling backend/controllers/achievementController.js, backend/tests/achievementController.unit.test.js |
An empty unlockedAchievements array returns success without calling the update. If the update finds no user, the controller returns 404 with User not found. Tests cover both responses. |
Priority:
Estimated code review effort: 2 (Simple) | ~8 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers: hariom888
Merge Risk: 
Minimal · up to 15fbf
-
The requested achievement-save behavior is implemented and no actionable merge-blocking risk remains.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Linked Issues check
-
Explanation
Issue
#2394requires a successful no-op for an emptyunlockedAchievementsarray without a MongoDB$each: []update.saveAchievementsnow returns{ success: true }beforeUser.findByIdAndUpdate, and the unit test verifies both the response and the absence of the update call. The existing array validation remains in place.
Full details: Docstring Coverage
-
Explanation
No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.
-
Full details: Out of Scope Changes check
-
Explanation
The added
User.findByIdAndUpdatenull check changes non-empty requests to return HTTP 404 withUser not found. Issue#2394only covers empty arrays and does not require user-not-found handling. The added 404 test covers this unrelated behavior.Resolution
Remove the unrelated user-not-found response change and its dedicated test, unless a separate active linked issue adds this requirement.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
Some tools did not complete. Review the errors below.
🔧 Biome (2.5.15)
-
backend/tests/achievementController.unit.test.js
-
File contains syntax errors that prevent linting: Line 1: Illegal use of an import declaration outside of a module
-
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
This pull request has had no activity for 14 days and has been marked as |
|
Still active and ready for review. All unit tests in �chievementController.unit.test.js pass cleanly (10/10). |
📝 Pull Request Description
Related Issue
Closes #2394
Summary
When
POST /api/achievementsis called with{ "unlockedAchievements": [] }, the previous implementation passedunlockedAchievementsdirectly to{ $addToSet: { unlockedAchievements: { $each: unlockedAchievements } } }. MongoDB's$eachoperator requires a non-empty array and rejects$each: []withMongoServerError: The $each special array operator must be used with a non-empty array, causing an unhandled HTTP 500 error.This PR fixes the issue by:
{ success: true }whenunlockedAchievementsis an empty array[]without executing an illegal$each: []MongoDB query.userexists when executingUser.findByIdAndUpdateand returning HTTP 404 if not found.backend/tests/achievementController.unit.test.jsasserting that empty arrays return{ success: true }without invokingUser.findByIdAndUpdate, and asserting HTTP 404 when user is not found.Type of Change
How Has This Been Tested?
npm test -- tests/achievementController.unit.test.js(10/10 passed).npm test(254/254 tests passed across 22 test files).Checklist
Summary
saveAchievementsnow returns{ success: true }without a database update whenunlockedAchievementsis empty. For non-empty arrays, it returns 404 withUser not foundif the update does not find a user.Unit tests cover both cases. The changes address the reported empty-array error.