fix(team-billing): move credits when downgrading team to free#28746
Open
sudoKrishna wants to merge 1 commit intocalcom:mainfrom
Open
fix(team-billing): move credits when downgrading team to free#28746sudoKrishna wants to merge 1 commit intocalcom:mainfrom
sudoKrishna wants to merge 1 commit intocalcom:mainfrom
Conversation
Author
|
Hi, I’ve implemented credit transfer on team downgrade. |
Contributor
There was a problem hiding this comment.
2 issues found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/features/ee/billing/service/teams/TeamBillingService.ts">
<violation number="1" location="packages/features/ee/billing/service/teams/TeamBillingService.ts:151">
P1: `downgrade()` is non-atomic: failures in owner lookup/credit transfer can skip billing metadata cleanup, leaving stale subscription metadata after cancellation.</violation>
</file>
<file name="packages/features/ee/billing/credit-service.ts">
<violation number="1" location="packages/features/ee/billing/credit-service.ts:812">
P1: Concurrent executions can double-transfer team credits because transfer amount is read before zeroing without lock/guard.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| throw new Error(`No owner found for team ${this.team.id}`); | ||
| } | ||
|
|
||
| await creditService.moveCreditsFromTeamToUser({ |
Contributor
There was a problem hiding this comment.
P1: downgrade() is non-atomic: failures in owner lookup/credit transfer can skip billing metadata cleanup, leaving stale subscription metadata after cancellation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/features/ee/billing/service/teams/TeamBillingService.ts, line 151:
<comment>`downgrade()` is non-atomic: failures in owner lookup/credit transfer can skip billing metadata cleanup, leaving stale subscription metadata after cancellation.</comment>
<file context>
@@ -133,6 +135,25 @@ export class TeamBillingService implements ITeamBillingService {
+ throw new Error(`No owner found for team ${this.team.id}`);
+ }
+
+ await creditService.moveCreditsFromTeamToUser({
+ teamId : this.team.id,
+ userId : owner.userId,
</file context>
| ); | ||
| } | ||
|
|
||
| const creditsToTransfer = teamCreditBalance.additionalCredits; |
Contributor
There was a problem hiding this comment.
P1: Concurrent executions can double-transfer team credits because transfer amount is read before zeroing without lock/guard.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/features/ee/billing/credit-service.ts, line 812:
<comment>Concurrent executions can double-transfer team credits because transfer amount is read before zeroing without lock/guard.</comment>
<file context>
@@ -792,6 +792,54 @@ export class CreditService {
+ );
+ }
+
+ const creditsToTransfer = teamCreditBalance.additionalCredits;
+
+ logger.info("Moving credits from team to owner" , {
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #28104
When a team is downgraded from Team → Free, the team subscription is removed but the remaining credits are not transferred. This can cause credits to be lost.
This PR fixes that by transferring the remaining team credits to the team owner during downgrade.
What this PR does
Finds the team owner
Transfers team credits to owner
Resets team credits to 0
Keeps behavior consistent with existing credit transfer logic
Added credit transfer logic inside TeamBillingService.downgrade()
Why this change
Credits should not be lost when downgrading a team.
This change ensures credits are preserved and moved to the team owner.
Tested
Team with credits → credits moved to owner
Team without credits → no errors
Owner without credit balance → created automatically