fix(billing): replace $sortArray with portable $unwind+$sort+$group (MongoDB 5.0.x compat)#3796
Conversation
…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
|
Warning Review limit reached
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 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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
$sortArrayusage inlistLedgerPagewith a portable aggregation pipeline ($unwind+$sort+$group) and filters out thenullsentinel produced for empty arrays. - Adds unit tests covering
listLedgerPageinput guards, null return behavior, ordering expectations, and a$sortArrayregression 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. |
| * 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. |
Summary
$sortArray(MongoDB ≥5.2) inlistLedgerPagewith a portable$unwind→$sort→$grouppipeline compatible with MongoDB ≥4.4$filterguard in the final$projectto prevent anullsentinel entry inledgerPagewhen the ledger array is empty (edge case exposed bypreserveNullAndEmptyArrays)listLedgerPage(input guards, sort order, empty result, portability regression guard) and 1 integration test for the empty-ledger edge caseCloses #3774
Test plan
npm run test:unit— 1740/1740 pass (includes 7 new unit tests forlistLedgerPage)listLedger.perf— 5/5 pass on mongodb-memory-server (proves MongoDB 5.0.x portability)npm run lint— clean