Skip to content

Commit

Permalink
fix: cancel sub when movingTeamToOrg (#15594)
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-brydon authored Jun 27, 2024
1 parent 7ccb59d commit 00bee12
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Prisma } from "@prisma/client";

import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
import stripe from "@calcom/features/ee/payments/server/stripe";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { UserRepository } from "@calcom/lib/server/repository/user";
Expand Down Expand Up @@ -184,6 +185,7 @@ async function moveTeam({
},
select: {
slug: true,
metadata: true,
members: {
select: {
role: true,
Expand Down Expand Up @@ -238,6 +240,18 @@ async function moveTeam({
teamSlug: newSlug,
orgSlug: org.slug || (orgMetadata?.requestedSlug ?? null),
});

// Cancel existing stripe subscriptions once the team is migrated
const parsedMetadata = teamMetadataSchema.safeParse(team.metadata);
if (parsedMetadata.success) {
const subscriptionId = parsedMetadata.data?.subscriptionId;
if (subscriptionId) {
log.debug("Canceling stripe subscription", safeStringify({ team, subscriptionId }));
await stripe.subscriptions.cancel(subscriptionId);
} else {
log.warn("No subscriptionId found in team metadata", safeStringify({ team, parsedMetadata }));
}
}
}

async function addTeamRedirect({
Expand Down

0 comments on commit 00bee12

Please sign in to comment.