Skip to content

Commit 1945d6c

Browse files
jankeromnesroboquat
authored andcommitted
[server] When deleting a team with an active usage-based subscription, also cancel the subscription
1 parent 2e7d2ef commit 1945d6c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

components/server/ee/src/user/stripe-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export class StripeService {
125125
return result.data[0];
126126
}
127127

128+
async cancelSubscription(subscriptionId: string): Promise<void> {
129+
await this.getStripe().subscriptions.del(subscriptionId);
130+
}
131+
128132
async createSubscriptionForCustomer(customerId: string, currency: Currency): Promise<void> {
129133
const priceId = this.config?.stripeConfig?.usageProductPriceIds[currency];
130134
if (!priceId) {

components/server/ee/src/workspace/gitpod-server-impl.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,17 +1455,22 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
14551455

14561456
protected async onTeamDeleted(teamId: string): Promise<void> {
14571457
const now = new Date();
1458-
const teamSubscription = await this.teamSubscription2DB.findForTeam(teamId, now.toISOString());
1459-
if (!teamSubscription) {
1460-
// No team subscription, nothing to do 🌴
1461-
return;
1458+
const ts2 = await this.teamSubscription2DB.findForTeam(teamId, now.toISOString());
1459+
if (ts2) {
1460+
const chargebeeSubscriptionId = ts2.paymentReference;
1461+
await this.chargebeeService.cancelSubscription(
1462+
chargebeeSubscriptionId,
1463+
{},
1464+
{ teamId, chargebeeSubscriptionId },
1465+
);
1466+
}
1467+
const teamCustomer = await this.stripeService.findCustomerByTeamId(teamId);
1468+
if (teamCustomer) {
1469+
const subsciption = await this.stripeService.findUncancelledSubscriptionByCustomer(teamCustomer.id);
1470+
if (subsciption) {
1471+
await this.stripeService.cancelSubscription(subsciption.id);
1472+
}
14621473
}
1463-
const chargebeeSubscriptionId = teamSubscription.paymentReference;
1464-
await this.chargebeeService.cancelSubscription(
1465-
chargebeeSubscriptionId,
1466-
{},
1467-
{ teamId, chargebeeSubscriptionId },
1468-
);
14691474
}
14701475

14711476
protected async updateTeamSubscriptionQuantity(teamSubscription: TeamSubscription2): Promise<void> {

0 commit comments

Comments
 (0)