Skip to content

fix(team-billing): move credits when downgrading team to free#28746

Open
sudoKrishna wants to merge 1 commit intocalcom:mainfrom
sudoKrishna:fix/team-credit-downgrade-clean
Open

fix(team-billing): move credits when downgrading team to free#28746
sudoKrishna wants to merge 1 commit intocalcom:mainfrom
sudoKrishna:fix/team-credit-downgrade-clean

Conversation

@sudoKrishna
Copy link
Copy Markdown

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()

const creditService = new CreditService();

const owner = await prisma.membership.findFirst({
  where: {
    teamId: this.team.id,
    role: MembershipRole.OWNER,
  },
});

if (!owner) {
  throw new Error(`No owner found for team ${this.team.id}`);
}

await creditService.moveCreditsFromTeamToUser({
  teamId: this.team.id,
  userId: owner.userId,
});

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

@github-actions github-actions bot added consumer High priority Created by Linear-GitHub Sync labels Apr 5, 2026
@sudoKrishna
Copy link
Copy Markdown
Author

Hi, I’ve implemented credit transfer on team downgrade.
Please let me know if any changes are needed. Thanks!

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

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({
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 5, 2026

Choose a reason for hiding this comment

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

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>
Fix with Cubic

);
}

const creditsToTransfer = teamCreditBalance.additionalCredits;
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 5, 2026

Choose a reason for hiding this comment

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

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>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

consumer High priority Created by Linear-GitHub Sync size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sure all credits are correctly moved over on upgrades and downgrades

1 participant