fix(team-billing): move credits when downgrading team to free#28746
fix(team-billing): move credits when downgrading team to free#28746sudoKrishna wants to merge 1 commit intocalcom:mainfrom
Conversation
|
Hi, I’ve implemented credit transfer on team downgrade. |
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({ |
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; |
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 PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active. |
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