Skip to content

fix(billing): replace $sortArray with portable $unwind+$sort+$group (MongoDB 5.0.x compat)#3796

Merged
PierreBrisorgueil merged 1 commit into
masterfrom
fix/billing-sortarray-mongo52
Jun 4, 2026
Merged

fix(billing): replace $sortArray with portable $unwind+$sort+$group (MongoDB 5.0.x compat)#3796
PierreBrisorgueil merged 1 commit into
masterfrom
fix/billing-sortarray-mongo52

Conversation

@PierreBrisorgueil

Copy link
Copy Markdown
Contributor

Summary

  • Replaces $sortArray (MongoDB ≥5.2) in listLedgerPage with a portable $unwind$sort$group pipeline compatible with MongoDB ≥4.4
  • Adds a $filter guard in the final $project to prevent a null sentinel entry in ledgerPage when the ledger array is empty (edge case exposed by preserveNullAndEmptyArrays)
  • Adds 7 unit tests for listLedgerPage (input guards, sort order, empty result, portability regression guard) and 1 integration test for the empty-ledger edge case

Closes #3774

Test plan

  • npm run test:unit — 1740/1740 pass (includes 7 new unit tests for listLedgerPage)
  • Integration tests for listLedger.perf — 5/5 pass on mongodb-memory-server (proves MongoDB 5.0.x portability)
  • npm run lint — clean
  • CI on GitHub runners

…up in listLedgerPage

\$sortArray requires MongoDB ≥5.2 and fails on 5.0.x CI environments.
Replaces it with \$unwind + \$sort + \$group which works on MongoDB ≥4.4.
Adds a \$filter guard to prevent null sentinel in ledgerPage when ledger is empty.

Closes #3774
Copilot AI review requested due to automatic review settings June 4, 2026 15:48
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@PierreBrisorgueil, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 59 minutes and 2 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 152fdeed-38dc-4498-8dc5-17ef467e4f16

📥 Commits

Reviewing files that changed from the base of the PR and between 690e756 and cff1391.

📒 Files selected for processing (3)
  • modules/billing/repositories/billing.extraBalance.repository.js
  • modules/billing/tests/billing.extraBalance.listLedger.perf.integration.tests.js
  • modules/billing/tests/billing.extraBalance.unit.tests.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/billing-sortarray-mongo52

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 and usage tips.

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.23%. Comparing base (428fb18) to head (cff1391).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3796   +/-   ##
=======================================
  Coverage   90.23%   90.23%           
=======================================
  Files         152      152           
  Lines        5026     5028    +2     
  Branches     1599     1600    +1     
=======================================
+ Hits         4535     4537    +2     
  Misses        386      386           
  Partials      105      105           
Flag Coverage Δ
integration 59.06% <ø> (-0.03%) ⬇️
unit 69.76% <ø> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 428fb18...cff1391. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates BillingExtraBalanceRepository.listLedgerPage to avoid the MongoDB 5.2+ $sortArray operator by switching to a portable $unwind$sort$group pipeline, and adds regression tests around ordering and the empty-ledger edge case.

Changes:

  • Replaces $sortArray usage in listLedgerPage with a portable aggregation pipeline ($unwind + $sort + $group) and filters out the null sentinel produced for empty arrays.
  • Adds unit tests covering listLedgerPage input guards, null return behavior, ordering expectations, and a $sortArray regression guard.
  • Adds an integration test to ensure an empty ledger returns ledgerPage: [] (not [null]).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
modules/billing/repositories/billing.extraBalance.repository.js Reworks the aggregation pipeline to avoid $sortArray and adds an empty-ledger guard via $filter.
modules/billing/tests/billing.extraBalance.unit.tests.js Adds unit tests for listLedgerPage behavior and a portability regression guard against $sortArray.
modules/billing/tests/billing.extraBalance.listLedger.perf.integration.tests.js Adds an integration regression test for the empty-ledger [null] sentinel case.

Comment on lines +403 to +405
* Note: $sortArray (MongoDB ≥5.2) is intentionally avoided so the repository
* works on MongoDB 5.0.x (mongodb-memory-server default in CI).
* The $unwind + $sort + $group pattern is equivalent and fully portable.
@PierreBrisorgueil PierreBrisorgueil merged commit b355e97 into master Jun 4, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(billing): $sortArray in listLedgerPage requires MongoDB 5.2+ (fails on 5.0.x)

2 participants