Skip to content

fix(billing): derive meterQuota from live plan config, not stale DB snapshot#3667

Open
PierreBrisorgueil wants to merge 1 commit into
masterfrom
fix/meter-quota-stale-on-upgrade
Open

fix(billing): derive meterQuota from live plan config, not stale DB snapshot#3667
PierreBrisorgueil wants to merge 1 commit into
masterfrom
fix/meter-quota-stale-on-upgrade

Conversation

@PierreBrisorgueil
Copy link
Copy Markdown
Contributor

Summary

Mirror of comes-io/trawl_node#1174 (already merged) to keep devkit Node parity and prevent the next /update-stack from wiping the downstream patch — per memory feedback_update_stack_theirs_wipes_patches.

Bug

getUsage controller returned meterQuota: meter?.meterQuota ?? 0. The DB doc's meterQuota is a snapshot baked at last incrementMeter time, so it stays at the old plan's quota (e.g. 10 for free) after the user upgrades to growth (1600), until the next scrap run actually mutates the snapshot. UI reads the stale snapshot instead of the live config — Bug #5/#6.

Fix

Derive meterQuota from BillingPlanService.getActivePlan(plan)?.meterQuota with a fallback to the DB snapshot when the live config returns null (unknown plan id), and 0 as last resort.

Test plan

  • Unit: growth plan returns 1600 (live) even when DB snapshot shows 10
  • Unit: unknown plan falls back to DB snapshot
  • Unit: both unavailable → 0
  • Lint clean, full unit suite (1433 tests) green
  • Downstream propagation already shipped via trawl_node#1174; this PR just realigns devkit Node so future /update-stack doesn't wipe it

Phase 0 critical-review

Same change as trawl_node#1174 (Phase 0 verdict: OK with nits — medium follow-up about logging when getActivePlan returns null is tracked separately).

…napshot

Mirror of trawl_node#1174 to keep devkit Node parity and prevent the next
/update-stack from wiping the downstream patch (per feedback memory
update_stack_theirs_wipes_patches).

Bug: getUsage controller returns `meterQuota: meter?.meterQuota ?? 0`.
The DB doc's `meterQuota` is a snapshot baked at last `incrementMeter`
time, so it stays at the old plan's quota (e.g. 10 for free) after the
user upgrades, until the next scrap run. UI reads the stale snapshot
instead of the live config.

Fix: derive meterQuota from BillingPlanService.getActivePlan(plan)?.meterQuota
with a fallback to the DB snapshot if the live config returns null
(unknown plan id) and 0 as last resort.

Tests cover:
- growth plan returns 1600 (live) even when DB snapshot shows 10
- unknown plan falls back to DB snapshot
- both unavailable → 0
Copilot AI review requested due to automatic review settings May 12, 2026 08:59
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Warning

Rate limit exceeded

@PierreBrisorgueil has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 29 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, 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 have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 63ade391-9258-469e-af43-7aac91181bc2

📥 Commits

Reviewing files that changed from the base of the PR and between bc3ea12 and 1811d75.

📒 Files selected for processing (2)
  • modules/billing/controllers/billing.controller.js
  • modules/billing/tests/billing.usage.endpoint.unit.tests.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/meter-quota-stale-on-upgrade

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
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.54%. Comparing base (cf6ea66) to head (1811d75).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3667   +/-   ##
=======================================
  Coverage   89.54%   89.54%           
=======================================
  Files         138      138           
  Lines        4733     4734    +1     
  Branches     1472     1472           
=======================================
+ Hits         4238     4239    +1     
  Misses        388      388           
  Partials      107      107           
Flag Coverage Δ
integration 59.84% <100.00%> (+<0.01%) ⬆️
unit 64.97% <100.00%> (+<0.01%) ⬆️

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


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc3ea12...1811d75. 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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the billing usage endpoint to report meterQuota from the live plan configuration (instead of a potentially stale BillingUsage DB snapshot) so upgraded orgs immediately see the correct quota in meter mode.

Changes:

  • getUsage now derives meterQuota from BillingPlanService.getActivePlan(plan)?.meterQuota, with fallback to the DB snapshot and then 0.
  • Added unit tests covering: live quota override after upgrade, fallback for unknown plans, and 0 when neither source is available.

Reviewed changes

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

File Description
modules/billing/controllers/billing.controller.js Derives meterQuota from live plan config in meterMode rather than the stored snapshot.
modules/billing/tests/billing.usage.endpoint.unit.tests.js Adds unit coverage for live quota override + fallback behavior in meterMode.

Comment on lines +97 to +101
// Derive meterQuota from the live plan config — DB snapshot is stale after a plan upgrade
// (free → growth) until the next incrementMeter call. Live config is authoritative.
const livePlan = BillingPlanService.getActivePlan(plan);
const liveQuota = livePlan?.meterQuota ?? meter?.meterQuota ?? 0;

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.

2 participants