Skip to content

fix(billing): anchor monthly quotas to the subscription period, not the calendar month - #500

Merged
gkhngyk merged 1 commit into
mainfrom
fix/quota-billing-period
Jul 21, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/quota-billing-period

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Volume analysis, content brief and site audit quotas counted usage since the 1st of the calendar month, while the billing UI tells the customer their plan "renews on" the subscription's anniversary day. A customer whose subscription renewed mid-month kept seeing an exhausted quota that would not reset for another week or more — despite having just paid.

  • quotaPeriodStart(orgId) derives the quota window from organizations.subscription_ends_at (Stripe's current_period_end, kept fresh by the webhook): the window is the billing month containing now, so quotas renew the moment the subscription renews.
  • The month walk handles webhook lag right after renewal (ends_at briefly in the past → window starts today, not last month) and annual billing (ends_at far in the future → still a monthly window on the billing day). Day-of-month is clamped Stripe-style (a subscription anchored on the 31st resets on Feb 28/29).
  • Orgs without an active/trialing subscription fall back to the old calendar-month window; self-hosted paths are unaffected (unlimited).
  • Error messages and the prompts-page quota hint now say the quota resets when the subscription renews, instead of "on the 1st".
  • Window math extracted into server/src/lib/quota-period.js as pure functions with unit tests (8 new — the supabase config exits the process when env vars are missing, so tests must not import plan-guard).

Related issue

—

Type of change

  • fix — Bug fix

How to test

  1. yarn test from server/ — quota-period.test.js covers the monthly window, mid-period, webhook lag, annual billing and short-month clamping cases.
  2. On cloud with an org whose subscription renewed mid-month: exhaust the volume quota, then advance past the renewal (or adjust subscription_ends_at) — the quota status endpoint reports usage counted from the billing day, not the 1st.
  3. Orgs with subscription_ends_at = NULL or an inactive status keep the calendar-month window.
  4. Self-hosted: no behavior change (limits are unlimited before any window math runs).

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

…he calendar month

Volume analysis, content brief and site audit quotas counted usage since
the 1st of the calendar month, while the billing UI says the plan renews
on the subscription's anniversary day — a customer whose subscription
renewed mid-month kept seeing an exhausted quota that would not reset
for another week or more, despite having just paid.

quotaPeriodStart() now derives the window from
organizations.subscription_ends_at (Stripe's current_period_end, kept
fresh by the webhook): the quota window is the billing month containing
now, computed with Stripe-style day clamping. The month walk also
handles webhook lag right after renewal and annual billing. Orgs without
an active subscription fall back to the old calendar-month window, and
self-hosted paths are unaffected (unlimited). Error and UI copy now say
the quota resets when the subscription renews.

Window math lives in quota-period.js as pure functions with unit tests
(the supabase config exits the process when env vars are missing, so the
tests must not import plan-guard).
@gkhngyk
gkhngyk merged commit fec807e into main Jul 21, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/quota-billing-period branch July 21, 2026 19:04
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.

1 participant