Skip to content

fix(subscription): move users to the remaining subscription's group on expiry - #8161

Open
duu261 wants to merge 1 commit into
QuantumNous:mainfrom
duu261:fix/subscription-expiry-remaining-group
Open

duu261 wants to merge 1 commit into
QuantumNous:mainfrom
duu261:fix/subscription-expiry-remaining-group

Conversation

@duu261

@duu261 duu261 commented Nov 2, 2026 •

Copy link
Copy Markdown

Agent

  • Tool: Hermes Agent
  • Tool version: v0.22.5
  • Model (full id): claude-opus-5-5
  • Host (CLI / IDE / GitHub coding agent / other): other (chat gateway)
  • Date (UTC): 2026-11-02

Links

User request

  • Verbatim:
Queue code fix in [notes app] new-api related
Change the related user also have that bug subcription behavior
  • Later constraints or corrections from the user (quote, or none):
Pr lgtm
If next release not cover the [rest redacted: private deployment plan]

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user (stop here; do not open a PR): n/a

Open gate — do not open unless all are satisfied

  • Out of scope (including pass-through-only forwarding): no
  • Usage / configuration / integration (answered instead of opening): no
  • Required issue facts present without invention: yes
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • Open: yes
  • If no, what was told to the user (stop here): n/a

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

Issue facts

  • Actual behavior: when a subscription expires (scheduled or admin cancel/delete) while another active subscription grants a different upgrade group, the user keeps the expired subscription's group.
  • Impact: the user keeps the expired plan's group (ratio and channel access) until the other subscription ends.
  • Frequency: every expiry where the remaining active subscription grants a different group.
  • Evidence that the problem is in new-api rather than the client or upstream: group state written by model/subscription.go; reproduced by a unit test on main.
  • Applicable types and their fields (relay / billing / frontend / deployment; write "not applicable" otherwise): not applicable (subscription group state; billing code is unchanged)

Change

ExpireDueSubscriptions and downgradeUserGroupForSubscriptionTx used to return as soon as any other active subscription had an upgrade_group. Now they ask which group the remaining subscriptions grant (activeSubscriptionUpgradeGroupTx):

  • one of them grants the current group: keep it (same as before);
  • none does: move the user to the group of the remaining subscription that ends last;
  • only when the current group was granted by a subscription expiring now: one of the rows this scan marks expired in the scheduled path, the cancelled or deleted sub in the admin path. A group no expiring subscription granted, for example one an admin assigned, is left alone, even if an older expired subscription once granted it.

The path with no remaining upgraded subscription is unchanged. Queries use GORM only.

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ : /en/docs/guide/feature-guide/user/wallet, "Viewing subscription status" item 2: with other active upgrade subscriptions, "the current group may be maintained; the final group will be processed in conjunction with other subscription statuses".
  • https://deepwiki.com/QuantumNous/new-api : "Payments & Subscriptions"; no setting for this case.
  • README / repo docs: nothing relevant.
  • Code paths and what they imply for this change: ExpireDueSubscriptions (scheduled expiry) and downgradeUserGroupForSubscriptionTx (AdminInvalidateUserSubscription, AdminDeleteUserSubscription) share the same early return, so both are changed.

Alternatives considered

  • Option A: rank groups (higher tier wins). Rejected: new-api has no group ranking.
  • Option B: always use the remaining subscription that ends last. Rejected: a user still holding a subscription for the current group would be moved needlessly.
  • Why this approach: keeps today's behavior whenever the current group is still paid for, and only fixes the stale case.

Files

Path Why
model/subscription.go Resolve the remaining subscription's group in both expiry paths
model/subscription_auth_test.go Table test for scheduled and admin expiry

Behavior

Verification

  • Commands and results:
    • RED on main 70e6af6 with only the new test: go test ./model/ -run 'TestSubscriptionExpiryMovesUserToRemainingSubscriptionGroup$' -count=1 -> FAIL .../scheduled/remaining_lower_tier and .../admin/remaining_lower_tier, expected: "basic" actual: "pro".
    • GREEN with the fix: same command, 10/10 subtests pass (scheduled and admin x lower tier, current group still granted, no remaining subscription, manually assigned group, manually assigned group that an older expired subscription once granted).
    • go test ./model/ -count=1 -> ok github.com/QuantumNous/new-api/model 12.975s; go vet ./model/ clean; gofmt -l model/ empty.
    • Same test against real MySQL 8.4.13 and PostgreSQL 16.15 (docker containers) through a local harness that points DB at each engine: --- PASS on both.
  • Manual steps and observed result: none beyond the tests.
  • UI: screenshot or recording (or why none): no UI change.
  • Tests added or updated, or why none: one table test in the existing model/subscription_auth_test.go.
  • Databases / providers / platforms exercised: SQLite (in-memory), MySQL 8.4.13, PostgreSQL 16.15; linux/amd64, Go 1.26.8.
  • Not verified: MySQL 5.7.8 and PostgreSQL 9.6 minimums; no schema change, only WHERE/ORDER BY/COUNT through GORM. The local MySQL/PostgreSQL harness is not part of the PR.

Risks

  • Failure modes: a user holding a remaining subscription for a lower group now loses the higher group when the higher plan expires. That is the intended fix, but deployments that relied on the old behavior will see it.
  • Billing / quota / auth impact: the group ratio applied to later requests follows the corrected group. Quota, settlement and auth are unchanged.
  • Follow-ups: none required.

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no

Summary by CodeRabbit

  • Bug Fixes
    • When an upgraded subscription expires or is invalidated, the account now switches to the group provided by another active upgraded subscription when appropriate.
    • Accounts retain their current group when it is still supported by another subscription or was assigned independently. If no active subscription supports the current subscription-granted group, the account returns to the default group.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8cc0bcf4-1251-43e2-b183-f1dfb66db67a
Commits

Reviewing files that changed from the base of the PR and between 70e6af6 and d3dcd6f.

Files selected for processing: 2
  • model/subscription.go
  • model/subscription_auth_test.go

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

Walkthrough

Subscription expiry and admin invalidation now select a group granted by remaining active subscriptions. They preserve the current group when it is still granted or was not granted by a subscription expiring now. Tests cover both paths.

Changes

Subscription group transitions

Layer / File(s) Summary
Select group during admin invalidation
model/subscription.go
A shared selector chooses the current group when a remaining active subscription grants it. Otherwise, it selects the group from the subscription ending latest. Admin invalidation changes the user’s group only when the current group matches the removed subscription’s upgrade group.
Apply group selection during scheduled expiry
model/subscription.go, model/subscription_auth_test.go
Scheduled expiry records groups from due subscriptions and updates the user’s group only when the current group matches one of them. Tests cover scheduled expiry and admin invalidation, including cases where a remaining or expired subscription grants the current group.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested reviewers: calcium-ion

Merge Risk: Minimal · up to d3dcd

No actionable issue is established by the supplied evidence; the change appears mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
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 names the main change: when a subscription expires, users move to the group of the remaining active subscription. The raw summary confirms this is the core fix in model/subscription.go (Expi…
Linked Issues check Passed The only directly linked issue, #8160, is closed. It supplies historical context only. No active directly linked targets remain, so no linked-issue coding requirements apply. For reference, the diff a…
Out of Scope Changes check Passed The whole-PR diff against merge base 70e6af6 touches only model/subscription.go and model/subscription_auth_test.go. Both files match the PR's Files table. The model/subscription.go hunks change…
Full details: Title check

Explanation

The title names the main change: when a subscription expires, users move to the group of the remaining active subscription. The raw summary confirms this is the core fix in model/subscription.go (ExpireDueSubscriptions and downgradeUserGroupForSubscriptionTx), with a supporting test. The title is specific and follows the conventional-commit style. It is 77 characters, within a readable length.

Full details: Linked Issues check

Explanation

The only directly linked issue, #8160, is closed. It supplies historical context only. No active directly linked targets remain, so no linked-issue coding requirements apply. For reference, the diff at head d3dcd6f still implements the behavior described in #8160. In model/subscription.go, ExpireDueSubscriptions and downgradeUserGroupForSubscriptionTx now call the new activeSubscriptionUpgradeGroupTx. The user moves to the remaining subscription's group only when the current group was granted by a subscription expiring now. A group granted by no expiring subscription is left unchanged. The new test TestSubscriptionExpiryMovesUserToRemainingSubscriptionGroup covers the scheduled and admin paths. The author reports that it passes; this assessment did not run it.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against merge base 70e6af6 touches only model/subscription.go and model/subscription_auth_test.go. Both files match the PR's Files table. The model/subscription.go hunks change only the remaining-subscription lookup, the group update, and the due-group capture in the scheduled expiry and admin downgrade paths. They also add the slices import and the helper function. Every hunk serves the expiry group-selection behavior described in #8160 and the PR description. The test hunk covers the same behavior. No unrelated refactor, feature, or configuration change appears in the diff.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

⚠️ This pull request shows signs of AI-generated slop (ai_padded_prose). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

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

I’m a rabbit watching plans expire,
I hop as groups change their attire.
The latest plan may set the way,
A granted group can choose to stay.
Then I tuck the tests away.

This branch has not been deployed

No deployments
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.

Subscription expiry keeps the expired plan's group while another active subscription grants a different group

1 participant