Skip to content

fix(achievements): handle empty unlockedAchievements array (#2394) - #2395

Open
shrinivas-sn wants to merge 1 commit into
Canopus-Labs:mainfrom
shrinivas-sn:fix/achievements-empty-array-2394
Open

shrinivas-sn wants to merge 1 commit into
Canopus-Labs:mainfrom
shrinivas-sn:fix/achievements-empty-array-2394

Conversation

@shrinivas-sn

@shrinivas-sn shrinivas-sn commented Oct 25, 2026 •

Copy link
Copy Markdown

📝 Pull Request Description

Related Issue

Closes #2394

Summary

When POST /api/achievements is called with { "unlockedAchievements": [] }, the previous implementation passed unlockedAchievements directly to { $addToSet: { unlockedAchievements: { $each: unlockedAchievements } } }. MongoDB's $each operator requires a non-empty array and rejects $each: [] with MongoServerError: 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:

  1. Returning an immediate { success: true } when unlockedAchievements is an empty array [] without executing an illegal $each: [] MongoDB query.
  2. Checking if user exists when executing User.findByIdAndUpdate and returning HTTP 404 if not found.
  3. Adding unit test coverage in backend/tests/achievementController.unit.test.js asserting that empty arrays return { success: true } without invoking User.findByIdAndUpdate, and asserting HTTP 404 when user is not found.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature
  • ♻️ Refactoring
  • 📝 Documentation update
  • 🎨 UI/UX improvement
  • 🔥 Other(please describe) ______

How Has This Been Tested?

  • Ran unit tests: npm test -- tests/achievementController.unit.test.js (10/10 passed).
  • Ran full backend test suite: npm test (254/254 tests passed across 22 test files).
  • Verified clean git diff with 0 whitespace errors.

Checklist

  • My code follows the project's guidelines
  • I have tested my changes
  • I have updated documentation where necessary
  • I have linked the related issue
  • My changes do not introduce new warnings or errors

Summary

saveAchievements now returns { success: true } without a database update when unlockedAchievements is empty. For non-empty arrays, it returns 404 with User not found if the update does not find a user.

Unit tests cover both cases. The changes address the reported empty-array error.

@github-actions

Copy link
Copy Markdown

Thank you for submitting your pull request, @shrinivas-sn! 🙌
We'll review it as soon as possible.
If there are any specific instructions or feedback regarding your PR, we'll provide them here.
Thanks again for your contribution to our project! 😊

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0092c1c7-861c-4c66-9d3b-2462a140289d
Commits

Reviewing files that changed from the base of the PR and between a8d14cf and 15fbfd0.

Files selected for processing: 2
  • backend/controllers/achievementController.js
  • backend/tests/achievementController.unit.test.js

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

saveAchievements returns success without updating the user when unlockedAchievements is empty. When the update finds no user, the controller returns 404. Tests cover both outcomes.

Changes: Achievement save handling

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: Normal

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: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Out of Scope Changes check Warning The added User.findByIdAndUpdate null check changes non-empty requests to return HTTP 404 with User not found. Issue #2394 only covers empty arrays and does not require user-not-found handling. Th… Remove the unrelated user-not-found response change and its dedicated test, unless a separate active linked issue adds this requirement.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the primary change: handling an empty unlockedAchievements array in the achievements controller.
Linked Issues check Passed Issue #2394 requires a successful no-op for an empty unlockedAchievements array without a MongoDB $each: [] update. saveAchievements now returns { success: true } before `User.findByIdAndUpdat…
Docstring Coverage Passed 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…
Full details: Linked Issues check

Explanation

Issue #2394 requires a successful no-op for an empty unlockedAchievements array without a MongoDB $each: [] update. saveAchievements now returns { success: true } before User.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.findByIdAndUpdate null check changes non-empty requests to return HTTP 404 with User not found. Issue #2394 only 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the merge ready PR is mergeable and has no conflicts label Oct 25, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

This pull request has had no activity for 14 days and has been marked as stale. If this is still being worked on, please push a commit or leave a comment within the next 7 days, otherwise it will be closed automatically.

@github-actions github-actions Bot added the stale label Nov 9, 2026
@shrinivas-sn

Copy link
Copy Markdown
Author

Still active and ready for review. All unit tests in �chievementController.unit.test.js pass cleanly (10/10).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready PR is mergeable and has no conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: saveAchievements throws 500 error when unlockedAchievements is an empty array

1 participant